由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - g 家面经
相关主题
F家电面如何实现binary tree的从下到上的分层打印?
碰到一道题share 面试题
一道电话题Is this a DP problem?
how to code this question of LinkedIn这个用stack实现queue
新鲜电面经求救: 打印binary tree
电面问题如何用JAVA中的circular array of queue 解决Josephus problem? (转载)
question 2: o(1) euque and dequeue?昨天面试遇到的两道题,编程语言和数据库设计相关
请教一个系统设计问题 (转载)问个题:get max value from Queue, with O(1)?
相关话题的讨论汇总
话题: producer话题: consumer话题: queue话题: public
进入JobHunting版参与讨论
1 (共1页)
h******e
发帖数: 52
1
producer / consumer 问题, 要求threadsafe, high throughput
class ProducerConsumer
{
ReaderWriterLock rwLock = new ReaderWriterLock();
AutoResetEvent FullEvent = new AutoResetEvent ();
AutoResetEvent EmptyEvent = new AutoResetEvent ();
public void Producer()
{
rwLock.AcquireWriterLock();
while(queue is full)
{
FullEvent.waitOne();
}
//add
if(Queue.count == 1)
EmptyEvent.set();
rwLock.ReleaseLock();

}
public void Consumer()
{
rwLock.AcquireReaderLock();
while(Queue is empty)
{
EmptyEvent.WaitOne();
}
dequueue();
if (Count == max – 1);
FullEvent.set();
rwLock.ReleaseReaderLock();
}
h**p
发帖数: 211
2
LZ这是电面?感觉不好搞啊
LZ提供下背景吧

【在 h******e 的大作中提到】
: producer / consumer 问题, 要求threadsafe, high throughput
: class ProducerConsumer
: {
: ReaderWriterLock rwLock = new ReaderWriterLock();
: AutoResetEvent FullEvent = new AutoResetEvent ();
: AutoResetEvent EmptyEvent = new AutoResetEvent ();
: public void Producer()
: {
: rwLock.AcquireWriterLock();
: while(queue is full)

N*****a
发帖数: 17
3
对啊,没怎么复习concurrency的题,感觉挺难的。
请问楼主,consumer为什么用reader lock,consumer里面有dequeue这个操作,更改了
queue的内容,这样岂不是可以好几个reader thread一起dequeue?就不thread safe了
吧。是不是应该直接在method上加synchronized?
h******e
发帖数: 52
4
简化一下
public class ProducerConsumer
{
private Semaphore fillCount;
private Semaphore emptyCount;
private ConcurrentQueue queue;
public ProducerConsumer()
{
fillCount = new Semaphore(0, 100);
emptyCount = new Semaphore(100, 100);
queue = ConcurrentQueue();
}
public void Producer()
{
while(true)
{
//produce something
int x = 1;
emptyCount.WaitOne();
queue.Enqueue(x);

fillCount.Release();
}
}
public void Consumer()
{
while(true)
{
fillCount.WaitOne();
int x = queue.Dequeue();
emptyCount.Release();
}
}
}

【在 N*****a 的大作中提到】
: 对啊,没怎么复习concurrency的题,感觉挺难的。
: 请问楼主,consumer为什么用reader lock,consumer里面有dequeue这个操作,更改了
: queue的内容,这样岂不是可以好几个reader thread一起dequeue?就不thread safe了
: 吧。是不是应该直接在method上加synchronized?

h**p
发帖数: 211
5
LZ, high through put是怎么做到的?面试官有详细说明吗?
前后提供的代码好像都有一点点小问题
详细的请看这里:https://en.wikipedia.org/wiki/Monitor_(synchronization)
下面的conditional variable, producer/consumer这块

【在 h******e 的大作中提到】
: 简化一下
: public class ProducerConsumer
: {
: private Semaphore fillCount;
: private Semaphore emptyCount;
: private ConcurrentQueue queue;
: public ProducerConsumer()
: {
: fillCount = new Semaphore(0, 100);
: emptyCount = new Semaphore(100, 100);

b******b
发帖数: 713
6
sorry cannot type in chinese.
I would start with, in practice no one is going to write producer/consumer
queue himself instead of using an existing concurrent queue implementation.
E.g. in java, there are ArrayBlockingQueue, LinkedBlockingQueue, etc, which
can be used for producer/consumer problem, and they have different
performance depend on use case. We can dive down int more details, e.g.
ArrayBlockingQueue is using 1 lock, thus producer/consumer is sharing same
lock, while LinkedBlockingQueue is doing lock splitting, thus producer/
consumer is not blocking each other, but the add/remove operation is more
expensive than ArrayQueue which just update the index,etc.
In the case of more than 1 producer/consumer, there are other tech to
improve concurrency, e.g. lock stripping, or some open source product like
Disruptor queue, etc.

【在 h**p 的大作中提到】
: LZ, high through put是怎么做到的?面试官有详细说明吗?
: 前后提供的代码好像都有一点点小问题
: 详细的请看这里:https://en.wikipedia.org/wiki/Monitor_(synchronization)
: 下面的conditional variable, producer/consumer这块

J****r
发帖数: 274
7
High throughput需要用到lock-free datastructure。Google一下LMAX Disruptor吧。

【在 h******e 的大作中提到】
: producer / consumer 问题, 要求threadsafe, high throughput
: class ProducerConsumer
: {
: ReaderWriterLock rwLock = new ReaderWriterLock();
: AutoResetEvent FullEvent = new AutoResetEvent ();
: AutoResetEvent EmptyEvent = new AutoResetEvent ();
: public void Producer()
: {
: rwLock.AcquireWriterLock();
: while(queue is full)

1 (共1页)
进入JobHunting版参与讨论
相关主题
问个题:get max value from Queue, with O(1)?新鲜电面经
F家 一道LIS 的变种电面问题
面试题question 2: o(1) euque and dequeue?
一道很难的面试题请教一个系统设计问题 (转载)
F家电面如何实现binary tree的从下到上的分层打印?
碰到一道题share 面试题
一道电话题Is this a DP problem?
how to code this question of LinkedIn这个用stack实现queue
相关话题的讨论汇总
话题: producer话题: consumer话题: queue话题: public