LinkedBlockingDeque工作密取

[java]   view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.java;  
  2.   
  3. import java.util.Random;  
  4. import java.util.concurrent.LinkedBlockingDeque;  
  5.   
  6. public class SecretWorkFetch {  
  7.     private static class Work implements Runnable{  
  8.         private static Object object=new Object();  
  9.         private static int count=0;  
  10.         public final int id;  
  11.         private long putThread;   
  12.         public Work(){  
  13.             synchronized(object){  
  14.                 id=count++;  
  15.             }  
  16.         }  
  17.         @Override  
  18.         public void run() {  
  19.             if(Thread.currentThread().getId()!=putThread){  
  20.                 System.out.println("===================================================");  
  21.             }  
  22.             System.out.println(Thread.currentThread().getId()+":"+putThread+"// finish job "+id);  
  23.               
  24.         }  
  25.         public long getPutThread() {  
  26.             return putThread;  
  27.         }  
  28.         public void setPutThread(long putThread) {  
  29.             this.putThread = putThread;  
  30.         }  
  31.           
  32.           
  33.           
  34.     }  
  35.     public static Work generateWork(){  
  36.         return new Work();  
  37.     }  
  38.     private static class ConsumerAndProducer implements Runnable{  
  39.         private Random random=new Random();  
  40.         private final LinkedBlockingDeque<Work> deque;  
  41.         private final LinkedBlockingDeque<Work> otherWork;  
  42.         public ConsumerAndProducer(LinkedBlockingDeque<Work> deque,LinkedBlockingDeque<Work> otherWork){  
  43.             this.deque=deque;  
  44.             this.otherWork=otherWork;  
  45.         }  
  46.         @Override  
  47.         public void run() {  
  48.             while(!Thread.interrupted()){  
  49.                 try {  
  50.                     Thread.sleep(200);  
  51.                     if(random.nextBoolean()){  
  52.                         int count=random.nextInt(5);  
  53.                         for(int i=0;i<count;i++){  
  54.                             Work w=generateWork();  
  55.                             w.setPutThread(Thread.currentThread().getId());  
  56.                             deque.putLast(w);  
  57.                         }  
  58.                     }  
  59.                     if(deque.isEmpty()){  
  60.                         if(!otherWork.isEmpty()){  
  61.                             otherWork.takeLast().run();;  
  62.                         }  
  63.                           
  64.                     }else{  
  65.                         deque.takeFirst().run();  
  66.                     }  
  67.                 } catch (InterruptedException e) {  
  68.                       
  69.                 }  
  70.             }  
  71.               
  72.               
  73.               
  74.         }  
  75.           
  76.           
  77.     }  
  78.       
  79.       
  80.     public static void main(String[] args) {  
  81.         LinkedBlockingDeque<Work> deque=new LinkedBlockingDeque<Work>();  
  82.         LinkedBlockingDeque<Work> other=new LinkedBlockingDeque<Work>();  
  83.         new Thread(new ConsumerAndProducer(deque,other)).start();  
  84.         new Thread(new ConsumerAndProducer(deque,other)).start();  
  85.           
  86.         new Thread(new ConsumerAndProducer(other,deque)).start();  
  87.         new Thread(new ConsumerAndProducer(other,deque)).start();  
  88.     }  
  89.   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值