1

执行以下代码并抛出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();
        }
      }  
}
}
}
4

3 回答 3

4

正如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);也会这样做。
于 2013-02-11T12:25:32.790 回答
2

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

换句话说:

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

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

同样的问题:如何在 Java 中使用等待和通知?

于 2013-02-11T12:26:39.520 回答
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();
                }
            }
        }
    }
}
于 2013-02-11T12:54:26.060 回答