1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| public class Test implements Runnable {
public static int n = 0; public static synchronized void increase() { n++; } @Override public void run() { for (int i = 0; i < 100000; i++) { increase(); } }
public static void main(String[] args) throws InterruptedException { Test test = new Test(); Thread thread1 = new Thread(new Test()); Thread thread2 = new Thread(new Test());
thread1.start(); thread2.start(); thread1.join(); thread2.join();
System.out.println(n); }
|