由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 一道多线程的面试题
相关主题
MITBBS 面试题第一题L 家面经
LinkedIn 面经面试时候差点想直接推门走,真有这感觉!
问一道Multithread和一道social graph的题面试的时候让你写一个thread pool,能写正确的来举手
Java编程讨论:LinkedIn的H2Ojava concurrence 例子
Amazon一道synchronization的面试题hashmap和hashtable的区别?
分享面试题怎么练习multi-threading,平常工作都是用Java框架
concurrency应该怎么复习一个题:多线程怎么调试?
如何处理多用户同时调用方法修改数据库菜鸟请教多线程怎么学
相关话题的讨论汇总
话题: thread话题: printing话题: threads话题: what话题: word
进入JobHunting版参与讨论
1 (共1页)
n****e
发帖数: 43
1
You are given a paragraph , which contain n number of words, you are given m
threads. What you need to do is , each thread should print one word and
give the control to next thread, this way each thread will keep on printing
one word , in case last thread come, it should invoke the first thread.
Printing will repeat until all the words are printed in paragraph. Finally
all threads should exit gracefully. What kind of synchronization will use?
I have no idea how to solve this question. Any guru helps me? Thanks.
s********r
发帖数: 403
2
message queue
或自己建一个 circular buffer size of m, 每个指向一个 event,
thread 完成自己的操作后向下一个 slot post event,转完一圈后正好可以回到原来的
位置。
n****e
发帖数: 43
3
我觉得是不是这样的。可以建一个 circular buffer size of n(不是m)。然后这个
circular buffer 里面放的是n个word(这样才能知道什么时候把所有的word读完了)
。然后产生m 个event,除了一个thread以外,其他m-1个thread都被events block 住
。每次一个thread 读完一个数据,就设置下一个event 是signal的,这样就能保证每
次都只有一个thread 在读数据.

【在 s********r 的大作中提到】
: message queue
: 或自己建一个 circular buffer size of m, 每个指向一个 event,
: thread 完成自己的操作后向下一个 slot post event,转完一圈后正好可以回到原来的
: 位置。

p********s
发帖数: 3
4
如何实现 “Finally all threads should exit gracefully.”?

【在 s********r 的大作中提到】
: message queue
: 或自己建一个 circular buffer size of m, 每个指向一个 event,
: thread 完成自己的操作后向下一个 slot post event,转完一圈后正好可以回到原来的
: 位置。

s********r
发帖数: 403
5
当剩余的buffer size n_left == 0 时,多转一圈即可

【在 p********s 的大作中提到】
: 如何实现 “Finally all threads should exit gracefully.”?
G****A
发帖数: 4160
6
+1

【在 n****e 的大作中提到】
: 我觉得是不是这样的。可以建一个 circular buffer size of n(不是m)。然后这个
: circular buffer 里面放的是n个word(这样才能知道什么时候把所有的word读完了)
: 。然后产生m 个event,除了一个thread以外,其他m-1个thread都被events block 住
: 。每次一个thread 读完一个数据,就设置下一个event 是signal的,这样就能保证每
: 次都只有一个thread 在读数据.

n****e
发帖数: 43
7
不明白你这是怎么做到让所有thread都退出的。我觉得还需要一个退出event,当某个
thread读到最后一个word的时候,就让这个退出event处于signal状态,这样所有的
thread 就都可以退出了。

【在 s********r 的大作中提到】
: 当剩余的buffer size n_left == 0 时,多转一圈即可
s********r
发帖数: 403
8
how about m=4, n=1 billion
4 core 系统, 1 个 billion 的 words

【在 n****e 的大作中提到】
: 我觉得是不是这样的。可以建一个 circular buffer size of n(不是m)。然后这个
: circular buffer 里面放的是n个word(这样才能知道什么时候把所有的word读完了)
: 。然后产生m 个event,除了一个thread以外,其他m-1个thread都被events block 住
: 。每次一个thread 读完一个数据,就设置下一个event 是signal的,这样就能保证每
: 次都只有一个thread 在读数据.

s********r
发帖数: 403
9
At each print, should check the buffer anyway,
if the buffer is ended, post the event to next slot and return.
So the last cycle round, no threads print, only post events.

【在 n****e 的大作中提到】
: 不明白你这是怎么做到让所有thread都退出的。我觉得还需要一个退出event,当某个
: thread读到最后一个word的时候,就让这个退出event处于signal状态,这样所有的
: thread 就都可以退出了。

m*****k
发帖数: 731
10
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/C

m
printing

【在 n****e 的大作中提到】
: You are given a paragraph , which contain n number of words, you are given m
: threads. What you need to do is , each thread should print one word and
: give the control to next thread, this way each thread will keep on printing
: one word , in case last thread come, it should invoke the first thread.
: Printing will repeat until all the words are printed in paragraph. Finally
: all threads should exit gracefully. What kind of synchronization will use?
: I have no idea how to solve this question. Any guru helps me? Thanks.

相关主题
分享面试题L 家面经
concurrency应该怎么复习面试时候差点想直接推门走,真有这感觉!
如何处理多用户同时调用方法修改数据库面试的时候让你写一个thread pool,能写正确的来举手
进入JobHunting版参与讨论
m*******h
发帖数: 2
11
Not the best quality of code, but does solve the problem.
https://gist.github.com/anonymous/6182478
x**n
发帖数: 461
12
这不是上课的作业题吗?
s********r
发帖数: 403
13
这个就是弄着玩儿的,
多核线程设计这种sequential print 模式, 直接用1个 thread print 算了
f*******b
发帖数: 520
14
MARK
a****Q
发帖数: 83
15
two steps: 1) read the next word, and 2) print it. 1) is in memory operation
and needs to be managed; 2) is per thread and no thread safty concern.
To implement 1), ether list or array shall work. and lockfree is the key.
r****s
发帖数: 1025
16
用一个synchronous queue,再加个semaphore size(1), 一个字符 变量, 不就行了?
每个thread wait on semaphore, 而不是queue。抢到semaphore的就先查前一字符是什
么,如果是回车就release semaphore, exit; 如果不是就等在queue那里,进来一个字
符就print,然后把字符update进变量。release semaphore.
最后送一个回车符,大家一起exit就行了。
1 (共1页)
进入JobHunting版参与讨论
相关主题
菜鸟请教多线程怎么学Amazon一道synchronization的面试题
有一道Java多线程的面试题能不能帮我看看?分享面试题
two functons and two threadsconcurrency应该怎么复习
请教三个多线程(multi-threading)题目如何处理多用户同时调用方法修改数据库
MITBBS 面试题第一题L 家面经
LinkedIn 面经面试时候差点想直接推门走,真有这感觉!
问一道Multithread和一道social graph的题面试的时候让你写一个thread pool,能写正确的来举手
Java编程讨论:LinkedIn的H2Ojava concurrence 例子
相关话题的讨论汇总
话题: thread话题: printing话题: threads话题: what话题: word