阻塞队列 线程阻塞队列 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 虽然你没有贴代码出来,但是我建议你去掉 synchronised (queue)同时用synchronised (queue) { ... queue.take(); } 和 synchronized (queue) {... queue.put(..)},那是妥妥的死锁 当你take的时候,队列里没有值它就会锁死。public E take() throws InterruptedException { final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { try { while (count == 0) notEmpty.await(); } catch (InterruptedException ie) { notEmpty.signal(); // propagate to non-interrupted thread throw ie; } E x = extract(); return x; } finally { lock.unlock(); } }当你put的时候,队列里已经满它就会锁死。public void put(E e) throws InterruptedException { if (e == null) throw new NullPointerException(); final E[] items = this.items; final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { try { while (count == items.length) notFull.await(); } catch (InterruptedException ie) { notFull.signal(); // propagate to non-interrupted thread throw ie; } insert(e); } finally { lock.unlock(); } } 将put改成 poll(long timeout, TimeUnit unit)方法试试 notEmpty.await();notEmpty.signal() 把容量设成最大的BlockingQueue<String> blockQueue = new ArrayBlockingQueue<String>(Integer.MAX_VALUE ); 5#说的很对,我也在JDK的源码中看到了这个。但我就想用一个队列,实现一个线程take一下,另外一个线程put一下,相当于是生产-消费者互相可以通信,代码中我没有用任何synchronized锁,难道用一个队列不行吗?我知道用两个队列可以实现 不需要你用任何synchronized,因为是堵塞队列当队列中没有数据的时候take会一直堵塞,直到有线程往队列中put,当队列已经满了的时候put会一直堵塞,直到有线程从队列中take.只能介绍这么多的原理了。希望能帮助到我,这是我写socket,多线程编程的一点经验。 还有一种方式就是自己实现堵塞队伍,当队列中没有值是wait(),当队列中有值是再,调用 notify.或使用lock锁。 1.在A库创建B库dblink。2.在A库中可以直接引用B库的表进行查询操作。3.再根据查询做相关的插入。具体dblink的创建请百度 对于押宝游戏出个题目 C++代码更改java代码--- for循环 请教关于输入的问题 又有问题请教高人了,多谢了! [求助]关于File路径的问题 hibernate 的load()方法在什么地方使用? 急急急!!!新手求教,C/S图书管理系统中一些问题 请大家帮帮我 菜鸟提问:package是否是多余的? 今天写了一个计算器,出现了问题,求大侠帮忙解决,先谢谢了 自己无聊根据视频写了一个抢票的的程序,但是出了点问题
public E take() throws InterruptedException {
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
try {
while (count == 0)
notEmpty.await();
} catch (InterruptedException ie) {
notEmpty.signal(); // propagate to non-interrupted thread
throw ie;
}
E x = extract();
return x;
} finally {
lock.unlock();
}
}
当你put的时候,队列里已经满它就会锁死。public void put(E e) throws InterruptedException {
if (e == null) throw new NullPointerException();
final E[] items = this.items;
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
try {
while (count == items.length)
notFull.await();
} catch (InterruptedException ie) {
notFull.signal(); // propagate to non-interrupted thread
throw ie;
}
insert(e);
} finally {
lock.unlock();
}
}
notEmpty.signal()
BlockingQueue<String> blockQueue = new ArrayBlockingQueue<String>(Integer.MAX_VALUE );
当队列中没有数据的时候take会一直堵塞,直到有线程往队列中put,
当队列已经满了的时候put会一直堵塞,直到有线程从队列中take.
只能介绍这么多的原理了。希望能帮助到我,这是我写socket,多线程编程的一点经验。
当队列中没有值是wait(),当队列中有值是再,调用 notify.或使用lock锁。
2.在A库中可以直接引用B库的表进行查询操作。
3.再根据查询做相关的插入。具体dblink的创建请百度