java lang报错_java报错java.lang.IllegalMonitorStateException

先把错误的代码放出来,很简单的错误!哈哈哈

import java.util.concurrent.CountDownLatch;

import java.util.concurrent.LinkedBlockingDeque;

import java.util.concurrent.ThreadPoolExecutor;

import java.util.concurrent.TimeUnit;

import java.util.concurrent.locks.ReentrantLock;

public class RedisTest {

private static Integer inventory = 1001;

private static final int NUM = 1000;

private static LinkedBlockingDeque linkedBlockingDeque = new LinkedBlockingDeque();

static ReentrantLock reentrantLock = new ReentrantLock();

public static void main(String[] args) {

ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(inventory,inventory,10L,TimeUnit.SECONDS,linkedBlockingDeque);

final CountDownLatch countDownLatch = new CountDownLatch(NUM);

long start = System.currentTimeMillis();

for (int i = 0;i <= NUM;i++){

threadPoolExecutor.execute(new Runnable() {

@Override

public void run() {

// reentrantLock.lock();

inventory--;

reentrantLock.unlock();

System.out.println("线程执行"+Thread.currentThread().getName());

countDownLatch.countDown();

}

});

}

threadPoolExecutor.shutdown();

try {

countDownLatch.await();

}catch (InterruptedException e){

e.printStackTrace();

}

long end = System.currentTimeMillis();

System.out.println("执行线程数"+NUM +" 总耗时:"+(end -start)+" 库存数为"+inventory);

}

}

出现java.lang.IllegalMonitorStateException错误,由以下情况导致:

1>当前线程不含有当前对象的锁资源的时候,调用obj.wait()方法;

2>当前线程不含有当前对象的锁资源的时候,调用obj.notify()方法。

3>当前线程不含有当前对象的锁资源的时候,调用obj.notifyAll()方法。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值