执行下面的代码并抛出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();
}
}
}
}
}发布于 2013-02-11 20:54:26
试试这个..。如果你有doudt就告诉我。
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-11 20:25:33
正如the javadoc中所解释的,您需要在使用对象作为监视器的synchronized块中,才能对该对象调用notify或wait。
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();
}
}
}
}
}请注意,您的代码中还有其他几个问题,特别是:
details)
sleep是一种静态方法,不需要使用Thread.currentThread().sleep(2000); - sleep(2000);也会做同样的事情。发布于 2013-02-11 20:26:40
这些方法必须包含在synchronized块中。阅读有关java tutorial中的对象锁的内容。
换句话说:
//thread 1
synchronized (commonObj) {
commonObj.wait();
}
//thread 2:
synchronized (commonObj) {
commonObj.notify();
}https://stackoverflow.com/questions/14811803
复制相似问题