由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 实现一个 thread-safe blocking queue这题怎么写啊?L家的常考
相关主题
这个Java blocking queue实现是不是有问题?Java编程讨论:LinkedIn的H2O
thread safe blocking queue问题chess game的OOD
DFS比BFS好在哪?leetcode valid number
service now 卧佛和面筋leetcode上Symmetric Tree这道题怎么用iterative的方法做?
Java Blocking Queue问题求问两题思路
[合集] M$ interview questionJava concurrency 面试题
[合集] 【讨论】两道非常难的Google面试题有没有Blocking Bounded Queue 用Pthread实现的Sample Code?
也问两个算法题word search BST 解法,大测试超时,请大家指点迷津
相关话题的讨论汇总
话题: isempty话题: boolean话题: notifyall话题: isfull
进入JobHunting版参与讨论
1 (共1页)
y*****e
发帖数: 712
1
好怕这种thread safe的题,不知有人愿意分享一下代码吗?在网上找了几个代码,因
为半懂不懂的,也分不出个好坏高低来,还是觉得板上牛人多。
http://codereview.stackexchange.com/questions/7002/java-blockin
y*****e
发帖数: 712
2
public class BQueue {
private Queue q = new LinkedList();
private int limit;
public BQueue(int limit) {
this.limit = limit;
}
public synchronized void put (T t) throws InterruptedException {
while (isFull()) {
wait();
}
boolean e = isEmpty();
q.add(t);
if (e)
notifyAll();
}
public synchronized T get () throws InterruptedException {
while (isEmpty()) {
wait();
}
boolean f = isFull();
T t = q.poll();
if (f)
notifyAll();
return t;
}
private boolean isEmpty() {
return q.size() == 0;
}
private boolean isFull() {
return q.size() == limit;
}
}
这个implemetation里为啥boolean e = isEmpty()要在q.add(t)前面?如果empty,但
add之后不就不再empty了吗?这时候再notifyAll还有效吗?
e***a
发帖数: 1661
3
there is some math theory in it
f******n
发帖数: 198
4
都让你写这个了,怎么可能还让你用synchronized,肯定会让你直接用lock看你知不知
道什么时候可以context switch,什么时候不可以。



【在 y*****e 的大作中提到】
: public class BQueue {
: private Queue q = new LinkedList();
: private int limit;
: public BQueue(int limit) {
: this.limit = limit;
: }
: public synchronized void put (T t) throws InterruptedException {
: while (isFull()) {
: wait();
: }

y*****e
发帖数: 712
5
T_T
我再去找找用lock怎么写。。。

【在 f******n 的大作中提到】
: 都让你写这个了,怎么可能还让你用synchronized,肯定会让你直接用lock看你知不知
: 道什么时候可以context switch,什么时候不可以。
:
:

c******f
发帖数: 243
6
可以用这个吗
projectlombok dot org/features/Synchronized dot html
D**C
发帖数: 6754
7
这都没法编译,wait/notify都要建立在一个object上面。
当然略加修改就可以了,我不觉得还需要check empty/full,只需要notify the
waiting process就可以了,没有waiting的,notifyall没有负效果

【在 y*****e 的大作中提到】
: public class BQueue {
: private Queue q = new LinkedList();
: private int limit;
: public BQueue(int limit) {
: this.limit = limit;
: }
: public synchronized void put (T t) throws InterruptedException {
: while (isFull()) {
: wait();
: }

b**********5
发帖数: 7881
8
小姐啊, 有个网站叫grepcode, 你自己去看看java的blockingqueue的source code不
就知道了?

【在 y*****e 的大作中提到】
: 好怕这种thread safe的题,不知有人愿意分享一下代码吗?在网上找了几个代码,因
: 为半懂不懂的,也分不出个好坏高低来,还是觉得板上牛人多。
: http://codereview.stackexchange.com/questions/7002/java-blockin

1 (共1页)
进入JobHunting版参与讨论
相关主题
word search BST 解法,大测试超时,请大家指点迷津Java Blocking Queue问题
leetcode 新题 Course Schedule用BFS怎么做?[合集] M$ interview question
我恨iPhone@Facebook电面[合集] 【讨论】两道非常难的Google面试题
分享下G家第一个phone interview的题目也问两个算法题
这个Java blocking queue实现是不是有问题?Java编程讨论:LinkedIn的H2O
thread safe blocking queue问题chess game的OOD
DFS比BFS好在哪?leetcode valid number
service now 卧佛和面筋leetcode上Symmetric Tree这道题怎么用iterative的方法做?
相关话题的讨论汇总
话题: isempty话题: boolean话题: notifyall话题: isfull