Java线程输出

时间:2017-04-09 01:54:54

标签: java multithreading

我需要知道此代码的输出:

  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(); 
        } 
       }

1 个答案:

答案 0 :(得分:0)

它将为每个线程打印到控制台:

1 1

2 2

3 3

4 4

5 5

6 6

7 7

8 8

9 9

10 10

消息将在每个线程的消息之间混淆,因为它们同时运行。