java基础 并发测试代码

This commit is contained in:
wyl
2024-07-01 00:28:32 +08:00
parent da0e11337c
commit 30d826bb8e
23 changed files with 1258 additions and 25 deletions
@@ -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();
}
}
}
}