67,515
社区成员
发帖
与我相关
我的任务
分享
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();
}
}
@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();
}
}