[英]why two thread produce below result ( ocjp test)?
I have below ocjp test which i don't understand the answer (as I thought the answer should be A+D,but the correct answer is A+B), anyone can explain that?:我有以下 ocjp 测试,我不明白答案(因为我认为答案应该是 A+D,但正确答案是 A+B),谁能解释一下?:
Given a piece of code.
给出一段代码。 Which two are possible results?
哪两个是可能的结果? (Select two)
(选择两项)
public class Cruiser { private int a = 0; public void foo() { Runnable r = new LittleCruiser(); new Thread(r).start(); new Thread(r).start(); } public static void main(String arg[]) { Cruiser c = new Cruiser(); c.foo(); } public class LittleCruiser implements Runnable { public void run() { int current = 0; for (int i = 0; i < 4; i++) { current = a; System.out.print(current + ", "); a = current + 2; } } } }
A) 0, 2, 4, 0, 2, 4, 6, 6,
A) 0, 2, 4, 0, 2, 4, 6, 6,
B) 0, 2, 4, 6, 8, 10, 12, 14,B) 0, 2, 4, 6, 8, 10, 12, 14,
C) 0, 2, 4, 6, 8, 10, 2, 4,C) 0, 2, 4, 6, 8, 10, 2, 4,
D) 0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14,D) 0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14,
E) 0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10, 12, 14,E) 0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10, 12, 14,
Answers: A, B答案:A、B
The run
method has a for
loop that executes 4 times and prints something each time. run
方法有一个for
循环,该循环执行 4 次并每次打印一些内容。 As you run 2 threads, you should end up printing 8 things, so answers D and E are not possible.当你运行 2 个线程时,你最终应该打印 8 个东西,所以答案 D 和 E 是不可能的。
As for what is being displayed, you need to see that a
is shared and remember that the threads run in parallel: it's possible that run
from thread 1 runs entirely and then the run
of thread 2 runs entirely, or they get mixed up.至于正在显示的内容,你需要看到的是
a
共享和记住线程并行运行:这是可能的run
从线程1点完全运行,然后run
线程2点完全运行,或它们混合起来。 In the latter case, thread 1 may be inside the for
loop printing something, and then instead of executing the +2
you switch to thread 2 and print again the same number.在后一种情况下,线程 1 可能在
for
循环内打印某些内容,然后您切换到线程 2 并再次打印相同的数字而不是执行+2
。
Answer A is possible: thread 1 starts and executes current = a;
答案 A 是可能的:线程 1 启动并执行
current = a;
in the for
loop, so current
is equal to 0
and a
hasn't been modified.在
for
循环中,所以current
等于0
并且a
没有被修改。 Then thread 2 starts and executes the for
3 times, thus printing 0, 2, 4,
.那么线程2点开始并执行
for
3次,因此印刷0, 2, 4,
Let's say it also sets a
to 6
.假设它还将
a
设置为6
。 Then we're back to thread 1 that continues with its current
being equal to 0
, and it has time to print 3 times, thus printing 0, 2, 4,
.然后我们回到线程 1,它继续它的
current
等于0
,它有时间打印 3 次,从而打印0, 2, 4,
。 It also sets a
to 6
.它还将
a
设置为6
。 Then we're back on thread 2 that gets a
and prints it ie '6,', then we're back on thread that also gets
a` and prints it ie '6,'.然后我们回到获得
a
并打印它的线程 2,即 '6,',然后我们回到that also gets
a` 并打印它的线程that also gets
即 '6,'。
Answer B is possible: for ex thread 1 runs entirely then thread 2. Or the threads get mixed up but not too much, eg each thread has time to execute the 3 lines inside the for
loop before the context switches to the other thread.答案 B 是可能的:例如,线程 1 完全运行,然后线程 2。或者线程混淆但不会太多,例如,在上下文切换到另一个线程之前,每个线程都有时间执行
for
循环内的 3 行。
Answer C is not possible: you can only go back to a previous number if you do a current = a
.答案 C 是不可能的:如果您执行
current = a
则只能返回到以前的数字。 As you go back to 2
, it means that thread 1 already printed 0
, then did the +2
, then current = a
.当你回到
2
,这意味着线程 1 已经打印了0
,然后是+2
,然后是current = a
。 Then thread 2 runs entirely and prints 2, 4, 6, 8
.然后线程 2 完全运行并打印
2, 4, 6, 8
。 Then thread 1 goes back to printing 2
.然后线程 1 返回到打印
2
。 It's not possible to be able to print 10
then go back to 2
(you could go back to 4
though).不可能打印
10
然后返回到2
(尽管您可以返回到4
)。
it's because private int a = 0;这是因为 private int a = 0; it's shared across the two threads.
它在两个线程之间共享。 So both threads are interacting with this variable at the same time.
所以两个线程同时与这个变量交互。 The following is likely to happen:
可能会发生以下情况:
0, 2, 4, 6, 8, 10, 12, 14 0, 2, 4, 6, 8, 10, 12, 14
0, 0, 2, 4, 2, 6, 4, 6, 0, 0, 2, 4, 2, 6, 4, 6,
0, 2, 4, 6, 0, 2, 4, 6, 0, 2, 4, 6, 0, 2, 4, 6,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.