wait()和notify

sail1202 2009-08-25 01:02:05
我现在的需求是
生产两个产品放入集合 然后在消费 要怎么实现
生产一个 消费一个 我知道
...全文
80 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
sail1202 2009-08-25
  • 打赏
  • 举报
回复
我生产两个 就消费两个 然后再生产
haoziheyezi 2009-08-25
  • 打赏
  • 举报
回复
友情帮顶
lk198186 2009-08-25
  • 打赏
  • 举报
回复
顶个
owen_008 2009-08-25
  • 打赏
  • 举报
回复
不明白~顶一个~
feishare 2009-08-25
  • 打赏
  • 举报
回复
如果想实现生产多个,在消费一个的话,可以让生产者睡眠的时间变短
但是这样还不如不用线程了,线程的调用执行是不确定的
andesen 2009-08-25
  • 打赏
  • 举报
回复
学习,顶起
yang_zheng_2008 2009-08-25
  • 打赏
  • 举报
回复
给他们加个同步锁,在生产两个之后,这个锁才释放,让消费者才可以消费
dsgdsg 2009-08-25
  • 打赏
  • 举报
回复
给你我写的同步的列子..
给车 上完蜡 后才能 喷漆


package thread;

/**
* Author: FangCheng<br/> Email: fc6029585@163.com<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: fc6029585@163.com<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: fc6029585@163.com<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: fc6029585@163.com<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();
}

}

nyxuekui 2009-08-25
  • 打赏
  • 举报
回复
不知道靓仔生产一个,跟生产两个有什么本质的区别
sail1202 2009-08-25
  • 打赏
  • 举报
回复
生产者

@SuppressWarnings("unused")
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();
}
}



上面这种实现生成一个 消费一个
但是我想生成两个之后 再消费

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧