由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 问一道Multithread和一道social graph的题
相关主题
怎么练习multi-threading,平常工作都是用Java框架菜鸟请教过来人,关于CS面试中的非刷题部分
一道多线程的面试题关于multithread programming大家看什么书
C++ timer class for multithreads? (转载)MS Azure组面试
Multithread问一下Multithreaded Programming有啥经典书籍可以推荐?
Thread 和 concurrency怎么准备?找工作需要:推荐一本multithreaded programming的书吧
关于java的疑惑Java编程讨论:LinkedIn的H2O
concurrency questions关于java synchronized statement和static method or variable
concurrency应该怎么复习multi thread复习请教
相关话题的讨论汇总
话题: thread话题: method话题: 运行话题: running
进入JobHunting版参与讨论
1 (共1页)
e********3
发帖数: 229
1
1. 给定2 threads, 每个thread相隔一个小时运行一次.他们运行时会调用myclass 里
的doWork method. 这个method就是update myclass里的一个field.比如一个int filed
.这个dowork method保证在2分钟之内完成. thread会在一开始运行时就call这个
method. 请问对这个method我们需要synchronization吗?
2. 如何implement 领英的social graph. 要求给定2个user,你能快速查出这2个user是
不是直接connected或者间接connected.比如2nd connection, 3rd connection.
3. 接2, 如果要求能查出某个user有多少个direct connections呢?
求大牛解惑
t**r
发帖数: 3428
2
1. 如果函数是no side-effect的话,不需要。java里int是atomic.
2. adjacent list. ; disjoint set.
3. ...
e********3
发帖数: 229
3
1. 我也觉得不需要.但是面试官的语气和态度像是需要...不知道是不是在唬我
2. 可以具体一点吗?
3. ...
h****e
发帖数: 2125
4
第一题thread相隔一小时运行是什么意思:是thread 1执行完了之后一小时thread 2再
执行呢,还是说thread 1六点整执行,thread 2七点执行?

【在 e********3 的大作中提到】
: 1. 我也觉得不需要.但是面试官的语气和态度像是需要...不知道是不是在唬我
: 2. 可以具体一点吗?
: 3. ...

b*****n
发帖数: 618
5
1.应该需要volatile来保证读写都是对memory进行的吧

【在 e********3 的大作中提到】
: 1. 我也觉得不需要.但是面试官的语气和态度像是需要...不知道是不是在唬我
: 2. 可以具体一点吗?
: 3. ...

t**r
发帖数: 3428
6
good point

【在 b*****n 的大作中提到】
: 1.应该需要volatile来保证读写都是对memory进行的吧
e********3
发帖数: 229
7

后者.thread1 6点开始执行,然后马上invoke那个dowork method.

【在 h****e 的大作中提到】
: 第一题thread相隔一小时运行是什么意思:是thread 1执行完了之后一小时thread 2再
: 执行呢,还是说thread 1六点整执行,thread 2七点执行?

t**r
发帖数: 3428
8
关键你要问他,这个函数还有没有别的side-effect.如果没有
肯定不需要synchronized keyword.

【在 e********3 的大作中提到】
: 1. 我也觉得不需要.但是面试官的语气和态度像是需要...不知道是不是在唬我
: 2. 可以具体一点吗?
: 3. ...

e********3
发帖数: 229
9

我也这么说.然后面试官就开始问:什么是synchronization,什么时候用
synchronization blabla...然后重新回过来问,现在这道题需要用synchronization吗?
不知道是想唬我还是在提示我...

【在 b*****n 的大作中提到】
: 1.应该需要volatile来保证读写都是对memory进行的吧
e********3
发帖数: 229
10

不一定用synchronized keyword,lock之类也行.应该是没有什么side-effect的.它就说
这个函数是gaurantee在2分钟内完成的.

【在 t**r 的大作中提到】
: 关键你要问他,这个函数还有没有别的side-effect.如果没有
: 肯定不需要synchronized keyword.

相关主题
关于java的疑惑菜鸟请教过来人,关于CS面试中的非刷题部分
concurrency questions关于multithread programming大家看什么书
concurrency应该怎么复习MS Azure组面试
进入JobHunting版参与讨论
b*****n
发帖数: 618
11
可能跟两个thread的真正的运行顺序有关?
是不是确定第一个thread运行完了另一个才会运行?
执行的意思是真正运行吗。。
update那个field是怎么update的,是不是atomic的?
只能想到这些。。

吗?

【在 e********3 的大作中提到】
:
: 不一定用synchronized keyword,lock之类也行.应该是没有什么side-effect的.它就说
: 这个函数是gaurantee在2分钟内完成的.

s********l
发帖数: 998
12
请问第2 3 题 你是怎么答的?

filed

【在 e********3 的大作中提到】
: 1. 给定2 threads, 每个thread相隔一个小时运行一次.他们运行时会调用myclass 里
: 的doWork method. 这个method就是update myclass里的一个field.比如一个int filed
: .这个dowork method保证在2分钟之内完成. thread会在一开始运行时就call这个
: method. 请问对这个method我们需要synchronization吗?
: 2. 如何implement 领英的social graph. 要求给定2个user,你能快速查出这2个user是
: 不是直接connected或者间接connected.比如2nd connection, 3rd connection.
: 3. 接2, 如果要求能查出某个user有多少个direct connections呢?
: 求大牛解惑

e********3
发帖数: 229
13

对.我也问了有没有可能在第59分钟thread才真正开始Invoke了那个method?答曰不会.
在thread一开始就invoke了.update应该就是保证在2分钟内即dowork完就update了.这2
个thread都是每隔1小时运行的.
那烙印可能想唬我改变答案吧...

【在 b*****n 的大作中提到】
: 可能跟两个thread的真正的运行顺序有关?
: 是不是确定第一个thread运行完了另一个才会运行?
: 执行的意思是真正运行吗。。
: update那个field是怎么update的,是不是atomic的?
: 只能想到这些。。
:
: 吗?

e********3
发帖数: 229
14

我也是来请教的...我自己胡说了一通...等大牛来答

【在 s********l 的大作中提到】
: 请问第2 3 题 你是怎么答的?
:
: filed

l******s
发帖数: 3045
15
当然需要,两分钟内运行完有可能是一秒,有可能是一分五十九秒 ,长时间后 ,两个
thread会有同时运行的风险。

filed

【在 e********3 的大作中提到】
: 1. 给定2 threads, 每个thread相隔一个小时运行一次.他们运行时会调用myclass 里
: 的doWork method. 这个method就是update myclass里的一个field.比如一个int filed
: .这个dowork method保证在2分钟之内完成. thread会在一开始运行时就call这个
: method. 请问对这个method我们需要synchronization吗?
: 2. 如何implement 领英的social graph. 要求给定2个user,你能快速查出这2个user是
: 不是直接connected或者间接connected.比如2nd connection, 3rd connection.
: 3. 接2, 如果要求能查出某个user有多少个direct connections呢?
: 求大牛解惑

h****e
发帖数: 2125
16
对,我问的问题就是这个意思。

【在 l******s 的大作中提到】
: 当然需要,两分钟内运行完有可能是一秒,有可能是一分五十九秒 ,长时间后 ,两个
: thread会有同时运行的风险。
:
: filed

g*****g
发帖数: 34805
17
我肯定加上 同步,反正也不影响性能,日后维护没炸弹。

这2

【在 e********3 的大作中提到】
:
: 我也是来请教的...我自己胡说了一通...等大牛来答

g*****g
发帖数: 34805
18
quartz之类的类库可以保证准点运行。

【在 l******s 的大作中提到】
: 当然需要,两分钟内运行完有可能是一秒,有可能是一分五十九秒 ,长时间后 ,两个
: thread会有同时运行的风险。
:
: filed

e********3
发帖数: 229
19

我问过面试官,说有没有可能在第59分才开始invoke thread.他说不会.thread start就
invoke了.也就是说每次thread运行完都会有58分钟的空缺直到下个thread运行.

【在 l******s 的大作中提到】
: 当然需要,两分钟内运行完有可能是一秒,有可能是一分五十九秒 ,长时间后 ,两个
: thread会有同时运行的风险。
:
: filed

e********3
发帖数: 229
20

是.但是他问的就是在这个特定的case里需不需要同步.

【在 g*****g 的大作中提到】
: 我肯定加上 同步,反正也不影响性能,日后维护没炸弹。
:
: 这2

相关主题
问一下Multithreaded Programming有啥经典书籍可以推荐?关于java synchronized statement和static method or variable
找工作需要:推荐一本multithreaded programming的书吧multi thread复习请教
Java编程讨论:LinkedIn的H2O问几个关于G M A F面试的general问题
进入JobHunting版参与讨论
l******s
发帖数: 3045
21
你的问题和他的回答无法推出你的结论。

【在 e********3 的大作中提到】
:
: 是.但是他问的就是在这个特定的case里需不需要同步.

e********3
发帖数: 229
22

好吧..那我没太懂你的问题.
一个thread在1点开始运行,然后2分钟内运行完,就是1:02运行完,剩下58分钟idle等下
个thread在2:00开始运行.根据我的理解这里是有什么不对么?

【在 l******s 的大作中提到】
: 你的问题和他的回答无法推出你的结论。
l******s
发帖数: 3045
23
My Chinese input stopped working on computer, so let me explain it in
English.
I understand your question here which is to confirm if the interval between
the 2 running of same threads are based on the time point of the beginning
of thread running, while your questions in last post made me think you are
asking if the thread will sleep/delay/suspend 59 minutes then run it, and I
thought that's what that interviewer understood so he answered "it's invoked
at start of the thread running".
So, we didn't get the question clarified, if the interval should be
calculated between the end of first running and begin of next running, or
begin of first running and begin of next running?

【在 e********3 的大作中提到】
:
: 好吧..那我没太懂你的问题.
: 一个thread在1点开始运行,然后2分钟内运行完,就是1:02运行完,剩下58分钟idle等下
: 个thread在2:00开始运行.根据我的理解这里是有什么不对么?

e********3
发帖数: 229
24

between
I
invoked
it should be the latter one. there will be 58 mins that nothing is happening
for both of the threads.

【在 l******s 的大作中提到】
: My Chinese input stopped working on computer, so let me explain it in
: English.
: I understand your question here which is to confirm if the interval between
: the 2 running of same threads are based on the time point of the beginning
: of thread running, while your questions in last post made me think you are
: asking if the thread will sleep/delay/suspend 59 minutes then run it, and I
: thought that's what that interviewer understood so he answered "it's invoked
: at start of the thread running".
: So, we didn't get the question clarified, if the interval should be
: calculated between the end of first running and begin of next running, or

p*u
发帖数: 2454
25
are things like daylight savings handled?

【在 e********3 的大作中提到】
:
: between
: I
: invoked
: it should be the latter one. there will be 58 mins that nothing is happening
: for both of the threads.

C*********o
发帖数: 119
26
That's a good point. 应该就是问这个DST transition time这个一年两次的特殊情况
z****e
发帖数: 54598
27
我觉得应该加上同步
因为虽然保证在2m内完成
那鬼知道呢,如果真发生了在1h内完不成的情况呢?
其次就是,这里有明显的side effect
都update myclass field了,显然这个method不是pure
最好逻辑代码放到myclass里面去
封装进去,然后synchronized所有methods
这样就可以互斥了,当然最理想的是从结构上打掉这种内存共享机制
除非不得不用,比如做pvp gaming
c***p
发帖数: 17
28
Social graph 这题是不是一般用bidirectional BFS? 然后直接看两个用户间的距离?

filed

【在 e********3 的大作中提到】
: 1. 给定2 threads, 每个thread相隔一个小时运行一次.他们运行时会调用myclass 里
: 的doWork method. 这个method就是update myclass里的一个field.比如一个int filed
: .这个dowork method保证在2分钟之内完成. thread会在一开始运行时就call这个
: method. 请问对这个method我们需要synchronization吗?
: 2. 如何implement 领英的social graph. 要求给定2个user,你能快速查出这2个user是
: 不是直接connected或者间接connected.比如2nd connection, 3rd connection.
: 3. 接2, 如果要求能查出某个user有多少个direct connections呢?
: 求大牛解惑

s***c
发帖数: 639
29
3怎么看都像是2的followup

filed

【在 e********3 的大作中提到】
: 1. 给定2 threads, 每个thread相隔一个小时运行一次.他们运行时会调用myclass 里
: 的doWork method. 这个method就是update myclass里的一个field.比如一个int filed
: .这个dowork method保证在2分钟之内完成. thread会在一开始运行时就call这个
: method. 请问对这个method我们需要synchronization吗?
: 2. 如何implement 领英的social graph. 要求给定2个user,你能快速查出这2个user是
: 不是直接connected或者间接connected.比如2nd connection, 3rd connection.
: 3. 接2, 如果要求能查出某个user有多少个direct connections呢?
: 求大牛解惑

b******b
发帖数: 713
30
Let me try my best to answer questions 1, if anything not clear, also I
strongly recommend you to read the first 50 pages of the book "Java
Concurrency in Practice", this is a very good book for you to understand
java concurrency better.
If you don't use any volatile/synchronization, if you changed a variable in
1 thread, there is no grantee when the other thread will see the change, and
in which order if there is multiple changes. For example, in Thread 1 (T1),
you change an int variable i from 1 to 2, Thread 2 may never see the
variable changed to 2 in its lifecycle, JVM doesn't guarantee anything about
when Thread 2 will see i changed to 2.
Another tricky things is, let's say in T1, you did following:
i = 2;
j = 2;
you may assume when T2 sees j changed to 2, it must see i changed to 2, but
this may also not true. if you don't use any memory barrier, e.g. volatile,
synchronization, etc. it's totally ok for JVM to reorder the execution, e.g.
do j=2 before i=2, and it may push j=2 to memory so T2 saw it, but T2 may
never saw i=2.
Again, for any confusion, please refer to "java concurrency in practice"
book, it explains much better than I do.

filed

【在 e********3 的大作中提到】
: 1. 给定2 threads, 每个thread相隔一个小时运行一次.他们运行时会调用myclass 里
: 的doWork method. 这个method就是update myclass里的一个field.比如一个int filed
: .这个dowork method保证在2分钟之内完成. thread会在一开始运行时就call这个
: method. 请问对这个method我们需要synchronization吗?
: 2. 如何implement 领英的social graph. 要求给定2个user,你能快速查出这2个user是
: 不是直接connected或者间接connected.比如2nd connection, 3rd connection.
: 3. 接2, 如果要求能查出某个user有多少个direct connections呢?
: 求大牛解惑

相关主题
本公司在招人,有需要推荐的请联系一道多线程的面试题
Facebook Moving To The JVMC++ timer class for multithreads? (转载)
怎么练习multi-threading,平常工作都是用Java框架Multithread
进入JobHunting版参与讨论
p*u
发帖数: 2454
31
did u even read lz's question??

in
and
),
about

【在 b******b 的大作中提到】
: Let me try my best to answer questions 1, if anything not clear, also I
: strongly recommend you to read the first 50 pages of the book "Java
: Concurrency in Practice", this is a very good book for you to understand
: java concurrency better.
: If you don't use any volatile/synchronization, if you changed a variable in
: 1 thread, there is no grantee when the other thread will see the change, and
: in which order if there is multiple changes. For example, in Thread 1 (T1),
: you change an int variable i from 1 to 2, Thread 2 may never see the
: variable changed to 2 in its lifecycle, JVM doesn't guarantee anything about
: when Thread 2 will see i changed to 2.

b******b
发帖数: 713
32
hmm, it's possible I misunderstand something, though I think my answer is
what the interviewer looking for. The question is, whether you need
synchronization if 2 thread is updating same field (suppose those 2 thread
also read the same field) even we know the 2 thread is not going to run at
same time, e.g. 1 hour apart. The obvious answer is we don't need
syncrhronization or volatile for the field, but this is wrong because how
the JVM handles the variable value publish (assume its using java). If this
is not the intention of the question, then what I said is totally not
relevant.

【在 p*u 的大作中提到】
: did u even read lz's question??
:
: in
: and
: ),
: about

1 (共1页)
进入JobHunting版参与讨论
相关主题
multi thread复习请教Thread 和 concurrency怎么准备?
问几个关于G M A F面试的general问题关于java的疑惑
本公司在招人,有需要推荐的请联系concurrency questions
Facebook Moving To The JVMconcurrency应该怎么复习
怎么练习multi-threading,平常工作都是用Java框架菜鸟请教过来人,关于CS面试中的非刷题部分
一道多线程的面试题关于multithread programming大家看什么书
C++ timer class for multithreads? (转载)MS Azure组面试
Multithread问一下Multithreaded Programming有啥经典书籍可以推荐?
相关话题的讨论汇总
话题: thread话题: method话题: 运行话题: running