【发布时间】:2017-10-06 00:54:09
【问题描述】:
最近在我的一次采访中,我遇到了一个关于多线程的问题,这让我很困惑,所以为了澄清我的概念,我问了这个问题。
问题是“是否有任何可能的执行场景可以为我提供以下代码的输出,例如 7 6 5 或 7 5 6”
public class SampleThread implements Runnable {
static int x = 4;
public void run() {
x++;
System.out.println(x);
}
}
现在假设有 3 个线程实例具有与目标对象相同的可运行实例,如下所示
public class Test {
public static void main(String[] args) {
SampleThread s1=new SampleThread();
Thread t1=new Thread(s1);
Thread t2=new Thread(s1);
Thread t3=new Thread(s1);
t1.start();
t2.start();
t3.start();
}}
【问题讨论】:
-
为什么这个问题被否决了?我已经给出了明确的问题描述,理解线程概念非常有用。所以请不要投反对票,而是尝试给出正确的答案...
标签: java multithreading