java 的多线程共享问题
package com.public class SaleTicket2 implements Runnable{
private int i = 1;
public void run(){
while(i<=20){
System.out.println(i + " ");
i++;
Thread.yield();
}
}
}
package com.
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SaleTicket2 st = new SaleTicket2();
Thread t1 = new Thread(st);
Thread t2 = new Thread(st);
Thread t3 = new Thread(st);
t1.start();
t2.start();
t3.start();
}
}
输出:
1
2
3
4
5
4
6
6
8
7
11
10
9
13
12
15
14
17
16
19
18
20
为什么不是按顺序的 1,2,3,4.....20,而是有些顺序乱了,有些重复的了?