Redian新闻
>
一道多线程的面试题 (转载)
avatar
一道多线程的面试题 (转载)# Programming - 葵花宝典
W*e
1
【 以下文字转载自 Stock 讨论区 】
发信人: g8 (♡♡Bush♡♡), 信区: Stock
标 题: 中钢协号召未来两个月抵制从三大矿山进口铁矿石
发信站: BBS 未名空间站 (Fri Apr 2 14:51:41 2010, 美东)
可以入选2010年十大JOKE!
第一财经频道记者获悉,中钢协秘书长单尚华今日表示,目前铁矿石谈判形势十分严峻,
中钢协号召国内具有进口铁矿石资质的钢企和贸易商,在接下来的两个月内不从三大矿
山进口铁矿石,抵制其垄断行为。
他是在中国钢铁工业协会和中国化工进出口商会在北京召开规模铁矿石市场秩序联合会
议上做出上述表述的。
单尚华表示,中钢协调研报告显示,目前我国钢铁企业的自存铁矿石,足以保证钢厂正常
生产两个月,而且目前我国港口仍积存铁矿石7500万吨。与此同时,今年1、2月份,我国
自产铁矿石生产量在铁矿石价格不断攀高的影响下同比增长18%。因此中钢协号召抵制
目前三大矿山的季度定价政策和无理的涨价要求。
此外,在今天的会议上,中钢协也协同五矿商会制定了三条行业性政策,以整顿目前我国
avatar
z*u
3
http://acg.178.com/201110/112916734716.html
由著名漫画家桂正和创作的超级英雄漫画《ZETMAN》今天流出将要动画化的消息。根据
日媒报道,在明天(10月18日)发售的《ZETMAN》第16卷单行本上讲公布动画化的消息
,预计将在2012年4月份上映。
【剧情介绍】
主角“阿人”,手背有一块环状肉瘤,体内隐藏住神秘力量“Z.E.T”,阿人原
来是实验室创造出的人造人,并不理解人类地情感,但他“爷爷”(参与制造人造人实
验的博士)的教导之下,学习并慢慢体会人类的感情。 这个体内隐藏着神秘力量的少
年──阿人,他会成为「英雄」?还是「恶魔」?手背上有一块环状肉瘤的神奇少年「
阿人」。在怪事件连环发生的情况下,一名神秘老人开始寻找「阿人」的下落。一天,
阿人突然遇上了异形杀人魔,于是令安稳的生活骤变。面对超越人类智能的杀人魔的力
量,阿人会怎么办呢?一个气势磅礡的故事,现在开始!
『ZETMAN』漫画:manhua.178.com/zetman/
avatar
s*t
4
俺也难得来这个版,还这么卖力帮你的版灌水,要100个都不过分。。。
avatar
d*o
5
象slickedit、source insight那样
avatar
w*1
6
sprint的rom,没改过的。谢谢。
avatar
n*e
7
【 以下文字转载自 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.
avatar
s*s
9
桂正和的作品啊,只要上了色我就没有想看的欲望了,百条司的也是。

【在 z****u 的大作中提到】
: http://acg.178.com/201110/112916734716.html
: 由著名漫画家桂正和创作的超级英雄漫画《ZETMAN》今天流出将要动画化的消息。根据
: 日媒报道,在明天(10月18日)发售的《ZETMAN》第16卷单行本上讲公布动画化的消息
: ,预计将在2012年4月份上映。
: 【剧情介绍】
: 主角“阿人”,手背有一块环状肉瘤,体内隐藏住神秘力量“Z.E.T”,阿人原
: 来是实验室创造出的人造人,并不理解人类地情感,但他“爷爷”(参与制造人造人实
: 验的博士)的教导之下,学习并慢慢体会人类的感情。 这个体内隐藏着神秘力量的少
: 年──阿人,他会成为「英雄」?还是「恶魔」?手背上有一块环状肉瘤的神奇少年「
: 阿人」。在怪事件连环发生的情况下,一名神秘老人开始寻找「阿人」的下落。一天,

avatar
P*N
10
Thx
avatar
S*w
11
ecde
avatar
c*x
12
没弄过?新玩意
avatar
g*g
13
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?

avatar
z*i
14
no idea. At least stock price dropped fast AH

【在 z***e 的大作中提到】
: no more cash to pay dividend?
avatar
f*l
15
好像还行的样子?
avatar
t*k
16
Thx
avatar
b*w
17
哪家的?S家的只要有数据计划就可以用wmwifirouter

【在 w****1 的大作中提到】
: sprint的rom,没改过的。谢谢。
avatar
c*e
18
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

avatar
k*n
19
AGNC最近几年随着revenue指数上升一直大量发行股票,一贯行为,不说明什么
倒是它的收入预期直接导致了下跌

【在 z**i 的大作中提到】
: no idea. At least stock price dropped fast AH
avatar
C*e
20
桂正和走这种路线了?
感觉有点怪怪的
avatar
m*e
21
靠,包子机?
avatar
A*a
22
用usb cable的话,手机上有个程序叫internet sharing
打开之后选connect, 然后连电脑就可以了

【在 w****1 的大作中提到】
: sprint的rom,没改过的。谢谢。
avatar
g*g
23
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

avatar
l*o
24
这触发器明显没有人工智能模块啊。

【在 m*****e 的大作中提到】
: 靠,包子机?
avatar
z*t
25

总显示 erro 67
我是sero plan
该怎么弄呢?
而且不支持动态ip分配 静态ip 网关怎么设置?

【在 A****a 的大作中提到】
: 用usb cable的话,手机上有个程序叫internet sharing
: 打开之后选connect, 然后连电脑就可以了

avatar
n*e
26
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

avatar
b*w
27
官方的sero如果要tether是要另交钱的,用wmwifirouter

【在 z***t 的大作中提到】
:
: 总显示 erro 67
: 我是sero plan
: 该怎么弄呢?
: 而且不支持动态ip分配 静态ip 网关怎么设置?

avatar
c*e
28
恩,其实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.

avatar
b*e
29
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

avatar
b*s
30
随便用个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?

avatar
j*I
31
好像题面没说是程序自己调度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

avatar
b*a
32
上代码,三个类。可以直接运行
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++;
}
}
avatar
h*l
33
这是我的第一想法,但是有没有可能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) {
: ...
: }

avatar
k*g
34

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++) {

avatar
a*e
35
这个应该是类似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

avatar
d*a
36
在高性能计算里,是基本功。

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

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。