1:Lock 比 synchronized 更加面对对象,lock可实现多线程之间的互斥关系,使得共享数据不会被污染;实现是其子类ReentrantLock
2:读写锁是Lock的是一个子类, 分为读锁和写锁,可实现多个读锁不互斥,多个写锁之间实现互斥
一:Lock
package com.xiaoniu.asset.service.asset; import sun.org.mozilla.javascript.internal.Synchronizer; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * Created by YALI on 2017/5/29. */ public class SynchTest { public static void main(String[] args){ new SynchTest().init(); } private void init(){ final Book outer=new Book(); new Thread(new Runnable() { @Override public void run() { while (true) { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } outer.threadTest("linzeyang"); } } }).start(); new Thread(new Runnable() { @Override public void run() { while (true) { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } outer.threadTest("wangyali"); } } }).start(); } class Book{ Lock lock=new ReentrantLock(); public void threadTest(String name){ lock.lock(); try { int len = name.length(); for (int i = 0; i < len; i++) { System.out.print(name.charAt(i)); } System.out.println(); }finally { lock.unlock(); } } } }
二:读写锁
ReadWriteLock是Lock的一个子类,分为读锁和写锁,读锁不进行互斥,写锁是互斥的关系
package com.xiaoniu.asset.service.asset; import java.util.Random; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; /** * Created by YALI on 2017/6/15. */ public class ReadWriteLockTest { public static void main(String[] args){ final A a=new A(); for(int i=0;i<3;i++){ new Thread(){ @Override public void run(){ a.getData(); } }.start(); new Thread(){ @Override public void run(){ a.putData(new Random().nextInt(1000)); } }.start(); } } static class A{ private int data=0;//共享数据 ReadWriteLock lock=new ReentrantReadWriteLock(); public void getData(){ lock.readLock().lock(); try { System.out.println(Thread.currentThread().getName() + " begin read data"); try { Thread.sleep((long) (Math.random()*1000)); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " has finish read:" + data); }finally { lock.readLock().unlock(); } } public void putData(int data){ lock.writeLock().lock(); try { System.out.println(Thread.currentThread().getName() + " begin write data:" + data); this.data = data; try { Thread.sleep((long) (Math.random()*1000)); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " has finish write!"); }finally { lock.writeLock().unlock(); } } } }
三:缓存系统
实现读写互斥
package com.xiaoniu.asset.service.asset; import java.util.HashMap; import java.util.Map; import java.util.Random; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; /** * Created by YALI on 2017/6/15. */ public class CacheTest { static ReadWriteLock lock=new ReentrantReadWriteLock(); static Map<String,Integer> cacheMap=new HashMap<String,Integer>(); public static void main(String[] args){ for(int i=0;i<10;i++){ new Thread(){ @Override public void run(){ System.out.println( getData()); } }.start(); } } public static int getData(){ lock.readLock().lock(); Integer data; try { data = cacheMap.get("data"); if (data == null) { lock.readLock().unlock(); lock.writeLock().lock(); try { if(data==null) { data = new Random().nextInt()*1000; } cacheMap.put("data", data); }finally { lock.writeLock().unlock(); } lock.readLock().lock(); } }finally { lock.readLock().unlock(); } return data; } }
四 condition
conidtion类似于传统技术中的await 和notity,在多线程中,当有一个处于等待状态的时候,通过满足一定的condition去进行虚拟的唤醒,达到现场间的通讯作用
package com.xiaoniu.asset.service.asset; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * Created by YALI on 2017/6/5. */ public class Condition { public static void main(String[] args){ final Business b=new Business(); new Thread(new Runnable() { @Override public void run() { for(int i=0;i<5;i++){ b.sub(i); } } }).start(); for(int i=0;i<30;i++){ b.main(i); } } static class Business{ int flag=1; Lock lock=new ReentrantLock(); java.util.concurrent.locks.Condition condition1=lock.newCondition(); java.util.concurrent.locks.Condition condition2=lock.newCondition(); public void sub(int i){ lock.lock(); try { while (flag!=1){ try { condition1.await(); } catch (InterruptedException e) { e.printStackTrace(); } } for(int j=0;j<10;j++) { System.out.println("sub thread --" + j+"--look for --"+i); } flag=2; condition2.signal(); }finally { lock.unlock(); } } public void main(int i){ lock.lock(); try { while (flag!=2){ try { condition2.await(); } catch (InterruptedException e) { e.printStackTrace(); } } for(int j=0;j<10;j++) { System.out.println("main thread --"+ j+"--look for --"+i); } flag=1; condition1.signal(); }finally { lock.unlock(); } } } }