线程:Wait()和notify()

时间:2013-02-11 12:23:16

标签: java multithreading

执行下面的代码并抛出IllegalMonitorStateException  例外。我收到的错误是:

java.lang.IllegalMonitorStateException
    at java.lang.Object.notify(Native Method)
    at com.blt.ThreadExample.main(ThreadExample.java:21)

我是多线程的新手,我想在代码中使用wait()notify()

package com.blt;



public class ThreadExample implements Runnable {
    public static void main(String args[])
    {


        System.out.println("A");
        Thread T = new Thread(new ThreadExample());
        Thread T1 = new Thread(new ThreadExample());

        System.out.println("B");
        try
        {
        T.setName("thread 1");
        T.start();
        T1.setName("thread 2");
        System.out.println("C");
        T.notify();

        System.out.println("D");
        T1.start();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }


        public  void run()
{


            synchronized(ThreadExample.class)
            {


        for(int i=0; i<5; i++)
    {

        try
        {
            Thread.currentThread().wait(400);
         System.out.println("Inside run=>"+Thread.currentThread().getName());
         Thread.currentThread().sleep(2000);




        }
         catch(Exception e)
        {
            e.printStackTrace();
        }
      }  
}
}
}

3 个答案:

答案 0 :(得分:4)

正如the javadoc中所述,您需要使用对象作为监视器,在同步块内,才能在该对象上调用notifywait

Thread.currentThread()很难跟踪,所以我建议你使用另一个对象。例如:

public class ThreadExample implements Runnable {

    private static final Object lock = new Object();

    public static void main(String args[]) {
        System.out.println("A");
        Thread T = new Thread(new ThreadExample());
        Thread T1 = new Thread(new ThreadExample());

        System.out.println("B");
        try {
            T.setName("thread 1");
            T.start();
            T1.setName("thread 2");
            System.out.println("C");
            synchronized(lock) {
                lock.notify();
            }
            System.out.println("D");
            T1.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void run() {
        synchronized (lock) {
            for (int i = 0; i < 5; i++) {
                try {
                    lock.wait(400);
                    System.out.println("Inside run=>" + Thread.currentThread().getName());
                    Thread.currentThread().sleep(2000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

请注意您的代码中还有其他一些问题,尤其是:

  • 你应该总是在循环中调用wait(阅读javadoc以获取更多详细信息)
  • sleep是一种静态方法,无需使用Thread.currentThread().sleep(2000); - sleep(2000);也会这样做。

答案 1 :(得分:2)

这些方法必须包含在同步块中。阅读java tutorial中的对象锁定。

换句话说:

//thread 1
synchronized (commonObj) {
  commonObj.wait();
}

//thread 2:
synchronized (commonObj) {
  commonObj.notify();
}

同样的问题:How to use wait and notify in Java?

答案 2 :(得分:0)

试试这个......如果你有任何声音,请告诉我。

package com.blt;

public class ThreadExample implements Runnable {
    public static void main(String args[]) {
        Thread T1 = new Thread(new ThreadExample());
        Thread T2 = new Thread(new ThreadExample());
        try {
            T1.setName("thread 1");
            T1.start();
            T2.setName("thread 2");
            T2.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void run() {

        synchronized (ThreadExample.class) {
            for (int i = 0; i < 5; i++) {
                try {
                    ThreadExample.class.notify();
                    System.out.println("Thread going to wait state::"+ Thread.currentThread().getName());
                    ThreadExample.class.wait(400);
                    System.out.println("Thread notified is::"+ Thread.currentThread().getName());
                    System.out.println("Thread going to sleep state::"+ Thread.currentThread().getName());
                    Thread.sleep(2000);
                    System.out.println("==========");

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}