信号量Semaphore,读写锁ReadWriteLock,计数器CountDownLatch,循环栅栏CyclicBarrier运用
1.信号量Semaphore: 像synchronized, ReentrantLock等这些对临界区资源进行同步后,所有对临界区资源进行访问的线程都得串行排队,而信号量允许指定的线程数同时进行访问 demo: import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; public class TestSemaphore implements Runnable{ private static Semaphore semaphore = new Semaphore(5); //允许同时5个线程进行访问 不会阻塞 @Override public void run(){ try { semaphore.acquire(); System.out.println(System.currentTimeMillis() +" "+ Thread.currentThread().getName()); Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); }finally { semaphore.release(