一.流水线工厂模式
流水线产品:任务对象
此处用计算任务代替产品生产 、
1.使用布尔值来做判断,控制流水线生产流程,线程不是谁先启动,谁就先执行
public class Task {
int num;
boolean flag1;
boolean flag2;
boolean flag3;
public void task1(){
if(!flag1){
num=20;
flag1=true;
}
}
public void task2(){
if(!flag2&&flag1){
num*=10;
flag2=true;
}
}
public void task3(){
if(!flag3&&flag2){
num*=num;
flag3=true;
}
}
}
假设创建500个产品任务,将任务对象加入到动态数组中(使用线程完成)
public class AutoCreateTask implements Runnable{
ArrayList<Task> taskList=null;
public AutoCreateTask(ArrayList<Task> taskList) {
this.taskList = taskList;
}
@Override
public void run() {
for (int i = 0; i < 500; i++) {
Task task=new Task();
taskList.add(task);
}
System.out.println("自动生产任务工作线程已完成"+taskList.size());
}
}
创建三个线程对任务流程进行控制
用while循环保证把符合条件的筛选一遍,如果只用一次for循环则任务二和三可能因为条件不满足而无法执行
public class Task_01_Thread implements Runnable{
ArrayList<Task> taskList;
public Task_01_Thread(ArrayList<Task> taskList) {
this.taskList = taskList;
}
@Override
public void run() {
//加入while循环来重新遍历符合条件的任务对象
while(true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("Task T1执行");
int count=0;
for (int i = 0; i <taskList.size() ; i++) {
Task task=taskList.get(i);
task.task1();
if(task.flag1){
count++;
System.out.println("T1:"+count);
}
}
if(count==500){
System.out.println("T1执行完成");
break;
}
}
}
}
class Task_02_Thread implements Runnable{
ArrayList<Task> taskList;
public Task_02_Thread(ArrayList<Task> taskList) {
this.taskList = taskList;
}
@Override
public void run() {
//加入while循环来重新遍历符合条件的任务对象
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("Task T2执行");
int count=0;
for (int i = 0; i <taskList.size() ; i++) {
Task task=taskList.get(i);
task.task2();
if(task.flag2){
count++;
System.out.println("T2:"+count);
}
}
if(count==500){
System.out.println("T2执行完成");
break;
}
}
}
}
class Task_03_Thread implements Runnable {
ArrayList<Task> taskList;
public Task_03_Thread(ArrayList<Task> taskList) {
this.taskList = taskList;
}
@Override
public void run() {
//加入while循环来重新遍历符合条件的任务对象
while(true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("Task T3执行");
int count=0;
for (int i = 0; i <taskList.size() ; i++) {
Task task=taskList.get(i);
task.task3();
if(task.flag3){
count++;
System.out.println("T3:"+count);
}
}
if(count==500){
System.out.println("T3执行完成");
break;
}
}
}
}
主函数运行
public class Manage {
public static void main(String[] args) {
ArrayList<Task> taskList=new ArrayList<>();
//创建自动生成线程并启动
AutoCreateTask atct=new AutoCreateTask(taskList);
Thread at1=new Thread(atct);
at1.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
//创建流水线线程 线程1 2 3
Task_01_Thread taskT01=new Task_01_Thread(taskList);
Task_02_Thread taskT02=new Task_02_Thread(taskList);
Task_03_Thread taskT03=new Task_03_Thread(taskList);
Thread t1=new Thread(taskT01);
Thread t2=new Thread(taskT02);
Thread t3=new Thread(taskT03);
//流水线执行
t1.start();
t2.start();
t3.start();
//等待所有的线程都完成工作了
//join提交线程去执行,直到线程执行完成后往后去执行
try {
at1.join();
t1.join();
t2.join();
t3.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
//查看结果
for (int i = 0; i < taskList.size(); i++) {
System.out.println(taskList.get(i).num);
}
}
}
T1 完成一个任务的第一部分 会告诉 T2 这个任务可以执行第二个部分了
完成了对流水线的大致模拟,实现了任务按照123的顺序执行,得出想要的结果即产品