线程问题<2>

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

能够支持2个生产者线程以及10个消费者线程的阻塞调用


1.使用wait和notify/notifyAll来实现

public class Test<T> {

	private LinkedList<T> list = new LinkedList<T>();
	private int MAX = 10;//最大容量
	private int count = 0;//当前容器的个数
	
	public synchronized void put(T t) {
		while(list.size() == MAX) {
			try {
				System.out.println("生产者进入了等待");
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		list.add(t);
		System.out.println("生产者生产了" + t);
		++count;
		this.notifyAll();
	}
	
	public synchronized <T> T get() {
		T t = null;
		while(list.size() == 0) {
			try {
				System.out.println("消费者进入了等待");
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		t = (T) list.removeFirst();
		System.out.println("消费者消费了" + t); 
		--count;
		this.notifyAll();
		return t;
	}
	
	public static void main(String[] args) throws InterruptedException {
		final Test test = new Test();

		for (int i = 0; i < 10; i++) {
			new Thread() {
				public void run() {
					for (int j = 0; j < 5; j++) {
						test.get();
					}
				}
			}.start();
		}
		
		for (int i = 0; i < 2; i++) {
			new Thread() {
				public void run() {
					for (int j = 0; j < 35; j++) {
						test.put(j);
					}
				}
			}.start();
		}
		
		Thread.sleep(1000);
		
		System.out.println("容器中还有" + test.count + "个");
		
	}
	
}


2.使用Lock和Condition来实现


public class Test51<T> {

	final private LinkedList<T> list = new LinkedList();
	private static int MAX = 10;
	private int count = 0;
	
	private Lock lock = new ReentrantLock();
	private Condition p = lock.newCondition();
	private Condition c = lock.newCondition();
	
	public void put(T t) {
		try {
			lock.lock();
			while(list.size() == MAX) {
				System.out.println("生产者进入等待");
				p.await();
			}
			list.add(t);
			System.out.println("生产者生产了" + t);
			++count;
			c.signalAll();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
	}
	
	public <T> T get() {
		T t = null;
		try {
			lock.lock();
			while(list.size() == 0) {
				System.out.println("消费者进入等待");
				c.await();
			}
			t = (T) list.removeFirst();
			System.out.println("消费者消费了" + t);
			--count;
			p.signalAll();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
		return t;
	}
	
	public static void main(String[] args) throws InterruptedException {
		final Test51 t = new Test51();
		
		for (int i = 0; i < 10; i++) {
			new Thread() {
				public void run() {
					for (int j = 0; j < 5; j++) {
						t.get();
					}
				}
			}.start();
		}
		
		for (int i = 0; i < 2; i++) {
			new Thread() {
				public void run() {
					for (int j = 0; j < 25; j++) {
						t.put(j);
					}
				}
			}.start();
		}
		
		Thread.sleep(1000);
		
		System.out.println("还剩余" + t.count);
	}
	
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值