并发数值增加范例
并发情况下数据增加的一个范例
package com.example.demo; import java.util.ArrayList; public class TestThread implements Runnable { static Integer i = 0; static TestThread instance = new TestThread(); public static void main(String[] args) throws InterruptedException { Thread t1 = new Thread(new TestThread()); Thread t2 = new Thread(new TestThread()); t1.start(); t2.start(); t1.join(); t2.join(); System.out.println(i); } @Override public void run() { for(int j=0;j<100000;j++){ synchronized (instance){ i++; } } } }