【发布时间】:2015-02-28 13:40:23
【问题描述】:
问题2: 我有几乎相同的问题,但有线程 这是代码:
public class Example5 implements Runnable {
static int a=0;
public Example5() {
a++;
}
public int getA() {
return (a);
}
public void run() {
System.out.println(getA());
}
public static void main(String[] s) {
Example5 ex1 = new Example5();
new Thread(ex1).start();
Example5 ex2 = new Example5();
new Thread (ex2).start();
}
}
它告诉我它必须打印: 1 或 2 2 任何人都可以在线程中解释这一点。
例如,任何人都可以向我解释我这样做时会发生什么(与第一个问题无关)
Thread t = new Thread();
t.start();
Thread t2 = new Thread();
t2.start();
它们是按顺序工作的(就像第一个线程先工作,然后是第二个线程)还是随机运行。 谢谢
【问题讨论】:
-
Java Thread Example?的可能重复
-
不,我刚刚再次发布,因为它几乎相同,但这是另一个问题,因为它带有线程......并且工作方式不同。
-
我试图编辑它,他们告诉我你不能在已经回答的第一个问题中添加另一个问题..
标签: java multithreading compilation