关闭

LinkedBlockingDeque工作密取

652人阅读 评论(0) 收藏 举报
分类:
  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.   

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1418346次
    • 积分:21785
    • 等级:
    • 排名:第333名
    • 原创:470篇
    • 转载:1953篇
    • 译文:3篇
    • 评论:69条
    文章分类
    最新评论
    博客推荐
    http://www.vpser.net/