package Exp1;

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

public class createQueue {
int max=5;
LinkedList<Integer> Queue=new LinkedList<Integer>();
Lock lock=new ReentrantLock();
Condition empty=lock.newCondition();
Condition full=lock.newCondition();

public synchronized void push(int ProdRandom){
	try{
		lock.lock();
		while(max==Queue.size()){
	System.out.println("存储空间已满,请等待");
	full.await();
		}
		Queue.add(ProdRandom);
		empty.signal();
	}
		catch(InterruptedException e){
			e.printStackTrace();
		}
	finally{
		lock.unlock();
	}
}

@SuppressWarnings("finally")
public synchronized int pop(){
	int m=0;
	try{
		lock.lock();

	while(Queue.size()==0){
		System.out.println("存储空间已空,请等待");
		empty.await();
	}
	m=Queue.removeFirst();
	full.signal();
	}
		catch(InterruptedException e){
			e.printStackTrace();
		}
	finally{
		lock.unlock();
		return m;
	}
}
}
最近下载更多
caomin  LV4 2023年5月25日
Tiam123  LV3 2021年12月22日
伴空0426  LV1 2020年12月17日
838092275  LV1 2020年11月29日
人来疯  LV1 2020年11月7日
wewqetrt  LV3 2020年1月2日
winky92  LV1 2019年11月28日
献出你的心脏  LV2 2019年6月22日
我真系刺客  LV2 2019年6月13日
xianshi  LV1 2019年5月16日
最近浏览更多
阿超在大学修仙  LV1 2023年9月15日
caomin  LV4 2023年5月25日
wuziayng1232  LV10 2023年2月21日
3043864991  LV2 2023年1月5日
jjfskldjf  LV2 2022年11月27日
微信网友_6191697646571520  LV6 2022年11月23日
shibing333  LV1 2022年10月10日
18214966423  LV2 2022年9月6日
陈小灏  LV14 2022年6月8日
Tiam123  LV3 2021年12月22日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友