菜鸟题---用一个线程的两个按钮控制另一个线程的wait()和notify() ,程序如下,实现不了
用一个线程的两个按钮控制另一个线程的wait()和notify() ,程序如下,实现不了
是哪里出错了?
import java.awt.*;
import java.awt.event.*;
class process extends Frame implements ActionListener
{
TextField text;
Button b1,b2;
mythread thread;
process(String t)
{
super(t);
setLayout(new FlowLayout());
b1=new Button("stop");
b2=new Button("resume");
text=new TextField(20);
thread=new mythread(text);
b1.addActionListener(this);
b2.addActionListener(this);
add(text);
add(b1);
add(b2);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
System.exit(0);
}
});
thread.start();
pack();
show();
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==b1)
{
try{
thread.w();
}
catch(Exception e)
{
}
}
if(event.getSource()==b2)
{
thread.n();
}
}
public static void main(String args[])
{
process m=new process("myprocess");
}
}
class mythread extends Thread
{
TextField text;
mythread(TextField t)
{
text=t;
}
synchronized public void w()
{
try{
wait();
}
catch(Exception e)
{
}
}
synchronized public void n()
{
notify();
}
public void run()
{
for(int i=0;i<=1000;i++)
{
text.setText("i="+i);
try{
sleep(500);
}
catch(Exception e)
{
}
}
}
}