Java面试题-生产者和消费者-高并发

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013231917/article/details/83796567

面试题:写一个固定容量同步容器,拥有put和get方法,以及getCount方法,

能够支持,2个生产者线程及10个消费者线程的阻塞调用(经常问!)

-----------------------------------------------------------------------------

1. 使用synchronized、wait和notify/notifyall来实现

package interview_test;
/**
 * 使用wait和notify/notifyall来实现
 * @author x1c
 *
 */

import java.util.LinkedList;
import java.util.concurrent.TimeUnit;

public class MyContainer1<T> {
	final private LinkedList<T> list = new LinkedList<>();
	final private int MAX = 10;
	private int count = 0;
	
	public synchronized void put(T t) {
		while (list.size() == MAX) {  //想想为什么用while而不是if   while会再次检查!
			try {
				this.wait(); //effective java一书中说wait 99.9%和 while一起使用,wait释放锁
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		list.add(t);
		++count;
		this.notifyAll(); //通知消费者线程进行消费
	}
	
	public synchronized T get() {
		T t = null;
		while (list.size() == 0) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		t = list.removeFirst(); //Removes and returns the first element from this list.
		count --;
		this.notifyAll();
		return t;
	}
	
	public static void main(String[] args) {
		MyContainer1<String> c = new MyContainer1<>();
		//启动消费者线程
		for (int i = 0; i < 10; i++) { //10个消费者线程
			new Thread(()->{
				for (int j = 0; j < 4; j++) {
					System.out.println(Thread.currentThread().getName()+"-->"+c.get());
				}
			},"consumer"+i) .start();
		}
		
		System.out.println("等待2秒生产-开始");
		try {
			TimeUnit.SECONDS.sleep(2);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("等待2秒生产-结束");
		
		//启动生产者线程
		for (int i = 0; i < 2; i++) { //2个生产者线程
			new Thread(()->{
				for (int j = 0; j < 8 ;j++) {
					c.put(Thread.currentThread().getName()+" "+j );
					System.out.println("生产者:"+Thread.currentThread().getName()+" "+j );
				}
			},"product"+i) .start();
		}
	}
}

2. 使用Lock、await和signalAll

package interview_test;
/**
 * 面试题:生产者和消费者 经常问
 * 使用Lock和Condition来实现
 *Condition方式可以更加精确的指定那些线程被唤醒
 * @author x1c
 * 总结:synchronized wait notifyall一起使用
 *     lock await signalAll 一起使用
 *
 */

import java.util.LinkedList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyContainer2<T> {
	final private LinkedList<T> list = new LinkedList<>();
	final private int MAX = 10;
	private int count = 0;
	
	private Lock lock = new ReentrantLock(); //Lock接口 java.util.concurrent.locks.Lock  ReentrantLock重入锁
	private Condition producer = lock.newCondition(); //Condition接口 java.util.concurrent.locks.Condition
	private Condition consumer = lock.newCondition(); 
	
	public /*synchronized*/ void put(T t) {
		lock.lock();//
		while (list.size() == MAX) {  //想想为什么用while而不是if   while会再次检查!
			try {
				//this.wait(); //effective java一书中说wait 99.9%和 while一起使用,wait释放锁
				producer.await();//
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		list.add(t);
		++count;
//		this.notifyAll(); //通知消费者线程进行消费
		consumer.signalAll(); //精确叫醒消费者线程
	}
	
	public /*synchronized*/ T get() {
		T t = null;
		lock.lock();//
		while (list.size() == 0) {
			try {
//				this.wait();
				consumer.await();//
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		t = list.removeFirst(); //Removes and returns the first element from this list.
		count --;
//		this.notifyAll();
		consumer.signalAll(); //精确指定消费者线程消费
		return t;
	}
	
	public static void main(String[] args) {
		MyContainer2<String> c = new MyContainer2<>();
		//启动消费者线程
		for (int i = 0; i < 10; i++) { //10个消费者线程
			new Thread(()->{
				for (int j = 0; j < 4; j++) {
					System.out.println(Thread.currentThread().getName()+"-->"+c.get());
				}
			},"consumer"+i) .start();
		}
		
		System.out.println("等待2秒生产-开始");
		try {
			TimeUnit.SECONDS.sleep(2);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("等待2秒生产-结束");
		
		//启动生产者线程
		for (int i = 0; i < 2; i++) { //2个生产者线程
			new Thread(()->{
				for (int j = 0; j < 8 ;j++) {
					c.put(Thread.currentThread().getName()+" "+j );
					System.out.println("生产者:"+Thread.currentThread().getName()+" "+j );
				}
			},"product"+i) .start();
		}
	}
}

------------------------------------------EOF-----------------------------------------------------

猜你喜欢

转载自blog.csdn.net/u013231917/article/details/83796567
今日推荐