多线程之生产者与消费者

买不起AJ就用程序买…
AJ11

public class AJ11 {
	private String name;
	private double size;

	public AJ11(String name, double size) {
		super();
		this.name = name;
		this.size = size;
	}

	public AJ11() {
		super();
		// TODO Auto-generated constructor stub
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getSize() {
		return size;
	}

	public void setSize(double size) {
		this.size = size;
	}

	@Override
	public String toString() {
		return "AJ11 [name=" + name + ", size=" + size + "]";
	}

}

ConsumerTask

import java.util.concurrent.LinkedBlockingQueue;

public class ConsumerTask implements Runnable {
	LinkedBlockingQueue<AJ11> queue;

	public ConsumerTask(LinkedBlockingQueue<AJ11> queue) {
		this.queue = queue;
	}

	@Override
	public void run() {
		while (true) {
			synchronized (queue) {
				while (queue.size() == 0) {
					try {
						queue.notifyAll();
						queue.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				AJ11 aj11 = queue.poll();
				String name = Thread.currentThread().getName();
				System.out.println(name + "消费了" + aj11.getName() + "尺码=" + aj11.getSize());
				queue.notifyAll();
			}
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}

ProducterTask

import java.util.Random;
import java.util.concurrent.LinkedBlockingQueue;

public class ProducterTask implements Runnable {
	LinkedBlockingQueue<AJ11> queue;

	public ProducterTask(LinkedBlockingQueue<AJ11> queue) {
		this.queue = queue;
	}

	@Override
	public void run() {
		while (true) {
			synchronized (queue) {
				while (queue.size() > 10) {
					try {
						queue.notifyAll();
						queue.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				Random r = new Random();
				AJ11 aj11 = new AJ11("aj11", r.nextInt(10) + 38.0);
				queue.add(aj11);
				String name = Thread.currentThread().getName();
				System.out.println(name + "生产了" + aj11.getName() + "尺码=" + aj11.getSize());
				queue.notifyAll();
			}
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}

ThreadDemo

import java.util.concurrent.LinkedBlockingQueue;

public class ThreadDemo {
	public static void main(String[] args) {
		LinkedBlockingQueue<AJ11> queue = new LinkedBlockingQueue<AJ11>();
		ProducterTask product = new ProducterTask(queue);
		new Thread(product, "生产者1").start();
		new Thread(product, "生产者2").start();
		new Thread(product, "生产者3").start();

		ConsumerTask consumer = new ConsumerTask(queue);
		new Thread(consumer, "消费者1").start();
		new Thread(consumer, "消费者2").start();
		new Thread(consumer, "消费者3").start();
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值