Код: Выделить всё
package com.example.springlearn1.demo;
import java.util.LinkedList;
import java.util.Random;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
class Producer implements Runnable {
Random rand = new Random();
ReentrantLock lk;
Condition cp;
Condition cc;
LinkedList l;
int cap;
public Producer(LinkedList l, ReentrantLock lk, int cap, Condition cp, Condition cc) {
this.l = l;
this.lk = lk;
this.cap = cap;
this.cp = cp;
this.cc = cc;
}
@Override
public void run() {
while(true) {
try {
Thread.sleep(500);
lk.lock();
System.out.println("In producer");
while(l.size() == cap) {
cp.await();
}
cc.signalAll();
if(Thread.currentThread().isInterrupted()) {
System.out.println("Producer Aborting now...\n");
break;
}
l.addLast(rand.nextInt(4000));
} catch (InterruptedException e) {
if(Thread.currentThread().isInterrupted()) {
System.out.println("Producer Thread is interrupted.");
Thread.currentThread().interrupt();
}
//throw new RuntimeException(e);
} finally {
lk.unlock();
}
}
}
}
class Consumer implements Runnable {
ReentrantLock lk;
LinkedList l;
Condition cp;
Condition cc;
int cap;
public Consumer(LinkedList l, ReentrantLock lk, int cap, Condition cp, Condition cc) {
this.l = l;
this.lk = lk;
this.cap = cap;
this.cp = cp;
this.cc = cc;
}
@Override
public void run() {
while(true) {
try {
Thread.sleep(1000);
lk.lock();
System.out.println("In consumer");
while(l.size() == 0) {
cc.await();
}
cp.signalAll();
if(Thread.currentThread().isInterrupted()) {
System.out.println("Aborting now...\n");
break;
}
System.out.println("Element is "+l.removeFirst());
} catch (InterruptedException e) {
if(Thread.currentThread().isInterrupted()) {
System.out.println("Consumer Thread is interrupted.");
Thread.currentThread().interrupt();
}
//throw new RuntimeException(e);
} finally {
lk.unlock();
}
}
}
}
public class ProducerConsumer {
public static void main(String[] args) throws InterruptedException {
LinkedList queue = new LinkedList();
ReentrantLock lock = new ReentrantLock();
Condition cp = lock.newCondition();
Condition cc = lock.newCondition();
int capacity = 10;
Thread producerThread = new Thread(new Producer(queue, lock, capacity, cp,cc));
Thread consumerThread = new Thread(new Consumer(queue, lock, capacity,cp,cc));
consumerThread.start();
producerThread.start();
System.out.println("Started both threads");
try {
Thread.sleep(60000); // Run for 1 minute
} catch (InterruptedException e) {
System.out.println("Threads interrupted, exiting.");
}
producerThread.interrupt();
consumerThread.interrupt();
producerThread.join();
consumerThread.join();
}
}
Код: Выделить всё
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
at java.base/java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:175)
at java.base/java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1007)
at java.base/java.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:494)
at com.example.springlearn1.demo.Consumer.run(ProducerConsumer.java:89)
at java.base/java.lang.Thread.run(Thread.java:842)
In producer
Код: Выделить всё
lk.unlock
Я не могу понять, почему это состояние возникает через некоторое время. Предполагается, что он будет работать в течение 1 минуты, а затем завершится.
Подробнее здесь: https://stackoverflow.com/questions/790 ... gram-not-t