thread1 has 5 tickets thread3 has 5 tickets thread2 has 5 tickets thread3 has 4 tickets thread1 has 4 tickets thread3 has 3 tickets thread2 has 4 tickets thread3 has 2 tickets thread1 has 3 tickets thread3 has 1 tickets thread2 has 3 tickets thread1 has 2 tickets thread2 has 2 tickets thread1 has 1 tickets thread2 has 1 tickets
可见输出是乱序的。然而调用 run() 方法之后,输出变成了这样:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
thread1 has 5 tickets thread1 has 4 tickets thread1 has 3 tickets thread1 has 2 tickets thread1 has 1 tickets thread2 has 5 tickets thread2 has 4 tickets thread2 has 3 tickets thread2 has 2 tickets thread2 has 1 tickets thread3 has 5 tickets thread3 has 4 tickets thread3 has 3 tickets thread3 has 2 tickets thread3 has 1 tickets
Thread-0 has 20 tickets Thread-2 has 19 tickets Thread-1 has 19 tickets Thread-2 has 17 tickets Thread-0 has 18 tickets Thread-2 has 15 tickets Thread-1 has 16 tickets Thread-2 has 13 tickets Thread-0 has 14 tickets Thread-2 has 11 tickets Thread-1 has 12 tickets Thread-0 has 10 tickets Thread-2 has 9 tickets Thread-0 has 7 tickets Thread-1 has 8 tickets Thread-0 has 5 tickets Thread-2 has 6 tickets Thread-0 has 3 tickets Thread-1 has 4 tickets Thread-0 has 1 tickets Thread-2 has 2 tickets
Thread-0 started. Thread-0 has 20 tickets Thread-0 has 19 tickets Thread-0 has 18 tickets Thread-0 has 17 tickets Thread-0 has 16 tickets Thread-0 has 15 tickets Thread-0 has 14 tickets Thread-0 has 13 tickets Thread-0 has 12 tickets Thread-0 has 11 tickets Thread-0 has 10 tickets Thread-0 has 9 tickets Thread-0 has 8 tickets Thread-0 has 7 tickets Thread-0 has 6 tickets Thread-0 has 5 tickets Thread-0 has 4 tickets Thread-0 has 3 tickets Thread-0 has 2 tickets Thread-0 has 1 tickets Thread-0 stopped. Thread-2 started. Thread-2 stopped. Thread-1 started. Thread-1 stopped.
Thread-0 started. Thread-2 started. Thread-1 started. Thread-0 has 20 tickets Thread-2 has 19 tickets Thread-1 has 18 tickets Thread-0 has 17 tickets Thread-1 has 16 tickets Thread-2 has 15 tickets Thread-0 has 14 tickets Thread-1 has 13 tickets Thread-2 has 12 tickets Thread-1 has 11 tickets Thread-0 has 10 tickets Thread-2 has 9 tickets Thread-1 has 8 tickets Thread-0 has 7 tickets Thread-2 has 6 tickets Thread-2 has 5 tickets Thread-0 has 4 tickets Thread-1 has 3 tickets Thread-2 has 2 tickets Thread-1 has 1 tickets Thread-0 stopped. Thread-2 stopped. Thread-1 stopped.
Thread-1 started. Thread-2 started. Thread-0 started. Thread-1 has 19 tickets Thread-0 has 18 tickets Thread-2 has 20 tickets Thread-1 has 17 tickets Thread-0 has 16 tickets Thread-2 has 15 tickets Thread-1 has 13 tickets Thread-2 has 12 tickets Thread-0 has 14 tickets Thread-0 has 11 tickets Thread-2 has 10 tickets Thread-1 has 9 tickets Thread-1 has 8 tickets Thread-0 has 6 tickets Thread-2 has 7 tickets Thread-2 has 4 tickets Thread-0 has 3 tickets Thread-1 has 5 tickets Thread-2 has 2 tickets Thread-0 has 1 tickets Thread-1 stopped. Thread-2 stopped. Thread-0 stopped.