42 lines
952 B
Java
42 lines
952 B
Java
package concurrent.model;
|
|
|
|
import lombok.AllArgsConstructor;
|
|
import lombok.SneakyThrows;
|
|
|
|
import java.util.Queue;
|
|
|
|
@AllArgsConstructor
|
|
public class WaitConsumer extends Thread {
|
|
private final Object LOCK;
|
|
private final Queue<Integer> buffer;
|
|
|
|
@SneakyThrows
|
|
@Override
|
|
public void run() {
|
|
work();
|
|
}
|
|
|
|
public void work() throws InterruptedException {
|
|
while (true) {
|
|
try {
|
|
Thread.sleep(1000);
|
|
}
|
|
catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
synchronized (LOCK) {
|
|
while (buffer.isEmpty()) {
|
|
LOCK.wait();
|
|
}
|
|
Integer poll = buffer.poll();
|
|
System.out.println(Thread.currentThread()
|
|
.getName() + "消费者消费" + poll);
|
|
LOCK.notifyAll();
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|
|
}
|