java基础 并发测试代码
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user