me域名网站,wordpress框架视频,石家庄手机网站建设公司,如何自己免费做网站Worker-Thread模式类似于工厂流水线#xff0c;有时也称为流水线设计模式。线程池在某种意义上也算是Worker-Thread模式的一种实现#xff0c;线程池初始化时创建线程类似于在流水线等待工作的工人#xff0c;提交给线程池的Runnable接口类似于需要加工的产品#xff0c;Ru… Worker-Thread模式类似于工厂流水线有时也称为流水线设计模式。线程池在某种意义上也算是Worker-Thread模式的一种实现线程池初始化时创建线程类似于在流水线等待工作的工人提交给线程池的Runnable接口类似于需要加工的产品Runnable的run方法相当于组装该产品的说明书。Worker-Thread模式需要如下几个角色 流水线工人对传送带上的产品进行加工 流水线传送带用于传送来自上游的产品 产品组装说明书用于说明该产品如何组装 Worker-Thread模式中生产线保存了在处理中的产品并且是启动生产线的线程后生产线启动若干数量的流水线工人线程 生产线聚合了产品和工人。生产者消费者模式是单纯的依赖关系生产者和消费者都依赖产品队列生产者和消费者是相互不知道。 示例代码如下
public abstract class InstructionBook {
protected abstract void firstProcess();
protected abstract void secondProcess();public final void create() {
this.firstProcess();
this.secondProcess();
}}
public class Production extends InstructionBook{
private final int productId;public Production(int productionId) {
this.productIdproductionId;
}public int getProductionId() {
return this.productId;
}Override
protected void firstProcess() {
System.out.println(execute the this.productId first process);
}Override
protected void secondProcess() {
System.out.println(execute the this.productId second process);
}}
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;public class Worker extends Thread{
private final ProductionChannel channel;public Worker(String workerName, ProductionChannel channel) {
super(workerName);
this.channelchannel;
}public void run() {
while(true) {
try {
Production productionthis.channel.takeProduction();
System.out.println(getName() process the production.getProductionId());
production.create();
TimeUnit.SECONDS.sleep(ThreadLocalRandom.current().nextInt(5));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}}
import java.util.ArrayList;public class ProductionChannel {
private final ArrayListProduction productionQueuenew ArrayList();
private int total20;
private final Worker[] workers;public ProductionChannel(int workerSize) {
this.workersnew Worker[workerSize];
for(int i0;iworkerSize;i) {
workers[i]new Worker(Worker-i,this);
workers[i].start();
}
}public void offerProduction(Production production) {
synchronized(this) {
while(totalthis.productionQueue.size()) {
try {
System.out.println(processing production idproduction.getProductionId() , in waiting state);
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(processing production idproduction.getProductionId());
this.productionQueue.add(production);
this.notifyAll();
}
}public Production takeProduction() {
synchronized(this) {
while(this.productionQueue.size()0) {
try {
System.out.println(processing to fetch production, while in waiting state);
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.notifyAll();
Production pthis.productionQueue.get(0);
this.productionQueue.remove(0);
return p;
}
}}
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;public class WTtest {public static void main(String[] args) {
ProductionChannel channelnew ProductionChannel(5);
AtomicInteger pidnew AtomicInteger();
IntStream.range(1, 8).forEach(i-new Thread(()-{
// while(true) {
channel.offerProduction(new Production(pid.getAndIncrement()));
try {
TimeUnit.SECONDS.sleep(ThreadLocalRandom.current().nextInt(5));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// }
}).start());
}}