生产者消费者代码

1、利用阻塞队列(BlockingQueue):

put(E e):如果队列满了,一直阻塞,直到队列不满了或者线程被中断–>阻塞;
take():如果队列空了,一直阻塞,直到队列不为空或者线程被中断–>阻塞。

生产者:

import java.util.concurrent.BlockingQueue;

public class Producer extends Thread{
	BlockingQueue<Integer> queue;
	
	public Producer(BlockingQueue<Integer> queue){
		this.queue = queue;
	}
	
	public void run(){
		while(true){
			try {
				queue.put(1);
				System.out.println("生产者" + Thread.currentThread().getName()
	                    + "生产一件资源," + "当前资源池有" + queue.size() + 
	                    "个资源");
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

消费者:

import java.util.concurrent.BlockingQueue;

public class Consumer extends Thread{
	BlockingQueue<Integer> queue;
	
	public Consumer(BlockingQueue<Integer> queue){
		this.queue = queue;
	}
	
	public void run(){
		while(true){
			try {
				queue.take();
				System.out.println("消费者" + Thread.currentThread().getName() + 
	                    "消耗一件资源," + "当前资源池有" + queue.size() 
	                    + "个资源");
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

测试:

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class Test {
	public static void main(String[] args) {
		BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(2);
		
		Producer producer = new Producer(queue);
		Consumer consumer = new Consumer(queue);
		
		producer.start();
		consumer.start();
	}
}

2、利用synchronized、wait和notify:
生产者:

public class Producer extends Thread {
	private Resource resource;

	public Producer(Resource resource) {
		this.resource = resource;
	}

	@Override
	public void run() {
		// 不断地生产资源
		while (true) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			resource.add();
		}
	}
}

消费者:

public class Consumer extends Thread {
	private Resource resource;

	public Consumer(Resource resource) {
		this.resource = resource;
	}

	@Override
	public void run() {
		while (true) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			resource.remove();
		}
	}
}

资源:

public class Resource {
	// 当前资源数量
	private int num = 0;
	// 资源池中允许存放的资源数目
	private int size = 10;

	/**
	 * 从资源池中取走资源
	 */
	public synchronized void remove() {
		if (num > 0) {
			num--;
			System.out.println("消费者" + Thread.currentThread().getName()
					+ "消耗一件资源," + "当前线程池有" + num + "个");
			notifyAll();// 通知生产者生产资源
		} else {
			try {
				// 如果没有资源,则消费者进入等待状态
				wait();
				System.out.println("消费者" + Thread.currentThread().getName()
						+ "线程进入等待状态");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 向资源池中添加资源
	 */
	public synchronized void add() {
		if (num < size) {
			num++;
			System.out.println(Thread.currentThread().getName()
					+ "生产一件资源,当前资源池有" + num + "个");
			// 通知等待的消费者
			notifyAll();
		} else {
			// 如果当前资源池中有10件资源
			try {
				wait();// 生产者进入等待状态,并释放锁
				System.out.println(Thread.currentThread().getName() + "线程进入等待");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

测试:

public class Test {
	public static void main(String[] args) {
		Resource resource = new Resource();
		// 生产者线程
		Producer p1 = new Producer(resource);
		Producer p2 = new Producer(resource);
		Producer p3 = new Producer(resource);
		// 消费者线程
		Consumer c1 = new Consumer(resource);
		// ConsumerThread c2 = new ConsumerThread(resource);
		// ConsumerThread c3 = new ConsumerThread(resource);

		p1.start();
		p2.start();
		p3.start();
		c1.start();
		// c2.start();
		// c3.start();
	}
}

你可能感兴趣的:(生产者消费者代码)