wait一般在多个线程共享临界区时使用
唤醒是由其他线程来唤醒
你这里只有一个线程,它wait后如何唤醒自己呢?所以可以使用suspend和resume
如:
synchronized(this)
{
if(isWait)
this.suspend();
}t.isWait=false;
//t.n();
//t.notify();
t.resume();功能可以实现,不过java不建议用这2个方法,不好意思,替代的方法还没有试过,可以自行参考看看。

解决方案 »

  1.   

    我这样做主要是为了严正我对JAVA线程的理解.见http://community.csdn.net/Expert/topic/3588/3588919.xml?temp=.9045832
    一个你说的这两个方法回造成对象的损坏。我看了corejava中建议的代替方法和和用notify()差不多。
    关键是为什么不能实现那?
      

  2.   

    他是通过他自己的notify()方法呀!
      

  3.   

    把一个线程wait()在自己的对象上,在自己的对象上调用notify方法应该是可以的呀。
      

  4.   

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import javax.swing.AbstractAction;
    import java.awt.event.ActionEvent;public class WaitAndNotify 
    {
    public static void main(String[] args) 
    {
    System.out.println("Hello World!");
    WaitAndNotifyJFrame frame = new WaitAndNotifyJFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.show();
    }
    }
    class WaitAndNotifyJFrame extends JFrame
    {
    public WaitAndNotifyJFrame()
    {
    setSize(300,100);
    setLocation(250,250);
    JPanel panel = new JPanel();
    JButton start = new JButton(new AbstractAction("Start")
    {
    public void actionPerformed(ActionEvent event)
    {
    if(t == null)
    {
    t = new WaitAndNotifyThread(WaitAndNotifyJFrame.this);
    t1 = new WaitAndNotifyThread(WaitAndNotifyJFrame.this);
    t.start();
    t1.start();
    }
    else
    {
    t.isWait=false;
    t1.isWait=true;
    }
    }
    });
    panel.add(start);
    JButton pause = new JButton(new AbstractAction("Pause")
    {
    public void actionPerformed(ActionEvent e)
    {
    t.isWait = true;
    t1.isWait=false;
    }
    });
    panel.add(pause);
    JButton end = new JButton(new AbstractAction("End")
    {
    public void actionPerformed(ActionEvent e)
    {
    t.interrupt();
    t = null;
    }
    });
    panel.add(end);
    getContentPane().add(panel);
    }
    private WaitAndNotifyThread t,t1;
    }
    class WaitAndNotifyThread extends Thread
    {
    public static Object ob=new Object();
    public WaitAndNotifyThread(WaitAndNotifyJFrame f)
    {
    control = f;
    isWait = false;
    count = 0;
    }
    public  void run()
    {
    try
    {
    synchronized(ob){
    while(true)
    {
    System.out.println(Thread.currentThread().getName()+"Count:"+count++);
    sleep(1000);

    if(isWait){
        System.out.println(Thread.currentThread().getName()+"wait");
        ob.notify();
    ob.wait();
    }
    }
    }
    }catch(Exception e){}
    }
    public void n()
    {
    notify();
    }
    public boolean isWait;
    private WaitAndNotifyJFrame control;
    private int count;
    }这是改的一个例子
    t和t1互斥运行
    wait和notify作用在同一个对象ob上。但不能依靠ob自己唤醒自己。
      

  5.   

    那么我把一个线程锁在JFrame上也不行呀。这就不是自己锁自己了。为什么还不行那?一定要用两个线程吗?
      

  6.   

    问题的关键不是锁定。这个程序一开始的pause是好用的。问题出在唤醒。
    这是用JFrame作为锁的CODE
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import javax.swing.AbstractAction;
    import java.awt.event.ActionEvent;public class WaitAndNotifyFromWeb 
    {
    public static void main(String[] args) 
    {
    System.out.println("Hello World!");
    WaitAndNotifyJFrame frame = new WaitAndNotifyJFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.show();
    }
    }
    class WaitAndNotifyJFrame extends JFrame
    {
    public WaitAndNotifyJFrame()
    {
    setSize(300,100);
    setLocation(250,250);
    JPanel panel = new JPanel();
    JButton start = new JButton(new AbstractAction("Start")
    {
    public void actionPerformed(ActionEvent event)
    {
    if(t == null)
    {
    t = new WaitAndNotifyThread(WaitAndNotifyJFrame.this);
    t1 = new WaitAndNotifyThread(WaitAndNotifyJFrame.this);
    t.start();
    t1.start();
    }
    else
    {
    t.isWait=false;
    t1.isWait=true;
    }
    }
    });
    panel.add(start);
    JButton pause = new JButton(new AbstractAction("Pause")
    {
    public void actionPerformed(ActionEvent e)
    {
    t.isWait = true;
    t1.isWait=false;
    }
    });
    panel.add(pause);
    JButton end = new JButton(new AbstractAction("End")
    {
    public void actionPerformed(ActionEvent e)
    {
    t.interrupt();
    t = null;
    }
    });
    panel.add(end);
    getContentPane().add(panel);
    }
    private WaitAndNotifyThread t,t1;
    }
    class WaitAndNotifyThread extends Thread
    {
    public static Object ob=new Object();
    public WaitAndNotifyThread(WaitAndNotifyJFrame f)
    {
    control = f;
    isWait = false;
    count = 0;
    }
    public  void run()
    {
    try
    {
    synchronized(ob){
    while(true)
    {
    System.out.println(Thread.currentThread().getName()+"Count:"+count++);
    sleep(1000);

    if(isWait){
        System.out.println(Thread.currentThread().getName()+"wait");
        ob.notify();
    ob.wait();
    }
    }
    }
    }catch(Exception e){}
    }
    public void n()
    {
    notify();
    }
    public boolean isWait;
    private WaitAndNotifyJFrame control;
    private int count;
    }