并发重入锁,reentrantLock
并发重入锁集合增加
package com.example.demo; import java.util.ArrayList; import java.util.concurrent.locks.ReentrantLock; public class TestThread implements Runnable { static Integer i = 0; public static ReentrantLock lock = new ReentrantLock(); //static TestThread instance = new TestThread(); public static void main(String[] args) throws InterruptedException { //ReenterLock rl = new Reenre 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++){ try { lock.lock(); i++; }finally { lock.unlock(); } /*synchronized (instance){ }*/ } } }