由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 一道多线程的面试题 (转载)
相关主题
真正的multi-threading是5个thread要5个cpu?那apache是真正的mwhy do we need to map user threads to kernel threads?
multi threading 还是 multi processingProblem of Thread by Prof. Lee大伙怎么看?
thread, semaphore, 问题。一个multithread的问题(是不是有人觉的很简单?)
python下的expectrand() in multitreading
多线程编程前景如何?thread c++ 问题
c++下如何实现多线程?a question about Nachos in C
[合集] 怎么样提高自己的multi-thread programming能力?用多线程怎么比单线程还慢呢?
有谁用TBB吗来来,讨论一下multithread, multi-core, affinity
相关话题的讨论汇总
话题: thread话题: paragraph话题: words话题: public
进入Programming版参与讨论
1 (共1页)
n****e
发帖数: 43
1
【 以下文字转载自 JobHunting 讨论区 】
发信人: njhome (njhome), 信区: JobHunting
标 题: 一道多线程的面试题
发信站: BBS 未名空间站 (Tue Aug 6 13:25:41 2013, 美东)
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.
g*****g
发帖数: 34805
2
You don't need synchronization, only one thread is running at one time. All
you need is a circular linked list.

m
printing

【在 n****e 的大作中提到】
: 【 以下文字转载自 JobHunting 讨论区 】
: 发信人: njhome (njhome), 信区: JobHunting
: 标 题: 一道多线程的面试题
: 发信站: BBS 未名空间站 (Tue Aug 6 13:25:41 2013, 美东)
: 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?

c*********e
发帖数: 16335
3
multi-threading真正用得多吗?听起来象是神功一样的东西,但是很少用到啊。

All

【在 g*****g 的大作中提到】
: You don't need synchronization, only one thread is running at one time. All
: you need is a circular linked list.
:
: m
: printing

g*****g
发帖数: 34805
4
Depends, if you are writing frameworks, yes, if you are writing application
on top of frameworks, you'll deal with it much less.

【在 c*********e 的大作中提到】
: multi-threading真正用得多吗?听起来象是神功一样的东西,但是很少用到啊。
:
: All

n****e
发帖数: 43
5
Let's discuss more details. Suppose we have 10 threads and 100 words. We put
100 words into a circular linked list and allow only one thread to run
initially. I think we need two global pointers. One points to first node of
circular linked list and another one points to current node. After the
running thread reads the node and prints out the word, it updates the
current node and resumes another thread and suspends itself. Keep doing this
until first node is equal to the current node. Is that what you suggested?
Thanks.

All

【在 g*****g 的大作中提到】
: You don't need synchronization, only one thread is running at one time. All
: you need is a circular linked list.
:
: m
: printing

c*********e
发帖数: 16335
6
恩,其实framework里面好多东西都已经是用multi-threading写成的了,比如servlet.

application

【在 g*****g 的大作中提到】
: Depends, if you are writing frameworks, yes, if you are writing application
: on top of frameworks, you'll deal with it much less.

b*****e
发帖数: 474
7
Setting up the threads to take turns is very easy. Each thread should have a
m_MyLock member object, and also a member m_NextLock pointing to
the next thread's m_MyLock object.
Each thread:
while ( ! done ) {
try {
m_MyLock.wait();
} catch ( InterruptionException e) {
...
}
if ( has more words ) print next word;
else done = true;
m_NextLock.notify();
}
Main program:
set up threads and their lock references;
start all threads; // they will all be waiting now
thread_0.m_Mylock.notify();
The "exit gracefully" part is vague, so I'd leave that part out.

m
printing

【在 n****e 的大作中提到】
: Let's discuss more details. Suppose we have 10 threads and 100 words. We put
: 100 words into a circular linked list and allow only one thread to run
: initially. I think we need two global pointers. One points to first node of
: circular linked list and another one points to current node. After the
: running thread reads the node and prints out the word, it updates the
: current node and resumes another thread and suspends itself. Keep doing this
: until first node is equal to the current node. Is that what you suggested?
: Thanks.
:
: All

b*******s
发帖数: 5216
8
随便用个sync primitive,比如mutex,保护一个计数
这个计数也是个index,0开始
每个得到锁的线程,判断index是不是 >= n ,成立则销毁
否则就先++index,释放锁,打印没有自增前index位置的词

m
printing

【在 n****e 的大作中提到】
: 【 以下文字转载自 JobHunting 讨论区 】
: 发信人: njhome (njhome), 信区: JobHunting
: 标 题: 一道多线程的面试题
: 发信站: BBS 未名空间站 (Tue Aug 6 13:25:41 2013, 美东)
: 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?

j*****I
发帖数: 2626
9
好像题面没说是程序自己调度threads. 当然也可以这样理解,不过那样就太矫情了。

All

【在 g*****g 的大作中提到】
: You don't need synchronization, only one thread is running at one time. All
: you need is a circular linked list.
:
: m
: printing

b*********a
发帖数: 723
10
上代码,三个类。可以直接运行
package com.bruce.concurrent;
public class MainApplication {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "I am from China and I was born in Jiangxi province";
Paragraph para = new Paragraph(str);
Thread pt = null;
int threadCount=5;
for (int i = 0; i < threadCount; i++) {
pt = new Thread(new PrinterThread(i, para, threadCount));
pt.start();
}
}
}
package com.bruce.concurrent;
public class PrinterThread implements Runnable {
private int threadNum = 0;
private int threadCount = 0;
private Paragraph para = null;
public PrinterThread(int threadNum, Paragraph para, int threadCount) {
this.threadNum = threadNum;
this.para = para;
this.threadCount = threadCount;
}
@Override
public void run() {
while (true) {
if (para.getNextWord() == null) {
System.out.println("Thread " + threadNum + " is completed...
");
return;
} else if ((para.getCurrentIndex()) % threadCount == threadNum) {
System.out.println(para.getNextWord());
para.next();
}
}
}
}
package com.bruce.concurrent;
import java.util.Arrays;
public class Paragraph {
private String[] words = null;
private int currentIndex = 0;
public Paragraph(String para) {
if (para == null)
words = null;
else
words = para.split(" ");
System.out.println(Arrays.toString(words));
}
public int getCurrentIndex() {
return currentIndex;
}
public String getNextWord() {
if (words == null || currentIndex > words.length - 1)
return null;
else {
return words[currentIndex];
}
}
public void next() {
currentIndex++;
}
}
h**********l
发帖数: 410
11
这是我的第一想法,但是有没有可能thread还没run到wait,main thread就notify了,我
的第二想法是用counting semaphore array, 第一个initialize成1, 后面的都
initialize成0, 然后每个thread先P()自己的semaphore, print word, 然后V()下一个
semaphore,我觉得这样比较保险一些

a

【在 b*****e 的大作中提到】
: Setting up the threads to take turns is very easy. Each thread should have a
: m_MyLock member object, and also a member m_NextLock pointing to
: the next thread's m_MyLock object.
: Each thread:
: while ( ! done ) {
: try {
: m_MyLock.wait();
: } catch ( InterruptionException e) {
: ...
: }

k**********g
发帖数: 989
12

http://stackoverflow.com/questions/508850/java-concurrency-cyni
Bruce (Eckel) 很坦承的交代他的电脑不是多核

【在 b*********a 的大作中提到】
: 上代码,三个类。可以直接运行
: package com.bruce.concurrent;
: public class MainApplication {
: public static void main(String[] args) {
: // TODO Auto-generated method stub
: String str = "I am from China and I was born in Jiangxi province";
: Paragraph para = new Paragraph(str);
: Thread pt = null;
: int threadCount=5;
: for (int i = 0; i < threadCount; i++) {

a***e
发帖数: 27968
13
这个应该是类似token ring的协议锁
每个thread建立的时候有个ID,token=ID的时候
检查剩下word的数,word数等于0,token+1,然后自己退出
否则打印,word数-1,然后token+1
token<>ID的时候就等待
这样所有的threads并发
主程序开始清token,建立字串,创建threads
然后token=1启动打印

m
printing

【在 n****e 的大作中提到】
: Let's discuss more details. Suppose we have 10 threads and 100 words. We put
: 100 words into a circular linked list and allow only one thread to run
: initially. I think we need two global pointers. One points to first node of
: circular linked list and another one points to current node. After the
: running thread reads the node and prints out the word, it updates the
: current node and resumes another thread and suspends itself. Keep doing this
: until first node is equal to the current node. Is that what you suggested?
: Thanks.
:
: All

d***a
发帖数: 13752
14
在高性能计算里,是基本功。

【在 c*********e 的大作中提到】
: multi-threading真正用得多吗?听起来象是神功一样的东西,但是很少用到啊。
:
: All

1 (共1页)
进入Programming版参与讨论
相关主题
来来,讨论一下multithread, multi-core, affinity多线程编程前景如何?
multithread app的design要注意哪些问题?c++下如何实现多线程?
一直没有很好理解thread join itself,哪位解惑一下 (转载)[合集] 怎么样提高自己的multi-thread programming能力?
写thread safe程序现在也是程序员必须要掌握的了吧有谁用TBB吗
真正的multi-threading是5个thread要5个cpu?那apache是真正的mwhy do we need to map user threads to kernel threads?
multi threading 还是 multi processingProblem of Thread by Prof. Lee大伙怎么看?
thread, semaphore, 问题。一个multithread的问题(是不是有人觉的很简单?)
python下的expectrand() in multitreading
相关话题的讨论汇总
话题: thread话题: paragraph话题: words话题: public