JAVA多线程—生产者与消费者模式

【TX】生产者与消费者是JAVA多线程中比较经典的案例,生产者不断生产,消费者不断取出生产者生产的产品。

然后这一案例存在两点问题:

A:假设生产者线程刚向数据存储空间添加了信息的名称,还没有加入该信息的内容,程序就切换到了消费者线程,消费者线程将把信息的名称和上一个信息的内容联系到了一起。

B:生产者放了若干次的数据,消费者才开始取出数据,或者是消费者取完一个数据后,还没等到生产者放入新的数据,又重复取出已取过的数据。

1:先定义一个商品类:

package com.tmx.Threads;

public class Product {
	private String name;
	private String color;
	private Double price;
	
	public Product() {
		super();
	}

	public Product(String name, String color, Double price) {
		super();
		this.name = name;
		this.color = color;
		this.price = price;
	}
        ..................省略getset方法..................
	@Override
	public String toString() {
		return "Product [name=" + name + ", color=" + color + ", price=" + price + "]";
	}
	
}

2:定义生产者和消费者类,由于这两者要操作同一个资源,所以两者均实现Runnable接口

生产者类:

public class Producer implements Runnable{
	private Product product;
	
	public Producer(Product product) {
		super();
		this.product = product;
	}

	@Override
	public void run() {
		boolean flag=false;
		for (int i = 0; i < 10; i++) {
			if(flag){
				this.product.setName("桌子");
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				this.product.setColor("黑色");
				this.product.setPrice(20.00);
				flag=false;
			}else{
				this.product.setName("纯净水");
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				this.product.setColor("无色");
				this.product.setPrice(1.00);
				flag=true;
			}
		}
	}
}

消费者类:

public class Consumer implements Runnable {
	private Product product;
	
	public Consumer(Product product) {
		super();
		this.product = product;
	}
	@Override
	public void run() {
		for (int i = 0; i < 10; i++) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(product);
		}
	}
}

测试类

public class ThreadTest2 {
	public static void main(String[] args) {
		Product product=new Product();
		Producer producer=new Producer(product);
		Consumer consumer=new Consumer(product);
		Thread thPro = new Thread(producer);
		Thread thCon = new Thread(consumer);
		thPro.start();
		thCon.start();
	}
}

输出结果:

Product [name=桌子, color=无色, price=1.0]
Product [name=桌子, color=黑色, price=20.0]
Product [name=纯净水, color=黑色, price=1.0]
Product [name=纯净水, color=黑色, price=20.0]
Product [name=纯净水, color=黑色, price=20.0]
Product [name=纯净水, color=黑色, price=20.0]
Product [name=桌子, color=无色, price=1.0]
Product [name=纯净水, color=黑色, price=20.0]
Product [name=纯净水, color=黑色, price=20.0]
Product [name=桌子, color=黑色, price=20.0]

现在先解决第一个问题 ,商品名称和内容不相符的问题

我们对Product类的属性加上同步处理:

1_1:商品类

public class Product {
	private String name;
	private String color;
	private Double price;
	
	public Product() {
		super();
	}

	public synchronized void setName(String name, String color, Double price) {
		this.name = name;
		try {
			Thread.sleep(10);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		this.color = color;
		this.price = price;
	}

	@Override
	public synchronized String toString() {
		try {
			Thread.sleep(12);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return "Product [name=" + name + ", color=" + color + ", price=" + price + "]";
	}
}

2_1:生产者类:

public class Producer implements Runnable{
	private Product product;
	
	public Producer(Product product) {
		super();
		this.product = product;
	}

	@Override
	public void run() {
		boolean flag=false;
		for (int i = 0; i < 10; i++) {
			if(flag){
				this.product.setName("桌子", "黑色", 20.0);
				flag=false;
			}else{
				this.product.setName("纯净水","无色",1.00);
				flag=true;
			}
		}
	}
}

3_1:消费者类

public class Consumer implements Runnable {
	private Product product;
	
	public Consumer(Product product) {
		super();
		this.product = product;
	}
	@Override
	public void run() {
		for (int i = 0; i < 10; i++) {
			try {
				Thread.sleep(12);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(product);
		}
	}
}

测试类不变,运行结果:

Product [name=桌子, color=黑色, price=20.0]
Product [name=纯净水, color=无色, price=1.0]
Product [name=纯净水, color=无色, price=1.0]
Product [name=纯净水, color=无色, price=1.0]
Product [name=桌子, color=黑色, price=20.0]
Product [name=桌子, color=黑色, price=20.0]
Product [name=桌子, color=黑色, price=20.0]
Product [name=桌子, color=黑色, price=20.0]
Product [name=桌子, color=黑色, price=20.0]
Product [name=桌子, color=黑色, price=20.0]

从结果看,信息紊乱的情况已经解决了,但依然会存在重复读取的情况。

对于重复读取的处理,可以加入线程等待唤醒,在Object类中,有对于线程支持的方法,wait()/wait(long timeout)【线程等待】;notify()/notifyAll()【线程唤醒】;其中notify方法为唤醒第一个等待的线程,notifyAll为唤醒所有的等待线程,哪个线程的优先级别高,哪个线程就有可能优先执行。

所以,为了达到生产一个才能取出一个商品的效果,我们可以在Product类中加入一个标识位。当标识位为false时,约定可以生产,但不能取出,当生产结束时,改变标识位位true,代表可以取出,但不能生产。所以我们修改Product类

public class Product {
	private String name;
	private String color;
	private Double price;
	private boolean flag=false;
	public Product() {
		super();
	}
	public synchronized void setName(String name, String color, Double price) {
		if(!flag){
			try {
				super.wait();//为true时,代表可以取出,但不能生产,执行线程等待
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		this.name=name;
		try {
			Thread.sleep(10);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		this.color = color;
		this.price = price;
		flag=false;
		super.notify();//为false时,唤醒生产者线程
	}

	public synchronized void String() {
		if(flag){
			try {
				super.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		try {
			Thread.sleep(10);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("Product [name=" + name + ", color=" + color + ", price=" + price + "]");
		flag=true;
		super.notify();
	}
}

测试类不变,运行结果为:

Product [name=纯净水, color=无色, price=1.0]
Product [name=桌子, color=黑色, price=20.0]
Product [name=纯净水, color=无色, price=1.0]
Product [name=桌子, color=黑色, price=20.0]
Product [name=纯净水, color=无色, price=1.0]
Product [name=桌子, color=黑色, price=20.0]
Product [name=纯净水, color=无色, price=1.0]
Product [name=桌子, color=黑色, price=20.0]
Product [name=纯净水, color=无色, price=1.0]
从结果看,生产者每生产一个,消费者就会取走一个,然后生产者再开始生产,如此循环。

最后我们看一下线程的生命周期

线程生命周期中有三个方法,目前已经是不推荐使用的了:suspend();resume();stop()。这三个方法都使用了@Deprecated声明,因为他们会引起死锁问题。

在开发中比较常用的一种停止线程的做法是设置一个标识位,通过标识位的改变来停止线程。

public class ThreadTest3 implements Runnable{
	private boolean flag=true;
	
	public ThreadTest3() {
		super();
	}
	
	@Override
	public void run() {
		int i=1;
		while(this.flag){
			while(true){
				System.out.println(Thread.currentThread().getName()+"运行:"+(i++));
			}
		}	
	}
	public void stop(){
		this.flag=false;	
	}
	
	public static void main(String[] args) {
		ThreadTest3 threadTest3 = new ThreadTest3();
		Thread thread = new Thread(threadTest3,"线程");
		thread.start();
		threadTest3.stop();
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值