weixin_43861158
weixin_43861158
采纳率33.3%
2019-06-28 08:37

大佬们,为什么我的代码总是报如下异常??

已采纳

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ProducerConsumerDemo {

public static void main(String[] args) {
    Resource res = new Resource();
    Producer pro = new Producer(res);
    Consumer con = new Consumer(res);
    Thread t1 = new Thread(pro);
    Thread t2 = new Thread(con);
    Thread t3 = new Thread(pro);
    Thread t4 = new Thread(con);

    t1.start();
    t2.start();
    t3.start();
    t4.start();


}

}
class Resource{
private String name;
private boolean flag = false;
private int count = 0;
Lock lock = new ReentrantLock();
Condition condition_pro = lock.newCondition();
Condition condition_con = lock.newCondition();
public /*synchronized*/ void set(String name) throws InterruptedException{
lock.lock();
try{
while(flag){
condition_pro.await();
}
this.name = name+count++;

        System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);
        flag = true;
        condition_con.signal();
    }finally{
        lock.unlock();
    }
}
public /*synchronized*/ void out() throws InterruptedException{
    try{
        while(!flag){
            condition_con.await();
        }
        System.out.println(Thread.currentThread().getName()+"...消费者..."+name);
        flag = false;
        condition_pro.signal();;
    }finally{
        lock.unlock();
    }
}

}
class Producer implements Runnable{

Resource res;
Producer(Resource res){
    this.res = res;
}

public void run(){
    while(true){
        try {
            res.set("商品");
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

}
class Consumer implements Runnable{

Resource res;
Consumer(Resource res){
    this.res = res;
}

public void run(){
    while(true){
        try {
            res.out();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

}

Exception in thread "Thread-1" Thread-0...生产者...商品0
Thread-3...消费者...商品0
Exception in thread "Thread-3" java.lang.IllegalMonitorStateException
at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(Unknown Source)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(Unknown Source)
at java.util.concurrent.locks.ReentrantLock.unlock(Unknown Source)
at 生产者消费者.Resource.out(ProducerConsumerDemo.java:65)
at 生产者消费者.Consumer.run(ProducerConsumerDemo.java:98)
at java.lang.Thread.run(Unknown Source)
java.lang.IllegalMonitorStateException
at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(Unknown Source)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(Unknown Source)
at java.util.concurrent.locks.ReentrantLock.unlock(Unknown Source)
at 生产者消费者.Resource.out(ProducerConsumerDemo.java:65)
at 生产者消费者.Consumer.run(ProducerConsumerDemo.java:98)
at java.lang.Thread.run(Unknown Source)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • wojiushiwo945you 毕小宝 2年前

    lock 必须先执行 lock() 操作才能再 unlock ,Resource 的 out() 这里漏掉了:

    public /* synchronized */ void out() throws InterruptedException {
            lock.lock();
            try {
                while (!flag) {
                    condition_con.await();
                }
                System.out.println(Thread.currentThread().getName() + "...消费者..." + name);
                flag = false;
                condition_pro.signal();
                ;
            } finally {
                lock.unlock();
            }
        }
    

    编码建议:1、适当格式化下代码,便于阅读 2、无关的空的 ; 语句应该去掉。

    点赞 1 评论 复制链接分享

相关推荐