public class ThreadTest implements Callable<String> {
public String call() throws Exception {
// TODO Auto-generated method stub
wait(10000);
return "hello";
}
}
调用代码:
public static void main(String[] args) {
System.out.println("开始启动线程"+Thread.currentThread().getName());
ExecutorService exe = Executors.newCachedThreadPool();
ThreadTest t= new ThreadTest();
Future<String> f = exe.submit(t);
Future<String> f2 = exe.submit(t);
try {
t.notify();
System.out.println(f.get());
System.out.println(f2.get());
System.out.println("主线程"+Thread.currentThread().getName());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
在学习并发是遇到了如下错误
java.lang.IllegalMonitorStateException: current thread not owner
我的代码如下:
通过查找资料找到原因即解决之道。
《java编程思想》第四版一书中有描述到:“线程操作的wait()、notify()、notifyAll()方法只能在同步控制方法或同步控制块内调用。如果在非同步控制方法或控制块里调用,程序能通过编译,但运行的时候,将得到 IllegalMonitorStateException 异常,并伴随着一些含糊信息,比如 ‘当前线程不是拥有者’。其实异常的含义是 调用wait()、notify()、notifyAll()的任务在调用这些方法前必须 ‘拥有’(获取)对象的锁。”
JAVA JDK API文档中也有描述如下:
wait()、notify()、notifyAll()法只应由作为此对象监视器的所有者的线程来调用。通过以下三种方法之一,线程可以成为此对象监视器的所有者:
- 通过执行此对象的同步 (Sychronized) 实例方法。
- 通过执行在此对象上进行同步的
synchronized
语句的正文。 - 对于
Class
类型的对象,可以通过执行该类的同步静态方法。
根据以上分析,我的代码修改如下:
1、call方法前增加synchronized关键字,这样调用wait()方法就不会出错了。
2、在t.notify()方法调用时使用同步上下文,改为如下:
synchronized (t) {
t.notify();
}
再次运行就正常了。