我需要知道此代码的输出:
class s1 implements Runnable
{
int x = 0, y = 0;
int addX() {x++; return x;}
int addY() {y++; return y;}
public void run() {
for(int i = 0; i < 10; i++)
System.out.println(addX() + " " + addY());
}
public static void main(String args[])
{
s1 run1 = new s1();
s1 run2 = new s1();
Thread t1 = new Thread(run1);
Thread t2 = new Thread(run2);
t1.start();
t2.start();
}
}
答案 0 :(得分:0)
它将为每个线程打印到控制台:
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
消息将在每个线程的消息之间混淆,因为它们同时运行。