多线程之责任链案例
需求:(本人最近学习多线程,该需求是自己想的,不对的地方望不要介意)
牛奶制作:1、获取牛奶源 2、工厂进行加工 3、包装 该过程是一个责任链的模式,整个过程是一个串行过程,单独1/2/3步骤是一个独立过程
package com.wl.Test.duty; public interface IMikeFactory { //定义一个生产牛奶方法 void produceMike(MikeEntity mike); }
package com.wl.Test.duty; /*** * 牛奶实体 * @author gzyc * */ public class MikeEntity { private String name; private String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "MikeEntity [name=" + name + ", address=" + address + "]"; } }
package com.wl.Test.duty; import java.util.concurrent.LinkedBlockingQueue; /*** * @desc 第一步:获取牛奶源 * @author gzyc * */ public class MikeSource extends Thread implements IMikeFactory{ private IMikeFactory mikeFactory; //定义一个存储牛奶源的阻塞队列 private LinkedBlockingQueue<MikeEntity> queue = new LinkedBlockingQueue<>(); //定义一个控制运行的开关 private volatile boolean flag = false; public MikeSource(IMikeFactory mikeFactory) { this.mikeFactory = mikeFactory; } @Override public void produceMike(MikeEntity mike) { queue.add(mike); } @Override public void run() { while (!this.currentThread().isInterrupted()) { //while (!flag) { try { //从队列中拿到牛奶源 MikeEntity mike = queue.take(); System.out.println("获取到牛奶源:"+mike); //交给下一步骤进行加工牛奶 mikeFactory.produceMike(mike); } catch (InterruptedException e) { //e.printStackTrace(); break; } } System.out.println("牛奶源结束"); } }
package com.wl.Test.duty; import java.util.concurrent.LinkedBlockingQueue; /*** * @desc 第二步:加工牛奶源 * @author gzyc * */ public class MikeProcess extends Thread implements IMikeFactory{ private IMikeFactory mikeFactory; //定义一个存储牛奶源的阻塞队列 private LinkedBlockingQueue<MikeEntity> queue = new LinkedBlockingQueue<>(); //定义一个控制运行的开关 private volatile boolean flag = false; public MikeProcess(IMikeFactory mikeFactory) { this.mikeFactory = mikeFactory; } @Override public void produceMike(MikeEntity mike) { //对牛奶进行加工 queue.add(mike); } @Override public void run() { while (!flag) { try { //从队列中拿到加工过后的牛奶 MikeEntity mike = queue.take(); System.out.println("加工过后的牛奶:"+mike); //交给下一步骤进行加工牛奶 mikeFactory.produceMike(mike); } catch (InterruptedException e) { //e.printStackTrace(); break; } } System.out.println("牛奶加工结束"); } }
package com.wl.Test.duty; import java.util.concurrent.LinkedBlockingQueue; /*** * @desc 第三步:牛奶包装 * @author gzyc * */ public class MikePackage extends Thread implements IMikeFactory{ private IMikeFactory mikeFactory; //定义一个存储牛奶源的阻塞队列 private LinkedBlockingQueue<MikeEntity> queue = new LinkedBlockingQueue<>(); //定义一个控制运行的开关 private volatile boolean flag = false; public MikePackage(IMikeFactory mikeFactory) { this.mikeFactory = mikeFactory; } public MikePackage() { } @Override public void produceMike(MikeEntity mike) { //对牛奶进行包装 queue.add(mike); } @Override public void run() { while (!flag) { try { //从队列中拿到包装过后的牛奶 MikeEntity mike = queue.take(); System.out.println("包装过后的牛奶:"+mike); //牛奶制作结束 System.out.println(mike+"牛奶制作结束"); //mikeFactory.produceMike(mike); } catch (InterruptedException e) { //e.printStackTrace(); break; } } System.out.println("牛奶包装结束"); } }
package com.wl.Test.duty; import java.util.concurrent.LinkedBlockingQueue; /*** * 责任链 * @author gzyc * 牛奶制作:1、获取牛奶源 2、工厂进行加工 3、包装 * 该过程是一个责任链的模式,整个过程是一个串行过程,单独1/2/3步骤是一个独立过程 */ public class DutyMainTest { private static IMikeFactory mikeFactory; public static void main(String[] args) throws InterruptedException { //启动包装牛奶线程 MikePackage mikePackage = new MikePackage(); mikePackage.start(); //启动加工牛奶线程 MikeProcess mikeProcess = new MikeProcess(mikePackage); mikeProcess.start(); //启动或者牛奶源线程 mikeFactory = new MikeSource(mikeProcess); ((MikeSource)mikeFactory).start(); MikeEntity mikeEntity; for(int i=1;i<=10;i++){ mikeEntity = new MikeEntity(); mikeEntity.setName("花生牛奶"+i); mikeEntity.setAddress("贵阳"+i); mikeFactory.produceMike(mikeEntity); } Thread.currentThread().sleep(5000); //中断线程, mikePackage.interrupt(); mikeProcess.interrupt(); ((MikeSource)mikeFactory).interrupt(); } }