synchronized public void waitButton(){ try{ wait(); }catch( InterruptedException e){} }
synchronized public void step(){ notify(); }
public void run(){ String st=""; for(int i=0;i<10;i++){ for(int j=0;i<10000;j++){ //dummy } st +="*"; main.msgLabel.setText(st); waitButton(); } main.stepButton.setEnabled(false); }
public class sync extends Applet{ proc proc1; boolean alive=false; public Button stepButton = new Button(); public Label msgLabel=new Label(); public void init(){ msgLabel.setText("textField1"); msgLabel.setBounds(new Rectangle(20, 20, 100, 20)); stepButton.setLabel("step"); stepButton.setBounds(new Rectangle(40, 60, 70, 20)); stepButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { stepButton_actionPerformed(e); } }); setLayout(null); add(msgLabel); add(stepButton, null); } public void start(){ proc1=new proc(this); proc1.start(); } void stepButton_actionPerformed(ActionEvent e) { proc1.step(); //proc1.notify(); } }
public class proc extends Thread{ sync main; public proc(sync sync) { this.main=sync; } public void run(){ String st=""; for(int i=0;i<10;i++){ for(int j=0;i<10000;j++){ //dummy } st +="*"; main.msgLabel.setText(st); waitButton(); } main.stepButton.setEnabled(false); } synchronized public void waitButton(){ try{ wait(); }catch( InterruptedException e){} } synchronized public void step(){ notify(); } }