Given:
public class Thread1 {
int x = 0;
public class Runner implements Runnable {
public void run() {
int current = 0;
for (int i = 0; i < 4; i++) {
current = x;
System.out.print(current + " ");
x = current + 2;
}
}
}
public void go() {
Runnable r1 = new Runner();
new Thread(r1).start();
new Thread(r1).start();
}
public static void main(String[] args) {
new Thread1().go();
}
}
Which two are possible results? (Choose two)
哪两个可能的结果? (选择两个)
A. 0, 2, 4, 4, 6, 8, 10, 6,
A. 0,2,4,4,6,8,10,6,
B. 0, 2, 4, 6, 8, 10, 2, 4,
B. 0,2,4,6,8,10,2,4,
C. 0, 2, 4, 6, 8, 10, 12, 14,
C. 0,2,4,6,8,10,12,14,
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,10,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,
I chose A and B but I'm not certain if that is the correct answer.
我选择了A和B,但我不确定这是否是正确的答案。
2 个解决方案
#1
3
First of all D. and E. are out. As i
scope local to the function, we know that there is going to be 8 numbers.
首先,D.和E.出局了。由于我在函数的本地范围内,我们知道将有8个数字。
Now
B. 0, 2, 4, 6, 8, 10, 2, 4,
B. 0,2,4,6,8,10,2,4,
is incorrect. Why? In order print the last two positions each thread has to write the variable x
at least two times. At this point the minimum value for x
is 4
. Which means that whatever the race conditions, the last two values should be greater or equal to 4
.
是不正确的。为什么?为了打印最后两个位置,每个线程必须至少两次写入变量x。此时x的最小值为4.这意味着无论竞争条件如何,最后两个值应大于或等于4。
EDIT C
is totally possible. Absence of synchronization doesn't mean it is not possible to have what is called a serial execution (ie as if threads execute one after another). The lesson from concurrency is that you don't know how threads will be interleaved
编辑C是完全可能的。没有同步并不意味着不可能拥有所谓的串行执行(即,如果线程一个接一个地执行)。并发的教训是你不知道如何交错线程
C. 0, 2, 4, 6, 8, 10, 12, 14,
happens for example :
例如:
- when the first thread as time to finish updating
x
before the second start - when the first thread is suspended before the first
current = x;
, the second finish executing and the first continue. - tons of other cases.
当第一个线程作为第二次启动之前完成更新x的时间
当第一个线程在第一个电流= x;之前暂停时,第二个完成执行并且第一个继续执行。
吨其他案件。
As Brian Goetz said:
正如Brian Goetz所说:
"Writing correct programs is hard; writing correct concurrent programs is harder."
“编写正确的程序很难;编写正确的并发程序更难。”
#2
0
The answer is A and C. You can eliminate D and E immediately because both threads will display 4 results, so 8 results in total, leaving only A, B & C in play.
答案是A和C.您可以立即消除D和E,因为两个线程将显示4个结果,因此总共有8个结果,只留下A,B和C。
B is eliminated because the sequence between the two 2's is too long for a single thread to have produced, so both threads must have done more work than only reaching 2.
B被消除了,因为两个2之间的序列对于单个线程来说太长了,所以两个线程必须完成更多工作而不是仅仅达到2。
#1
3
First of all D. and E. are out. As i
scope local to the function, we know that there is going to be 8 numbers.
首先,D.和E.出局了。由于我在函数的本地范围内,我们知道将有8个数字。
Now
B. 0, 2, 4, 6, 8, 10, 2, 4,
B. 0,2,4,6,8,10,2,4,
is incorrect. Why? In order print the last two positions each thread has to write the variable x
at least two times. At this point the minimum value for x
is 4
. Which means that whatever the race conditions, the last two values should be greater or equal to 4
.
是不正确的。为什么?为了打印最后两个位置,每个线程必须至少两次写入变量x。此时x的最小值为4.这意味着无论竞争条件如何,最后两个值应大于或等于4。
EDIT C
is totally possible. Absence of synchronization doesn't mean it is not possible to have what is called a serial execution (ie as if threads execute one after another). The lesson from concurrency is that you don't know how threads will be interleaved
编辑C是完全可能的。没有同步并不意味着不可能拥有所谓的串行执行(即,如果线程一个接一个地执行)。并发的教训是你不知道如何交错线程
C. 0, 2, 4, 6, 8, 10, 12, 14,
happens for example :
例如:
- when the first thread as time to finish updating
x
before the second start - when the first thread is suspended before the first
current = x;
, the second finish executing and the first continue. - tons of other cases.
当第一个线程作为第二次启动之前完成更新x的时间
当第一个线程在第一个电流= x;之前暂停时,第二个完成执行并且第一个继续执行。
吨其他案件。
As Brian Goetz said:
正如Brian Goetz所说:
"Writing correct programs is hard; writing correct concurrent programs is harder."
“编写正确的程序很难;编写正确的并发程序更难。”
#2
0
The answer is A and C. You can eliminate D and E immediately because both threads will display 4 results, so 8 results in total, leaving only A, B & C in play.
答案是A和C.您可以立即消除D和E,因为两个线程将显示4个结果,因此总共有8个结果,只留下A,B和C。
B is eliminated because the sequence between the two 2's is too long for a single thread to have produced, so both threads must have done more work than only reaching 2.
B被消除了,因为两个2之间的序列对于单个线程来说太长了,所以两个线程必须完成更多工作而不是仅仅达到2。