我现在的需求是
生产两个产品放入集合 然后在消费 要怎么实现
生产一个 消费一个 我知道
生产两个产品放入集合 然后在消费 要怎么实现
生产一个 消费一个 我知道
解决方案 »
- 怎样识别java程序是第一次登陆
- Jboss 进程下线程数不断增加的问题
- 菜鸟被要求,有效率的IO操作
- 如何根据某个html表单元素被选中来选择相应的servlet来处理
- 有愿意定期或不定期见面交流Java技术的人吗?--北京
- struts+mysql的中文乱码问题,急!
- 求救:用xlst把xml转化为另一个xml文件,数据在500条以上就内存溢出了
- jfreechart 堆栈图设置颜色
- 请教,oracle.jdbc.oracleTypes.cursor在哪个包?
- 关于sql server jdbc驱动的问题!!
- 一条简单的sql语句
- 我现在有一ArryayList,里面的数据结构是第一层是表的属性名,第二行开始以后都是数据,就像数据库中的表一样!我现在想把它再jsp中遍历一下,应该怎么遍历?
public class MMSTaskProducer implements IProducer {
private MMSSyncTaskStack taskStack;
private GroupSendJobService groupSendJobService= null;
public MMSTaskProducer(MMSSyncTaskStack taskStack) {
this.taskStack=taskStack;
groupSendJobService =GroupSendJobService.getInstance();
}
public void run() {
while(true) {
GroupSendJob groupSendJob = null;
for (int i = 0; i < 2; i++) {
groupSendJob=new GroupSendJob();
taskStack.push(groupSendJob,2);
}
}
}
}
消费者public class MMSTaskConsumer implements IConsumer {
private MMSSyncTaskStack taskStack;
public MMSTaskConsumer(MMSSyncTaskStack taskStack) {
this.taskStack=taskStack;
}
public void run() {
while(true) {
GroupSendJob groupSendJob = null;
for (int i = 0; i < taskStack.size(); i++){
groupSendJob=(GroupSendJob)taskStack.pop();
}
}
}}
public class MMSSyncTaskStack {
private Log log = LogFactory.getLog(getClass());
private int notify = 0; private LinkedList taskList = new LinkedList(); // 存放生成的MM任务 /**
* 增加一个任务
*
* @param task
* 任务对象
* @param notifyFlag
* 什么时候notifyAll
*/
public synchronized void push(Object task, int notifyFlag) {
System.out.println("notifyFlag : " + notifyFlag);
if (taskList.size() >= notifyFlag && taskList.size() <=10) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (notifyFlag==notify) {
notify = 0;
this.notifyAll();
}
notify++;
taskList.addFirst(task);
System.out.println("size : " + taskList.size());
System.out.println("notify : " + notify);
ThreadUtils.sleep(1000);
} /**
* 获取一个任务
*
* @return
*/
public synchronized Object pop() {
System.out.println("invoke pop method before taskList is size : " + taskList.size());
if (taskList.size() == 0) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (taskList.size() == 0) {
this.notifyAll();
}
Object task = taskList.removeLast();
return task;
} /**
* 当前任务大小
*
* @return
*/
public int size() {
return taskList.size();
}
}上面这种实现生成一个 消费一个
但是我想生成两个之后 再消费
给车 上完蜡 后才能 喷漆
package thread;/**
* Author: FangCheng<br/> Email: [email protected]<br/> Time: Jul 10, 2009<br/>
*/
public class Car {
public boolean waxOn = false;
/**上蜡*/
public synchronized void waxOn(){
waxOn=true;
notifyAll();
}
/**喷漆*/
public synchronized void buffered(){
waxOn=false;
notifyAll();
}
public synchronized void waitForWaxing() throws InterruptedException{
while(!waxOn)
wait();
}
public synchronized void waitforBuffered() throws InterruptedException{
while(waxOn)
wait();
}
}package thread;import java.util.concurrent.TimeUnit;/**
*Author: FangCheng<br/>
*Email: [email protected]<br/>
*Time: Jul 10, 2009<br/>
*/
public class WaxOn implements Runnable{
Car car;
public WaxOn(Car c){
car=c;
}
@Override
public void run() {
while(!Thread.interrupted()){
try {
System.out.print("Wax On!");
TimeUnit.MILLISECONDS.sleep(200);
car.waxOn();//上蜡
car.waitforBuffered(); //等待喷漆
} catch (InterruptedException e) {
System.out.println("Exiting interrupt WaxOn");
}//睡眠200ms
}
System.out.println("Ending WaxOn task");
}
}
package thread;import java.util.concurrent.TimeUnit;/**
*Author: FangCheng<br/>
*Email: [email protected]<br/>
*Time: Jul 16, 2009<br/>
*/
public class WaxOff implements Runnable { Car car;
public WaxOff(Car c){
this.car=c;
}
@Override
public void run() {
try{
while(!Thread.interrupted()){
car.waitForWaxing();
System.out.println("WaxOff");
TimeUnit.MILLISECONDS.sleep(300);//睡眠300ms
car.buffered();//喷漆
}
}catch(Exception e){
System.out.println("Exiting interrupt WaxOff");
}
System.out.println("Ending WaxOff task");
}
}package thread;import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;/**
*Author: FangCheng<br/>
*Email: [email protected]<br/>
*Time: Jul 16, 2009<br/>
*/
public class WaxMain {
public static void main(String[] args) throws InterruptedException {
Car car=new Car();
ExecutorService service=Executors.newCachedThreadPool();
service.execute(new WaxOff(car));
service.execute(new WaxOn(car));
TimeUnit.SECONDS.sleep(5);
service.shutdownNow();
}}