Java8——“生产者-消费者”模型

问题引出:

生产者和消费者是2个不同的线程类对象,操作同一资源的情况,具体操作流程如下:

  • 生产者负责生产数据,消费者负责取走数据;
  • 生产者每生产一组数据后,消费者就要取走这组数据;

范例:数据模型

class Info {
	private String tite;
	private String content;

	public String getTite() {
		return tite;
	}

	public void setTite(String tite) {
		this.tite = tite;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

}

class Productor implements Runnable {
	private Info info;

	public Productor(Info info) {
		this.info = info;
	}

	@Override
	public void run() {
		for (int x = 0; x < 100; x++) {
			if (x % 2 == 0) { // 偶数
				this.info.setTite("罗XX");
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				this.info.setContent("大笨蛋");
			} else {
				this.info.setTite("金XX");
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				this.info.setContent("小可爱");
			}
		}

	}
}

class Customer implements Runnable {
	private Info info;

	public Customer(Info info) {
		this.info = info;
	}

	@Override
	public void run() {
		for (int x = 0; x < 200; x++) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(this.info.getTite() + "-" + this.info.getContent());
		}

	}
}

public class test {
	public static void main(String args[]) throws Exception {
		Info info = new Info();
		new Thread(new Productor(info)).start();
		new Thread(new Customer(info)).start();
	}
}

通过上述代码会发现2个严重问题:

  • 数据错位:发现不再是一个所需要的完整数据;
  • 数据重复取出,数据重复设置;

同步处理——数据错乱:

数据错乱完全是因为非同步操作造成的,所以应该使用同步处理;也就是设置和取是一个动作,应该放在一个类里面;

class Info {
	private String title;
	private String content;

	public synchronized void set(String title, String content) {
		this.title = title;
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.content = content;
	}

	public synchronized void get() {
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(this.title + "-" + this.content);
	}

}

class Productor implements Runnable {
	private Info info;

	public Productor(Info info) {
		this.info = info;
	}

	@Override
	public void run() {
		for (int x = 0; x < 100; x++) {
			if (x % 2 == 0) { // 偶数
				this.info.set("罗XX", "大笨蛋");
			} else {
				this.info.set("金XX", "小可爱");
			}
		}
	}
}

class Customer implements Runnable {
	private Info info;

	public Customer(Info info) {
		this.info = info;
	}

	@Override
	public void run() {
		for (int x = 0; x < 200; x++) {
			this.info.get();
		}
	}
}

public class test {
	public static void main(String args[]) throws Exception {
		Info info = new Info();
		new Thread(new Productor(info)).start();
		new Thread(new Customer(info)).start();
	}
}

所以数据错乱问题解决了,但是重复问题变得更严重了;

同步处理——数据重复:

生产者生产了一个数据,放入公共区,此时指示灯变为红色,表示公共区已有数字等待取走,当消费者取走公共区的数字后,指示灯变为绿色,表示生产者可以向公共区生产数据...如果想实现整个流程,需要加入等待与唤醒机制,在Object类里有专门的方法:

  • 等待:public final void wait() throws InterruptedException;
  • 唤醒第一个等待线程:public final void notify();
  • 唤醒全部线程,优先级高的先执行:public final void notifyAll();

范例:解决

class Info {
	private String title;
	private String content;
	private boolean flag = true;
	// flag = true; 表示可以生产但是不可以取走;
	// flag = false; 表示可以取走但是不可以生产;

	public synchronized void set(String title, String content) {
		// 重复进入set()方法里面,发现不可以生产,所以要等待
		if (this.flag == false) {
			try {
				super.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		this.title = title;
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.content = content;
		this.flag = false; // 修改生产标记
		super.notify(); // 唤醒其他等待线程
	}

	public synchronized void get() {
		if (this.flag == true) { // 等待生产
			try {
				super.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(this.title + "-" + this.content);
		this.flag = true; // 修改生产标记
		super.notify(); // 唤醒其他等待线程
	}

}

class Productor implements Runnable {
	private Info info;

	public Productor(Info info) {
		this.info = info;
	}

	@Override
	public void run() {
		for (int x = 0; x < 100; x++) {
			if (x % 2 == 0) { // 偶数
				this.info.set("罗XX", "大笨蛋");
			} else {
				this.info.set("金XX", "小可爱");
			}
		}
	}
}

class Customer implements Runnable {
	private Info info;

	public Customer(Info info) {
		this.info = info;
	}

	@Override
	public void run() {
		for (int x = 0; x < 200; x++) {
			this.info.get();
		}
	}
}

public class test {
	public static void main(String args[]) throws Exception {
		Info info = new Info();
		new Thread(new Productor(info)).start();
		new Thread(new Customer(info)).start();
	}
}

面试题:请解释sleep()与wait()的区别?

  • sleep()是Thread类定义的方法,wait()是Object类定义的方法;
  • sleep()可以设置休眠时间,时间一到自动唤醒,而wait()需要等待notify()进行唤醒;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值