java线程协作,经典生产者/消费者模式(一、synchronized互斥)

java多线程协作,最经典的就是生产者/消费者模式,,比如,一个盘子(缓冲区),假如这个盘子最多只能放一个苹果。现在如果我要从盘里拿出一个苹果,首先要保证盘里有苹果,如果没有,就要等到放入了才能拿;同样,我如果要放一个苹果到盘子里,就要保证盘子里面现在没有苹果,如果有,就要等拿走了又才能放。下面贴上实现放入苹果/拿出苹果这个过程的线程代码:

/**
 * java线程协作,生产者/消费者
 * @author 李小拐
 * 2016年6月20日20:36:56
 */
public class Plate {
	
	private static int plate=0;//声明一个变量,代表盘子,可以放入和拿出
	//放入一个苹果,使用synchronized关键字
	public synchronized void putApple() throws InterruptedException {
		while (plate==1) {
			this.wait();//如果有苹果,不能再放了,就等待
		}
		System.out.println("放入apple");
		plate=1;
		this.notifyAll();//放入后,唤醒等待的线程
	}
	//拿出一个苹果,使用synchronized关键字
	public synchronized void getApple() throws InterruptedException {
		while (plate==0) {
			this.wait();//如果盘子为空,则不能取,等待放入苹果才能取
		}
		System.out.println("拿出苹果");
		plate=0;
		this.notifyAll();//取出后,唤醒等待线程
	}
	
	public static void main(String[] args) {
		Plate plate=new Plate();
		for (int i = 0; i < 200; i++) {//put和get各执行20次
			new getApple(plate).start();
			new putApple(plate).start();
		}
	}
}
//拿出苹果的线程
class getApple extends Thread{
	private Plate plate;
	public getApple(Plate plate) {
		this.plate=plate;
	}
	@Override
	public void run() {
		try {
			plate.getApple();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

//放入苹果的线程
class putApple extends Thread{
	private Plate plate;
	public putApple(Plate plate) {
		this.plate=plate;
	}
	@Override
	public void run() {
		try {
			plate.putApple();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}


以上这个列子就是经典的生产者/消费者模式,如果有不正确的地方,请看见的大神指出来,以免误导了后面学习的人。


下面,在贴一段经典面试题的代码:大致需求如下,开启ABC三个线程,A打印A,B打印B,C打印C,ABC三个线程同时启动,顺序输入ABC,如此循环10次。


/**
 * 生产者/消费者 :顺序打印ABCD
 * @author 李小拐
 * 2016年6月20日21:00:36
 */
public class PrintABCD {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//一定是在这里new一个对象,不能再线程里面new,否则多个线程就不是操作的同一个内存对象
		PrintABCD abcd=new PrintABCD();
		
		for (int i = 0; i < 10; i++) {
			(new printA(abcd)).start();
			(new printB(abcd)).start();
			(new printC(abcd)).start();
			(new printD(abcd)).start();
		}
	}

	private static int who=1;//用变量who来控制要打印的值;1,2,3,4分别代表A,B,C,D
	
	public synchronized void printA() throws InterruptedException {
		while (who!=1) {
			this.wait();//不等于1就等待,等于1的时候才执行
		}
		System.out.println("A");
		who=2;//A的下一状态B
		this.notifyAll();
	}
	
	public synchronized void printB() throws InterruptedException {
		while (who!=2) {
			this.wait();
		}
		System.out.println("--B");
		who=3;//B的下一状态C
		this.notifyAll();
	}
	
	public synchronized void printC() throws InterruptedException {
		while (who!=3) {
			this.wait();
		}
		System.out.println("----C");
		who=4;//C的下一状态D
		this.notifyAll();
	}
	
	public synchronized void printD() throws InterruptedException {
		while (who!=4) {
			this.wait();
		}
		System.out.println("------D");
		who=1;//D的下一状态A
		this.notifyAll();
	}
}
//四个线程,分别打印ABCD
class printA extends Thread{
	PrintABCD p;
	printA(PrintABCD p){
		this.p=p;
	}
	@Override
	public void run() {
		try {
			p.printA();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
class printB extends Thread{
	PrintABCD p;
	printB(PrintABCD p){
		this.p=p;
	}
	@Override
	public void run() {
		try {
			p.printB();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
class printC extends Thread{
	PrintABCD p;
	printC(PrintABCD p){
		this.p=p;
	}
	@Override
	public void run() {
		try {
			p.printC();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
class printD extends Thread{
	PrintABCD p;
	printD(PrintABCD p){
		this.p=p;
	}
	@Override
	public void run() {
		try {
			p.printD();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

这里打印了ABCD,其实和ABC是一样的,只是demo里面多写了一个。synchronized关键字保证了线程的互斥。JDK1.5以前基本都使用synchronized关键字进行线程间协作,JDK1.5以后提供了更高级的通信方式,我将在下一篇文章贴出demo代码。


最后一句,学无止境,文章若有不足之处,请大神们及时指正,,纠正了我,也及时避免误导后人


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值