3 synchronized和Lcok+Condition对比续

package Test;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/*
 *  三个线程一个打印1次,一个打印2次,一个打印三次;循环5次 
 * 
 *  lock阻塞和synchronized关键字一样,但利用condition可以控制唤醒哪个线程
 *  但还是不能没有标志位控制线程的运行顺序,唯一的好处是不用notifyAll
 * */
public class Ex2 {
	public static void main(String args[]){
		final Solution2 s = new Solution2();
		new Thread(new Runnable(){
			public void run(){
				for(int i=0;i<5;i++)
					s.thread1();
			}
		}).start();
		new Thread(new Runnable(){
			public void run(){
				for(int i=0;i<5;i++)
					s.thread2();
			}
		}).start();
		new Thread(new Runnable(){
			public void run(){
				for(int i=0;i<5;i++)
					s.thread3();
			}
		}).start();
	}
	
}




class Solution1{
	int turn = 1;
	public synchronized void thread1(){
		while(turn!=1){
			try{
				this.wait();
			}catch(InterruptedException e){}
		}
		
		System.out.println(Thread.currentThread().getName()+"第1次");
		turn = 2;
		this.notifyAll();
	}
	public synchronized void thread2(){
		while(turn!=2){
			try{
				this.wait();
			}catch(InterruptedException e){}
		}
		for(int i=1;i<=2;i++)
			System.out.println(Thread.currentThread().getName()+"第"+i+"次");
		turn = 3;
		this.notifyAll();
	}
	public synchronized void thread3(){
		while(turn!=3){
			try{
				this.wait();
			}catch(InterruptedException e){}
		}
		for(int i=1;i<=3;i++)
			System.out.println(Thread.currentThread().getName()+"第"+i+"次");
		turn = 1;
		this.notifyAll();
	}
	
}


class Solution2{
	Lock lock = new ReentrantLock();
	Condition c1 = lock.newCondition();
	Condition c2 = lock.newCondition();
	Condition c3 = lock.newCondition();
	int turn = 1;
	public void thread1(){
		lock.lock();
		try{
			try {
				while(turn!=1)
					c1.await();
			} catch (InterruptedException e) {}
			System.out.println(Thread.currentThread().getName()+"第1次");
			turn = 2;
			
			c2.signal();
			try {
				c1.await();
			} catch (InterruptedException e) {}
		}finally{
			lock.unlock();
		}
	}
	public void thread2(){
		lock.lock();
		try{
			while(turn!=2)
				try{
					c2.await();
				}catch(InterruptedException e){}
			for(int i=0;i<2;i++)
			{
				System.out.println(Thread.currentThread().getName()+"第"+i+"次");
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {	}
			}
			turn = 3;
			c3.signal();
		}finally{
			lock.unlock();
		}
	}
	
	public void thread3(){
		lock.lock();
		try{
			while(turn!=3)
				try{
					c3.await();
				}catch(InterruptedException e){}
			for(int i=0;i<3;i++)
			{
				System.out.println(Thread.currentThread().getName()+"第"+i+"次");
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {}
			}
				
			turn = 1;
			c1.signal();
		}finally{lock.unlock();}
	}
	
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值