概述
先把错误的代码放出来,很简单的错误!哈哈哈
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()方法。
最后
以上就是顺心枫叶为你收集整理的java lang报错_java报错java.lang.IllegalMonitorStateException的全部内容,希望文章能够帮你解决java lang报错_java报错java.lang.IllegalMonitorStateException所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复