一、实现生产者和消费者模式:一对一交替打印
package com.it.po.thread11; import java.util.Locale; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class MyService3 { private ReentrantLock lock =new ReentrantLock(); private Condition condition= lock.newCondition(); private Boolean istrue=false; public void setVale(){ try { lock.lock(); while (istrue==true){ condition.await(); } System.out.println("set..."); istrue=true; condition.signal(); } catch (InterruptedException e) { e.printStackTrace(); }finally { lock.unlock(); } } public void getVale(){ try { lock.lock(); while (istrue==false){ condition.await(); } System.out.println("get..."); istrue=false; condition.signal(); } catch (InterruptedException e) { e.printStackTrace(); }finally { lock.unlock(); } } }

package com.it.po.thread11; public class Run7 { public static void main(String[] args) throws InterruptedException { MyService3 myService3 = new MyService3(); MyThread7_1 my1 = new MyThread7_1(myService3); MyThread7_2 my2 = new MyThread7_2(myService3); my1.start(); my2.start(); } }
get...
set...
get...
set...
get...
set...
get...
set...
get...
set...
继续。。。。
二、实现生产者和消费者模式:多对多
package com.it.po.thread11; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; public class MyService4 { private ReentrantLock lock =new ReentrantLock(); private Condition condition= lock.newCondition(); private Boolean istrue=false; public void setVale(){ try { lock.lock(); while (istrue==true){ System.out.println("有可能连续等set。。。"); condition.await(); } System.out.println("set..."); istrue=true; condition.signal(); } catch (InterruptedException e) { e.printStackTrace(); }finally { lock.unlock(); } } public void getVale(){ try { lock.lock(); while (istrue==false){ System.out.println("有可能连续等get。。。"); condition.await(); } System.out.println("get..."); istrue=false; condition.signal(); } catch (InterruptedException e) { e.printStackTrace(); }finally { lock.unlock(); } } }

package com.it.po.thread11; public class Run8 { public static void main(String[] args) throws InterruptedException { MyService4 myService = new MyService4(); MyThread8_1[] my1 = new MyThread8_1[10]; MyThread8_2[] my2 = new MyThread8_2[10]; for(int i=0;i<10;i++){ my1[i]= new MyThread8_1(myService); my2[i]= new MyThread8_2(myService); my1[i].start(); my2[i].start(); } } }

set...
get...
有可能连续等get。。。
set...
有可能连续等set。。。
get...
有可能连续等get。。。
有可能连续等get。。。
有可能连续等get。。。
set...
有可能连续等set。。。
有可能连续等set。。。
有可能连续等set。。。
get...
有可能连续等get。。。
有可能连续等get。。。
set...
get...
有可能连续等get。。。
有可能连续等get。。。
有可能连续等get。。。
set...
有可能连续等set。。。
get...
set...
有可能连续等set。。。
get...
有可能连续等get。。。
set...
有可能连续等set。。。
get...
有可能连续等get。。。
程序假死
将signal 改为signalAll
package com.it.po.thread11; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; public class MyService4 { private ReentrantLock lock =new ReentrantLock(); private Condition condition= lock.newCondition(); private Boolean istrue=false; public void setVale(){ try { lock.lock(); while (istrue==true){ System.out.println("有可能连续等set。。。"); condition.await(); } System.out.println("set..."); istrue=true; condition.signalAll(); } catch (InterruptedException e) { e.printStackTrace(); }finally { lock.unlock(); } } public void getVale(){ try { lock.lock(); while (istrue==false){ System.out.println("有可能连续等get。。。"); condition.await(); } System.out.println("get..."); istrue=false; condition.signalAll(); } catch (InterruptedException e) { e.printStackTrace(); }finally { lock.unlock(); } } }
set...
get...
set...
get...
set...
get...
set...
有可能连续等set。。。
get...
有可能连续等get。。。
set...
get...
set...
有可能连续等set。。。
get...
set...
get...
set...
get...
有可能连续等get。。。
set...
有可能连续等set。。。
get...
set...
get...
注意:
signalAll 唤醒的可能是同类。
三、公平锁和非公平锁
锁Lock分为公平锁以及非公平锁,
公平锁就是线程获取锁的顺序是按照线程
加锁的顺序来分配的,即先来先得的FIFO先进先出顺序。
非公平锁:获取锁的抢占机制,是随机获取的。
(一)、公平锁
package com.it.po.thread11; import com.sun.org.apache.xpath.internal.operations.Bool; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; public class MyService5 { private ReentrantLock lock =null; public MyService5(Boolean isFair) { lock = new ReentrantLock(isFair); } public void methodA(){ try { lock.lock(); System.out.println("线程 "+Thread.currentThread().getName()+" 获得锁"); }finally { lock.unlock(); } } }
package com.it.po.thread11; public class Run9 { public static void main(String[] args) throws InterruptedException { MyService5 myService = new MyService5(true); Runnable runnable = new Runnable() { @Override public void run() { System.out.println("**线程 "+Thread.currentThread().getName()+" 运行了"); myService.methodA(); } }; Thread[] threads = new Thread[15]; for(int i=0;i<15;i++){ threads[i]=new Thread(runnable); threads[i].setName(""+(i+1)); } for(int i=0;i<15;i++){ threads[i].start(); } } }
**线程 2 运行了 线程 2 获得锁 **线程 3 运行了 线程 3 获得锁 **线程 4 运行了 线程 4 获得锁 **线程 1 运行了 线程 1 获得锁 **线程 5 运行了 线程 5 获得锁 **线程 7 运行了 线程 7 获得锁 **线程 9 运行了 线程 9 获得锁 **线程 6 运行了 线程 6 获得锁 **线程 10 运行了 线程 10 获得锁 **线程 11 运行了 线程 11 获得锁 **线程 8 运行了 线程 8 获得锁 **线程 12 运行了 线程 12 获得锁 **线程 14 运行了 线程 14 获得锁 **线程 13 运行了 线程 13 获得锁 **线程 15 运行了 线程 15 获得锁
注意:公平锁其实也是基本公平,打印基本是有序的状态。
(一)、非公平锁
package com.it.po.thread11; public class Run9 { public static void main(String[] args) throws InterruptedException { MyService5 myService = new MyService5(false); Runnable runnable = new Runnable() { @Override public void run() { System.out.println("**线程 "+Thread.currentThread().getName()+" 运行了"); myService.methodA(); } }; Thread[] threads = new Thread[15]; for(int i=0;i<15;i++){ threads[i]=new Thread(runnable); threads[i].setName(""+(i+1)); } for(int i=0;i<15;i++){ threads[i].start(); } } }
**线程 2 运行了 **线程 1 运行了 线程 2 获得锁 线程 1 获得锁 **线程 3 运行了 线程 3 获得锁 **线程 5 运行了 线程 5 获得锁 **线程 6 运行了 线程 6 获得锁 **线程 7 运行了 线程 7 获得锁 **线程 9 运行了 线程 9 获得锁 **线程 10 运行了 线程 10 获得锁 **线程 11 运行了 **线程 15 运行了 **线程 14 运行了 线程 11 获得锁 **线程 13 运行了 线程 13 获得锁 线程 15 获得锁 线程 14 获得锁 **线程 4 运行了 线程 4 获得锁 **线程 12 运行了 线程 12 获得锁 **线程 8 运行了 线程 8 获得锁
结果基本是乱序的。
浙公网安备 33010602011771号