由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - thread-safe blockingqueue
相关主题
这个Java blocking queue实现是不是有问题?如何用JAVA中的circular array of queue 解决Josephus problem? (转载)
再问一个blockingqueue的问题问个题:get max value from Queue, with O(1)?
面试题一道很难的面试题
请教一个系统设计问题 (转载)Two programming questions...
如何实现binary tree的从下到上的分层打印?A家电面
share 面试题LinkedIn 电面
这个用stack实现queuethread safe blocking queue问题
求救: 打印binary treeJava Blocking Queue问题
相关话题的讨论汇总
话题: queue话题: public话题: lock话题: capacity
进入JobHunting版参与讨论
1 (共1页)
n****e
发帖数: 678
1
在网上和版上搜了半天,implement一个thread-safe blockingqueue. 大家看看有没有
问题:
import java.utils.LinkedList;
public class BlockingQueue {
private int capactiy;
private LinkedList elements;

public BlockingQueue(int capacity) {
if (capacity <=0 ) {
throw new exception();
}

this.capacity = capacity;
this.elements = new LinkedList ();
}

public synchronized void put(T t) {
while(elements.size() == capacity) {
wait();
}

boolean flag = false;

if (elements.size() == 0) {
flag = true;
}

elements.add(t);
if (flag) {
notifyAll();
}


}

public synchronized T take(T t) {
while(elements.size() == 0) {
wait();
}

boolean flag = false;

if (elements.size() == capacity) {
flag = true;
}

T t = elements.remove(0);

if (flag) {
notifyAll();
}

return t;
}
}
m**p
发帖数: 189
2
呼唤C++的!
n****e
发帖数: 678
3
感觉还是用java容易点
m**p
发帖数: 189
p*****2
发帖数: 21240
5
用go channel就可以了。
n****e
发帖数: 678
6
看过这个网页的implmentation,感觉codes 有点问题。 比如,还没有add element 就
先notifyAll了。

【在 m**p 的大作中提到】
: http://tutorials.jenkov.com/java-util-concurrent/blockingqueue.
m**p
发帖数: 189
7
这个怎么样?
#include
#include
#include
#include
template
class Queue
{
public:
T pop()
{
std::unique_lock mlock(mutex_);
while (queue_.empty())
{
cond_.wait(mlock);
}
auto item = queue_.front();
queue_.pop();
return item;
}
void pop(T& item)
{
std::unique_lock mlock(mutex_);
while (queue_.empty())
{
cond_.wait(mlock);
}
item = queue_.front();
queue_.pop();
}
void push(const T& item)
{
std::unique_lock mlock(mutex_);
queue_.push(item);
mlock.unlock();
cond_.notify_one();
}
void push(T&& item)
{
std::unique_lock mlock(mutex_);
queue_.push(std::move(item));
mlock.unlock();
cond_.notify_one();
}
private:
std::queue queue_;
std::mutex mutex_;
std::condition_variable cond_;
};
x****o
发帖数: 29677
8
1.5以上有API直接用了吧
n****e
发帖数: 678
9
面试题要考,没办法啊

【在 x****o 的大作中提到】
: 1.5以上有API直接用了吧
d***n
发帖数: 832
10
贴个C#版本
public class BlockingQueue
{
private Queue m_queue = new Queue();
private int m_waitingConsumers = 0;
public int Count
{
get
{
lock (m_queue)
return m_queue.Count;
}
}
public void Enqueue(T item)
{
lock (m_queue)
{
m_queue.Enqueue(item);
if (m_waitingConsumers > 0)
Monitor.Pulse(m_queue);
}
}
public T Dequeue()
{
lock (m_queue)
{
while (m_queue.Count == 0)
{
m_waitingConsumers++;
try
{
Monitor.Wait(m_queue);
}
finally
{
m_waitingConsumers--;
}
}
return m_queue.Dequeue();
}
}
}
相关主题
share 面试题如何用JAVA中的circular array of queue 解决Josephus problem? (转载)
这个用stack实现queue问个题:get max value from Queue, with O(1)?
求救: 打印binary tree一道很难的面试题
进入JobHunting版参与讨论
d***n
发帖数: 832
11
前面有一贴用到Mutex效率不高
因为Mutex是kernel object
每次用到都到都有user mode和kernel mode的切换
我贴的是user mode的
工作中都可以拿来用的
d***n
发帖数: 832
12
再来个更先进的, blocking bounded queue
public class BlockingBoundedQueue
{
private Queue m_queue = new Queue();
private int m_capacity;
private object m_fullEvent = new object();
private int m_fullWaiters = 0;
private object m_emptyEvent = new object();
private int m_emptyWaiters = 0;
public BlockingBoundedQueue(int capacity)
{
m_capacity = capacity;
}
public int Count
{
get
{
lock (m_queue)
return m_queue.Count;
}
}
public void Clear()
{
lock (m_queue)
m_queue.Clear();
}
public bool Contains(T item)
{
lock (m_queue)
return m_queue.Contains(item);
}
public void Enqueue(T item)
{
lock (m_queue)
{
if (m_queue.Count == m_capacity)
{
m_fullWaiters++;
try
{
lock (m_fullEvent)
{
Monitor.Exit(m_queue);
Monitor.Wait(m_fullEvent);
Monitor.Enter(m_queue);
}
}
finally
{
m_fullWaiters--;
}
}
m_queue.Enqueue(item);
}
if (m_emptyWaiters > 0)
{
lock (m_emptyEvent)
Monitor.Pulse(m_emptyEvent);
}
}
public T Dequeue()
{
T item;
lock (m_queue)
{
while (m_queue.Count == 0)
{
m_emptyWaiters++;
try
{
lock (m_emptyEvent)
{
Monitor.Exit(m_queue);
Monitor.Wait(m_emptyEvent);
Monitor.Enter(m_queue);
}
}
finally
{
m_emptyWaiters--;
}
}
item = m_queue.Dequeue();
}
if (m_fullWaiters > 0)
lock (m_fullEvent)
Monitor.Pulse(m_fullEvent);
return item;
}
public T Peek()
{
lock (m_queue)
return m_queue.Peek();
}
}
J****3
发帖数: 427
13
不错 学习学习

【在 m**p 的大作中提到】
: 这个怎么样?
: #include
: #include
: #include
: #include
: template
: class Queue
: {
: public:
: T pop()

b**********5
发帖数: 7881
14
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/

【在 n****e 的大作中提到】
: 在网上和版上搜了半天,implement一个thread-safe blockingqueue. 大家看看有没有
: 问题:
: import java.utils.LinkedList;
: public class BlockingQueue {
: private int capactiy;
: private LinkedList elements;
:
: public BlockingQueue(int capacity) {
: if (capacity <=0 ) {
: throw new exception();

n****e
发帖数: 678
15
多谢!
之前也看过这个,感觉这个codes 太多了,现场写怕容易写错。
这个应该是最正确的版本吧。

【在 b**********5 的大作中提到】
: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/
n****e
发帖数: 678
16
多谢,,不会C#

【在 d***n 的大作中提到】
: 贴个C#版本
: public class BlockingQueue
: {
: private Queue m_queue = new Queue();
: private int m_waitingConsumers = 0;
: public int Count
: {
: get
: {
: lock (m_queue)

t****t
发帖数: 6806
17
求个C++的mutex-free queue, 单读单写.

【在 J****3 的大作中提到】
: 不错 学习学习
d***n
发帖数: 832
n****e
发帖数: 678
19
这个好!
准备面试问道这题,就照着这个写了。
多谢大牛!

【在 d***n 的大作中提到】
: java正式文档有很好的例子code
: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/l

s********u
发帖数: 1109
20
C++11:
template
class BlockingQueue{
private:
queue _queue;
mutex _mutex;
condition_variable _cond;
public:
void push( const T& item){
unique_lock locker(_mutex);
_queue.push(item);
locker.unlock();
_cond.notify_one();
}


T pop(){
unique_lock locker(_mutex);
_cond.wait(locker, [=](){ return !_queue.empty() ;} ); //lambda
function, capture by value
T item = _queue.front();
_queue.pop();
return item;
}
};
n****e
发帖数: 678
21
多谢!
我也一直想学习如何用C++来写BlockingQueue的

【在 s********u 的大作中提到】
: C++11:
: template
: class BlockingQueue{
: private:
: queue _queue;
: mutex _mutex;
: condition_variable _cond;
: public:
: void push( const T& item){
: unique_lock locker(_mutex);

1 (共1页)
进入JobHunting版参与讨论
相关主题
Java Blocking Queue问题如何实现binary tree的从下到上的分层打印?
爆个L家面静吧share 面试题
F家电面这个用stack实现queue
昨天被面试的问题求救: 打印binary tree
这个Java blocking queue实现是不是有问题?如何用JAVA中的circular array of queue 解决Josephus problem? (转载)
再问一个blockingqueue的问题问个题:get max value from Queue, with O(1)?
面试题一道很难的面试题
请教一个系统设计问题 (转载)Two programming questions...
相关话题的讨论汇总
话题: queue话题: public话题: lock话题: capacity