线程并发之Lock and Condition

Lock and Condition

 

Lock比传统线程模型中的synchronized方式更加面向对象,与生活中的锁类似,锁本身也应该是一个对象。两个线程执行的代码片段要实现同步互斥的效果,它们必须用同一个Lock对象。
l读写锁:分为读锁和写锁,多个读锁不互斥,读锁与写锁互斥,这是由jvm自己控制的,你只要上好相应的锁即可。如果你的代码只读数据,可以很多人同时读,但不能同时写,那就上读锁;如果你的代码修改数据,只能有一个人在写,且不能同时读取,那就上写锁。总之,读的时候上读锁,写的时候上写锁!
l在等待 Condition 时,允许发生“虚假唤醒”,这通常作为对基础平台语义的让步。对于大多数应用程序,这带来的实际影响很小,因为 Condition 应该总是在一个循环中被等待,并测试正被等待的状态声明。某个实现可以随意移除可能的虚假唤醒,但建议应用程序程序员总是假定这些虚假唤醒可能发生,因此总是在一个循环中等待。
l一个锁内部可以有多个Condition,即有多路等待和通知,可以参看jdk1.5提供的Lock与Condition实现的可阻塞队列的应用案例,从中除了要体味算法,还要体味面向对象的封装。在传统的线程机制中一个监视器对象上只能有一路等待和通知,要想实现多路等待和通知,必须嵌套使用多个同步监视器对象。(如果只用一个Condition,两个放的都在等,一旦一个放的进去了,那么它通知可能会导致另一个放接着往下走。)

 

 为什么要有读写锁:

提高读的效率,多个读可一起进行.. 读的时候不能进行写操作.,但读的时候可以有其他读操作.

 

-----------------锁的例子----------------------------------
package thread;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class LockTest {
public static void main(String[] args) {
final Business business = new Business();
ExecutorService executor =  Executors.newFixedThreadPool(3);
for(int i=0;i<3;i++)
{
executor.execute(
new Runnable()
{
public void run()
{
business.service();
}
}
);
}
executor.shutdown();
}
private static class Business
{
private int count;
Lock lock = new ReentrantLock();
public void service()
{
lock.lock();
try {
count++;
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(count);
} catch (RuntimeException e) {
e.printStackTrace();
}
finally
{
lock.unlock();
}
}
} 
}

-----------------------读写锁的例子---------------------------
注意:刚开始用eclipse for jee自己的jdk,没有看到读锁可以并发的效果,后来换成sun的jdk,就看到了效果!
 
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class ReadWriteLockTest {
public static void main(String[] args) {
final Queue3 q3 = new Queue3();
for(int i=0;i<3;i++)
{
new Thread(){
public void run(){
while(true){
q3.get();  
}
}
}.start();
}
for(int i=0;i<3;i++)
{ 
new Thread(){
public void run(){
while(true){
q3.put(new Random().nextInt(10000));
}
} 
}.start(); 
}
}
}
class Queue3{
private Object data = null;//共享数据,只能有一个线程能写该数据,但可以有多个线程同时读该数据。
private ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
public void get(){
rwl.readLock().lock();
System.out.println(Thread.currentThread().getName() + " be ready to read data!");
try {
Thread.sleep((long)(Math.random()*1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + “have read data :“ + data);
rwl.readLock().unlock();
}
public void put(Object data){
rwl.writeLock().lock();
System.out.println(Thread.currentThread().getName() + " be ready to write data!"); 
try {
Thread.sleep((long)(Math.random()*1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
this.data = data; 
System.out.println(Thread.currentThread().getName() + " have write data: “ + data);
rwl.writeLock().unlock(); 
}
}

------------------ Condition的例子1:实现两个线程交替执行-----------------------------
public class ConditionTest {
public static void main(String[] args) {
ExecutorService service = Executors.newSingleThreadExecutor();
final Business2 business = new Business2();
service.execute(new Runnable(){
public void run() {
for(int i=0;i<50;i++){
business.sub();
}
}
});
for(int i=0;i<50;i++){
business.main();
}
}
}
class Business2{
Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();
boolean bShouldSub = true;
public void sub(){
lock.lock();
if(!bShouldSub)
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
try
{
for(int i=0;i<10;i++){
System.out.println(Thread.currentThread().getName() + " : " + i);
}
bShouldSub = false;
condition.signal();
}finally{
lock.unlock();
}
}
public void main(){
lock.lock();
if(bShouldSub)
try {
condition.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} 
try
{
for(int i=0;i<5;i++){
System.out.println(Thread.currentThread().getName() + " : " + i);
}
bShouldSub = true;
condition.signal(); 
}finally{
lock.unlock();
} 
}
}

--------- Condition的例子2:实现三个线程交替运行的效果--------------------------
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class SignalTest2 {
public static void main(String[] args) {
new SignalTest2().init();
}
private void init(){
final Business b = new Business();
new Thread(){
public void run(){
for(int i=0;i<50;i++)
b.main();
}
}.start();
new Thread(){
public void run(){
for(int i=0;i<50;i++)  
b.sub();
} 
}.start();
new Thread(){
public void run(){
for(int i=0;i<50;i++)  
b.sub2();
} 
}.start(); 
}
private class Business{
int status = 1;
Lock lock = new ReentrantLock();
Condition cond1 = lock.newCondition();
Condition cond2 = lock.newCondition();
Condition cond3 = lock.newCondition(); 
public  void main(){
lock.lock();
while(status != 1){
try{cond1.await();}catch(Exception e){}
}
for(int i=1;i<=5;i++){
  try{Thread.sleep(200);}catch(Exception e){}
  System.out.println(Thread.currentThread().getName() + ":" + i);
}
status = 2;
cond2.signal();
lock.unlock();
}
public  void sub(){
lock.lock(); 
while(status != 2){
try{cond2.await();}catch(Exception e){}
}
for(int i=1;i<=10;i++){
  try{Thread.sleep(200);}catch(Exception e){}
  System.out.println(Thread.currentThread().getName() + ":" + i);
}
status = 3;
cond3.signal();
lock.unlock();
}
public  void sub2(){
lock.lock(); 
while(status != 3){
try{cond3.await();}catch(Exception e){}
}
for(int i=1;i<=10;i++){
  try{Thread.sleep(200);}catch(Exception e){}
  System.out.println(Thread.currentThread().getName() + ":" + i);
}
status = 1;
cond1.signal();
lock.unlock();
} 
}
}

 
Semaphore
Semaphore可以维护当前访问自身的线程个数,并提供了同步机制。使用Semaphore可以控制同时访问资源的线程个数,例如,实现一个文件允许的并发访问数。
单个信号量的Semaphore对象可以实现互斥锁的功能,并且可以是由一个线程获得了“锁”,再由另一个线程释放“锁”,这可应用于死锁恢复的一些场合。

 

package cn.itcast.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

public class SemaphoreTest {
 public static void main(String[] args) {
  ExecutorService service = Executors.newCachedThreadPool();
  final  Semaphore sp = new Semaphore(3);
  for(int i=0;i<10;i++){
   Runnable runnable = new Runnable(){
     public void run(){
     try {
      sp.acquire();
     } catch (InterruptedException e1) {
      e1.printStackTrace();
     }
     System.out.println("线程" + Thread.currentThread().getName() + 
       "进入,当前已有" + (3-sp.availablePermits()) + "个并发");
     try {
      Thread.sleep((long)(Math.random()*10000));
     } catch (InterruptedException e) {
      e.printStackTrace();
     }
     System.out.println("线程" + Thread.currentThread().getName() + 
       "即将离开");     
     sp.release();
     //下面代码有时候执行不准确,因为其没有和上面的代码合成原子单元
     System.out.println("线程" + Thread.currentThread().getName() + 
       "已离开,当前已有" + (3-sp.availablePermits()) + "个并发");     
    }
   };
   service.execute(runnable);   
  }

 }

}



 

CyclicBarrier
Ø表示大家彼此等待,大家集合好后才开始出发,分散活动后又在指定地点集合碰面,这就好比整个公司的人员利用周末时间集体郊游一样,先各自从家出发到公司集合后,再同时出发到公园游玩,在指定地点集合后再同时开始就餐,…。
lCountDownLatch
Ø犹如倒计时计数器,调用CountDownLatch对象的countDown方法就将计数器减1,当计数到达0时,则所有等待者或单个等待者开始执行。这直接通过代码来说明CountDownLatch的作用,这样学员的理解效果更直接。
Ø可以实现一个人(也可以是多个人)等待其他所有人都来通知他,这犹如一个计划需要多个领导都签字后才能继续向下实施。还可以实现一个人通知多个人的效果,类似裁判一声口令,运动员同时开始奔跑。用这个功能做百米赛跑的游戏程序不错哦!
lExchanger
Ø用于实现两个人之间的数据交换,每个人在完成一定的事务后想与对方交换数据,第一个先拿出数据的人将一直等待第二个人拿着数据到来时,才能彼此交换数据。

 

 

-----------------CyclicBarrier的代码:---------------------------------
package cn.itcast.day3.thread;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
public class CyclicBarrierTest {
public static void main(String[] args) {
ExecutorService service = Executors.newCachedThreadPool();
final  CyclicBarrier cb = new CyclicBarrier(3);
for(int i=0;i<3;i++){
Runnable runnable = new Runnable(){
public void run(){
try {
  Thread.sleep((long)(Math.random()*10000)); 
System.out.println("线程" + Thread.currentThread().getName() + 
"即将到达集合地点1,当前已有" + cb.getNumberWaiting() + "个已经到达,正在等候");  
cb.await();
  Thread.sleep((long)(Math.random()*10000)); 
System.out.println("线程" + Thread.currentThread().getName() + 
"即将到达集合地点2,当前已有" + cb.getNumberWaiting() + "个已经到达,正在等候");  
cb.await(); 
  Thread.sleep((long)(Math.random()*10000)); 
System.out.println("线程" + Thread.currentThread().getName() + 
"即将到达集合地点3,当前已有" + cb.getNumberWaiting() + "个已经到达,正在等候");  
cb.await();  
} catch (Exception e) {
e.printStackTrace();
}  
}
};
service.execute(runnable);
}
service.shutdown();
}
}

-----------------CountdownLatch的代码:---------------------------------
 
package cn.itcast.day3.thread;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CountdownLatchTest {
public static void main(String[] args) {
ExecutorService service = Executors.newCachedThreadPool();
final CountDownLatch cdOrder = new CountDownLatch(1);
final CountDownLatch cdAnswer = new CountDownLatch(3); 
for(int i=0;i<3;i++){
Runnable runnable = new Runnable(){
public void run(){
try {
System.out.println("线程" + Thread.currentThread().getName() + 
"正准备接受命令"); 
cdOrder.await();
System.out.println("线程" + Thread.currentThread().getName() + 
"已接受命令");  
  Thread.sleep((long)(Math.random()*10000)); 
System.out.println("线程" + Thread.currentThread().getName() + 
"回应命令处理结果"); 
cdAnswer.countDown();  
} catch (Exception e) {
e.printStackTrace();
}  
}
};
service.execute(runnable);
} 
try {
Thread.sleep((long)(Math.random()*10000));
System.out.println("线程" + Thread.currentThread().getName() + 
"即将发布命令");  
cdOrder.countDown();
System.out.println("线程" + Thread.currentThread().getName() + 
"已发送命令,正在等待结果"); 
cdAnswer.await();
System.out.println("线程" + Thread.currentThread().getName() + 
"已收到所有响应结果"); 
} catch (Exception e) {
e.printStackTrace();
} 
service.shutdown();
}
}

---------------------------ExchangerTest-------------------------
讲解Exchanger的比喻:好比两个毒贩要进行交易,一手交钱、一手交货,不管谁先来到接头地点后,就处于等待状态了,当另外一方也到达了接头地点(所谓到达接头地点,也就是到到达了准备接头的状态)时,两者的数据就立即交换了,然后就又可以各忙各的了。
exchange方法就相当于两手高高举着待交换物,等待人家前来交换,一旦人家到来(即人家也执行到exchange方法),则两者立马完成数据的交换。
 
package cn.itcast.day3.thread;
import java.util.concurrent.Exchanger;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ExchangerTest {
public static void main(String[] args) {
ExecutorService service = Executors.newCachedThreadPool();
final Exchanger exchanger = new Exchanger();
service.execute(new Runnable(){
public void run() {
try { 
Thread.sleep((long)(Math.random()*10000));
String data1 = "zxx";
System.out.println("线程" + Thread.currentThread().getName() + 
"正在把数据" + data1 +"换出去");
String data2 = (String)exchanger.exchange(data1);
System.out.println("线程" + Thread.currentThread().getName() + 
"换回的数据为" + data2);
}catch(Exception e){
}
} 
});
service.execute(new Runnable(){
public void run() {
try { 
Thread.sleep((long)(Math.random()*10000));
String data1 = "lhm";
System.out.println("线程" + Thread.currentThread().getName() + 
"正在把数据" + data1 +"换出去");
String data2 = (String)exchanger.exchange(data1);
System.out.println("线程" + Thread.currentThread().getName() + 
"换回的数据为" + data2);
}catch(Exception e){
} 
} 
}); 
}
}

 
Java5中提供了如下一些同步集合类:
Ø通过看java.util.concurrent包下的介绍可以知道有哪些并发集合
ØConcurrentHashMap
ØCopyOnWriteArrayList
ØCopyOnWriteArraySet

 
 
 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值