boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - thread safe blocking queue问题
相关主题
这个Java blocking queue实现是不是有问题?
再问一个blockingqueue的问题
Java Blocking Queue问题
实现一个 thread-safe blocking queue这题怎么写啊?L家的常考
请教一个系统设计问题 (转载)
如何实现binary tree的从下到上的分层打印?
share 面试题
这个用stack实现queue
求救: 打印binary tree
如何用JAVA中的circular array of queue 解决Josephus problem? (转载)
相关话题的讨论汇总
话题: object话题: throws话题: while
进入JobHunting版参与讨论
1 (共1页)
g****v
发帖数: 971
1
发现这个还是挺常见的,这是网上的标准解法,希望可以对大家有帮助。
唯一的问题是为什么2个方法里面都用while来判断, 而不是用if?
---------------------------------------------
public synchronized void enqueue(Object item)
throws InterruptedException {
while(this.queue.size() == this.limit) {
wait();
}
if(this.queue.size() == 0) {
notifyAll();
}
this.queue.add(item);
}
public synchronized Object dequeue()
throws InterruptedException{
while(this.queue.size() == 0){
wait();
}
if(this.queue.size() == this.limit){
notifyAll();
}
return this.queue.remove(0);
}
-------------------------------------------
b**********5
发帖数: 7881
2
这是标准解法??!! 老兄, 你自己去看看java ArrayBlockingQueue的
implementation
还他妈的synchronized。 lock 和condition!

【在 g****v 的大作中提到】
: 发现这个还是挺常见的,这是网上的标准解法,希望可以对大家有帮助。
: 唯一的问题是为什么2个方法里面都用while来判断, 而不是用if?
: ---------------------------------------------
: public synchronized void enqueue(Object item)
: throws InterruptedException {
: while(this.queue.size() == this.limit) {
: wait();
: }
: if(this.queue.size() == 0) {
: notifyAll();

g****v
发帖数: 971
3
难道不是么?你发个我看看

【在 b**********5 的大作中提到】
: 这是标准解法??!! 老兄, 你自己去看看java ArrayBlockingQueue的
: implementation
: 还他妈的synchronized。 lock 和condition!

b**********5
发帖数: 7881
4
你他妈的不会google arrayblockingqueue的implementation??!!!!

【在 g****v 的大作中提到】
: 难道不是么?你发个我看看
g****v
发帖数: 971
5
你没问题吧,找工作火气这么大,去flushing放松下

【在 b**********5 的大作中提到】
: 你他妈的不会google arrayblockingqueue的implementation??!!!!
b**********5
发帖数: 7881
6
你才傻逼火气大呢!!

【在 g****v 的大作中提到】
: 你没问题吧,找工作火气这么大,去flushing放松下
g****v
发帖数: 971
g****v
发帖数: 971
D***n
发帖数: 149
9
Use ReentranceLock , a internal array and two conditions.
IMO, i think implementing ( put take offer poll ) methods is enough .
g****v
发帖数: 971
10
用synchronized不行么

【在 D***n 的大作中提到】
: Use ReentranceLock , a internal array and two conditions.
: IMO, i think implementing ( put take offer poll ) methods is enough .

b**********5
发帖数: 7881
11
copied from Java Documentation!!!
class BoundedBuffer {
final Lock lock = new ReentrantLock();
final Condition notFull = lock.newCondition();
final Condition notEmpty = lock.newCondition();
final Object[] items = new Object[100];
int putptr, takeptr, count;
public void put(Object x) throws InterruptedException {
lock.lock();
try {
while (count == items.length)
notFull.await();
items[putptr] = x;
if (++putptr == items.length) putptr = 0;
++count;
notEmpty.signal();
} finally {
lock.unlock();
}
}
public Object take() throws InterruptedException {
lock.lock();
try {
while (count == 0)
notEmpty.await();
Object x = items[takeptr];
if (++takeptr == items.length) takeptr = 0;
--count;
notFull.signal();
return x;
} finally {
lock.unlock();
}
}
}

【在 D***n 的大作中提到】
: Use ReentranceLock , a internal array and two conditions.
: IMO, i think implementing ( put take offer poll ) methods is enough .

D***n
发帖数: 149
12
http://stackoverflow.com/questions/3940164/java-waiting-on-sync

【在 g****v 的大作中提到】
: 用synchronized不行么
1 (共1页)
进入JobHunting版参与讨论
相关主题
如何用JAVA中的circular array of queue 解决Josephus problem? (转载)
问个题:get max value from Queue, with O(1)?
面试题
一道很难的面试题
Two programming questions...
A家电面
thread-safe blockingqueue
请教一个java Synchronized Blocks和Lock的问题
昨天被面试的问题
一道面试题
相关话题的讨论汇总
话题: object话题: throws话题: while