线程竞争
1/**
2 *Author: wangy325
3 *Date: 2024-08-07 20:00:13
4 *Description: 线程争用
5**/
6public class Threading {
7
8 public static void main(String[] args) {
9 for (int i = 0; i < 100; i++) {
10 MyThread t = new MyThread();
11 t.start();
12 }
13 // uncertain output~
14 System.out.printf("shardInt: %d\n", MyThread.shardInt);
15 }
16}
17
18class MyThread extends Thread {
19 public static int shardInt = 0;
20 @Override
21 public void run() {
22 shardInt++;
23 try {
24 TimeUnit.SECONDS.sleep(1);
25 } catch (InterruptedException e) {
26 e.printStackTrace();
27 }
28 }
29}