生产者和消费者:
就犹如在快餐店点餐一样,有多个打饭的,有不定时的人来买饭,买饭的人从快餐店自动取餐,如果快餐的库存数量达到下限值时,自动启动打饭的,补充盒饭。
通过while循环的方式,传入变量isStop,来判断是否结束当前while循环。
1 package jquery.test.interview; 2 3 import java.util.Vector; 4 5 public class Productor<T> implements Runnable { 6 7 private volatile Vector<T> v; 8 9 private volatile boolean flg = true; 10 private volatile boolean procFlg = true; 11 12 public Productor(Vector<T> v){ 13 this.v = v; 14 } 15 16 /** 17 * 生产产品 18 * @return 19 */ 20 public void createProduct(){ 21 synchronized(v){ 22 while(flg){ 23 System.out.println("库存产品数....1.." + v.size()); 24 if((v.isEmpty() || v.size() < 3) && procFlg){ 25 System.out.println("库存紧张,开始生产......"); 26 v.add((T)"product1....."); 27 v.add((T)"product2....."); 28 v.add((T)"product3....."); 29 v.add((T)"product4....."); 30 v.add((T)"product5....."); 31 } 32 //开始等待 33 try { 34 System.out.println("库存产品数...2..." + v.size()); 35 v.notifyAll(); 36 v.wait(); 37 } catch (InterruptedException e) { 38 v.notifyAll(); 39 } 40 } 41 if(!flg){ 42 System.out.println("-------------stop proc ---"); 43 v.notifyAll(); 44 } 45 } 46 } 47 48 @Override 49 public void run() { 50 createProduct(); 51 } 52 53 public void stop(){ 54 flg = false; 55 } 56 57 public void stopProc(){ 58 procFlg = false; 59 } 60 61 }
1 package jquery.test.interview; 2 3 import java.util.Vector; 4 5 public class Customer<T> implements Runnable { 6 7 private volatile Vector<T> vector; 8 9 private volatile boolean flag2 = true; 10 11 public Customer(Vector<T> vector){ 12 this.vector = vector; 13 } 14 15 public void getProduct(){ 16 synchronized(vector){ 17 System.out.println("-----stop---customer--222222222-"); 18 while(flag2){ 19 System.out.println("-----flag2----^^^^-"); 20 if(null == vector || vector.isEmpty()){ 21 try { 22 System.out.println("--没有产品等待中....."); 23 vector.wait(); 24 } catch (InterruptedException e) { 25 e.printStackTrace(); 26 } 27 }else{ 28 System.out.println("--获取产品使用....." + vector.get(0)); 29 vector.remove(0); 30 vector.notify(); 31 } 32 } 33 if(!flag2){ 34 System.out.println("-----stop---customer---"); 35 vector.notifyAll(); 36 } 37 } 38 } 39 40 @Override 41 public void run() { 42 getProduct(); 43 } 44 45 public void stop(){ 46 flag2 = false; 47 } 48 49 }
1 public static void main(String[] args) throws InterruptedException, ExecutionException { 2 Vector<String> product = new Vector<String>(); 3 ExecutorService pool = Executors.newCachedThreadPool(); 4 Productor proc = new Productor(product); 5 pool.submit(proc); 6 Productor proc2 = new Productor(product); 7 pool.submit(proc2); 8 List<Customer> cusGroup = new ArrayList<Customer>(); 9 for(int i =0;i<1;i++){ 10 Customer cus = new Customer(product); 11 cusGroup.add(cus); 12 pool.submit(cus); 13 } 14 Thread.sleep(1000*1); 15 proc.stopProc(); // 终止生产 16 proc2.stopProc(); // 终止生产 17 cusGroup.forEach(cus -> cus.stop()); //关闭客户 18 proc.stop(); // 关闭生产者,(先关闭消费者,要不然消费者没法关闭) 19 proc2.stop(); // 关闭生产者,(先关闭消费者,要不然消费者没法关闭) 20 Thread.sleep(1000*2); 21 pool.shutdown(); 22 System.out.println("-------------结束"); 23 }