今天看了一个关于sleep的面试题,然后自己想了一个关于wait的 具体实现是这样的
有三个buton 分别是hello stop wake 实现的功能是 当点击hello就在控制台输出一个hello 然后点击stop 输出hello就wait指导再点击wake然后中间点了几次hello就一下输出几个hello下面是我写的 但是写不对,线程这东西好久不用都忘了,希望各位指点一下。import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;public class WaitNotifyTest extends JFrame {
private JButton sayHello;
private JButton stop;
private JButton wake;
private boolean flag; public static void main(String[] args) {
new WaitNotifyTest().launch();
} public void launch() {
sayHello = new JButton("hello");
stop = new JButton("stop");
wake = new JButton("wake");
flag = true;
add(sayHello);
add(stop);
add(wake);
sayHello.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
while (flag == false) {
synchronized (this) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("hello");
}
}); stop.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
flag = false;
}
}); wake.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
synchronized (this) {
flag = true;
this.notify();
}
}
});
this.setLayout(new FlowLayout());
this.pack();
this.setVisible(true);
}}

解决方案 »

  1.   

    一个线程里,一旦进入wait(),线程进入阻塞,谁能唤醒?
    修改了一下,楼主参考:
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JFrame;public class WaitNotifyTest extends JFrame {
        private JButton sayHello;
        private JButton stop;
        private JButton wake;
        private boolean flag;    static Object ob=new Object(); //用于做同步对象锁。
        public static void main(String[] args) {
            new WaitNotifyTest().launch();
        }    public void launch() {
            sayHello = new JButton("hello");
            stop = new JButton("stop");
            wake = new JButton("wake");
            flag = true;
            add(sayHello);
            add(stop);
            add(wake);
            sayHello.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent arg0) {
    new Thread(new Runnable(){ //内部类再创建并启动一个线程
    public void run(){
                    while (flag == false) {
                        synchronized (WaitNotifyTest.ob) {
                            try {                            WaitNotifyTest.ob.wait(); //不用this了.  } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    System.out.println("hello");
                }
        }).start();
        }
            });        stop.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    flag = false;
                }
            });        wake.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    synchronized (WaitNotifyTest.ob) { //这的同步对象也变了。
                        flag = true;
                        WaitNotifyTest.ob.notify();
                    }
                }
            });
            this.setLayout(new FlowLayout());
            this.pack();
            this.setVisible(true);
        }}
      

  2.   


    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JFrame;public class WaitNotifyTest extends JFrame {
        private JButton sayHello;
        private JButton stop;
        private JButton wake;
        private boolean flag;    public static void main(String[] args) {
          WaitNotifyTest t=  new WaitNotifyTest();//改哈这里 this不要乱用  由于直接new出来的东西没有引用,不是实体不能被引用
          
          t.launch(); //楼主接分吧
        }    public void launch() {
            sayHello = new JButton("hello");
            stop = new JButton("stop");
            wake = new JButton("wake");
            flag = true;
            add(sayHello);
            add(stop);
            add(wake);
            sayHello.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    while (flag == false) {
                        synchronized (this) {
                            try {
                                this.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    System.out.println("hello");
                }
            });        stop.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    flag = false;
                }
            });        wake.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    synchronized (this) {
                        flag = true;
                        this.notify();
                    }
                }
            });
            this.setLayout(new FlowLayout());
            this.pack();
            this.setVisible(true);
        }}
      

  3.   

    改成 WaitNotifyTest.ob.notifyAll(); 就行了 谢谢你 我明白了