synchronized的功能扩展 重入锁
java.util.concurrent.locks.ReentrantLock
package com.longfor.dragonshard.service.cost.standard.impl;import java.util.concurrent.locks.ReentrantLock;public class ReenterLock implements Runnable { public static ReentrantLock reentrantLock = new ReentrantLock(); public static int i=0; @Override public void run() { for (int j=0;j<10000;j++){ reentrantLock.lock(); try { i++; }finally { reentrantLock.unlock(); } } } public static void main(String[] args) throws InterruptedException{ Thread t1 = new Thread(new ReenterLock()); Thread t2 = new Thread(new ReenterLock()); t1.start(); t2.start(); t1.join(); t2.join(); System.out.println(i); }}
使用读写锁 读读非阻塞 写写 写读 读写 阻塞
当一个程序读大于写 那么读写锁效率最高
package com.longfor.dragonshard.service.cost.standard.impl;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReadWriteLock;import java.util.concurrent.locks.ReentrantLock;import java.util.concurrent.locks.ReentrantReadWriteLock;public class ReadWriteLockDemo{ private static ReentrantLock reentrantLock = new ReentrantLock(); private static ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock(); private static Lock readLock = readWriteLock.readLock(); private static Lock writeLock = readWriteLock.writeLock(); private int value; public Object handleRead(Lock lock) throws InterruptedException{ try { lock.lock(); Thread.sleep(1000); System.out.println(Thread.currentThread().getName()+"read has done"); return value; }finally { lock.unlock(); } } public void handleWrite(Lock lock,int value)throws InterruptedException{ try { lock.lock(); Thread.sleep(1000); System.out.println(Thread.currentThread().getName()+"write has done"); this.value=value; }finally { lock.unlock(); } } public static void main(String[] args) throws InterruptedException{ ReadWriteLockDemo readWriteLockDemo = new ReadWriteLockDemo(); Runnable readRunnable = new Runnable(){ @Override public void run() { try { //readWriteLockDemo.handleRead(readLock); readWriteLockDemo.handleRead(reentrantLock); }catch (InterruptedException e){ e.printStackTrace(); } } }; Runnable writeRunnable = new Runnable(){ @Override public void run() { try { //readWriteLockDemo.handleWrite(writeLock,2); readWriteLockDemo.handleWrite(reentrantLock,2); }catch(InterruptedException e ){ e.printStackTrace(); } } }; long startTime = System.currentTimeMillis(); for(int i=0;i<18;i++){ new Thread(readRunnable).start(); } for(int i=18;i<20;i++){ new Thread(writeRunnable).start(); } long endTime = System.currentTimeMillis(); System.out.println("the program is running for :"+(endTime-startTime)+"mill seconds ..."); }}
倒计时器 CountDownLatch 多线程控制工具类 意为:门闩 就是检查所有程序完最后执行 每次执行完程序 countDown() 最后要求所有程序检查完毕 await()
package com.longfor.dragonshard.service.cost.standard.impl;import java.util.Random;import java.util.concurrent.CountDownLatch;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class CountDownLatchTest implements Runnable{ private static CountDownLatch countDownLatch = new CountDownLatch(10); private static CountDownLatchTest countDownLatchTest = new CountDownLatchTest(); @Override public void run() { try { Thread.sleep(new Random().nextInt(10)*1000); System.out.println("check complete"); countDownLatch.countDown(); }catch (InterruptedException e){ e.printStackTrace(); } } public static void main(String[] args) throws InterruptedException{ ExecutorService executorService = Executors.newFixedThreadPool(10); for(int i=0;i<10;i++){ executorService.submit(countDownLatchTest); } countDownLatch.await(); System.out.println("Fire!"); executorService.shutdown(); }}