• 并发 错误 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方法前添加synchronizedkeyword,这样调用wait()方法就不会出错了。

    2、在t.notify()方法调用时使用同步上下文。改为例如以下:

    synchronized (t) {
    t.notify();
    }

    再次运行就正常了。

  • 相关阅读:
    远程登陆服务——SSH
    A web-based 3D modeling framework for a runner-gate design( 一种基于web的三维建模框架)
    Vue+Element+Echarts+Springboot+微信小程序开发(肿瘤医学项目)
    基于拖放布局的 Twitter Bootstrap 网站生成器
    Ubuntu下git使用华为云/gitee/github
    在 Ubuntu 18.04 上安装 Postman
    在Ubuntu 18.04上安装Git与入门教程
    转载:渲染层和逻辑层
    Ubuntu系统中安装Neo4j
    NosqlBooster连接数据库失败connect ECONNREFUSED 127.0.0.1:27017——mongodb连接失败
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10020496.html
Copyright © 2020-2023  润新知