并发 错误 java.lang.IllegalMonitorStateException: current thread not owner 分析

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(); 

再次运行就正常了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值