package thead.safe.safe0;

import lombok.Getter;

@Getter
public class SafeCounter {
    private static Integer count = 0;

    // 使用 synchronized 关键字
    public static void main(String[] args) throws InterruptedException {
        Thread[] threads = new Thread[1000];
        for (int i = 0; i < 1000; i++) {
            threads[i] = new Thread(() -> {
                for (int j = 0; j < 1000; j++) {
                    synchronized ("") {
                        count++;
                    }
                }
            });
            threads[i].start();
        }

        // 等待所有线程完成
        for (int i = 0; i < 1000; i++) {
            threads[i].join();
        }

        // 输出最终计数值
        System.out.println("Count: " + count);  // 结果应该等于 1000000
    }
}
