自定义线程池
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lhU7rkJd-1665716415837)(C:\Users\wu\AppData\Roaming\Typora\typora-user-images\image-20221010164225941.png)]](https://i-blog.csdnimg.cn/blog_migrate/3a567ed4dfd961fbf78a39314013ca3c.png)
阻塞队列
package ThreadPoolLearn;
@Slf4j(topic = "c.BlockingQueue")
public class BlockingQueue <T>{
private Deque<T> queue = new ArrayDeque<>();
private ReentrantLock lock = new ReentrantLock();
private Condition fullWaitSet = lock.newCondition();
private Condition emptyWaitSet = lock.newCondition();
private int capcity;
public BlockingQueue(int capcity) {
this.capcity = capcity;
}
public T poll(long timeout, TimeUnit unit) {
lock.lock();
try {
long nanos = unit.toNanos(timeout);
while (queue.isEmpty()) {
try {
if (nanos <= 0) {
return null;
}
nanos = emptyWaitSet.awaitNanos(nanos);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
T t = queue.removeFirst();
fullWaitSet.signal();
return t;
} finally {
lock.unlock();
}
}
public T take() {
lock.lock();
try {
while (queue.isEmpty()) {
try {
emptyWaitSet.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
T