作家
登录

一道多线程题目的解决方案

作者: 来源: 2012-06-12 13:53:33 阅读 我要评论

在iteye上看到的一道多线程的题目,参考了一下网友的实现,那Eclipse调试通过,算是对JAVA5的并发库有个大致的了解,分享出来,欢迎园里的同学拍砖。

题目:

要求用三个线程,按顺序打印1,2,3,4,5.... 71,72,73,74, 75.

线程1先打印1,2,3,4,5, 然后是线程2打印6,7,8,9,10, 然后是线程3打印11,12,13,14,15. 接着再由线程1打印16,17,18,19,20....以此类推, 直到线程3打印到75。

分析:感觉出题人是要考察一下你是否能够很好的控制多线程,让他们有序的进行。

1、线程池:3个线程,需要使用并发库的线程池

2、锁(lcok):在打印的时候,只允许一个线程进入,其他的线程等待

下面的主要的代码:

  1. import java.util.HashMap;  
  2. import java.util.Map;  
  3. import java.util.concurrent.CountDownLatch;  
  4. import java.util.concurrent.ExecutorService;  
  5. import java.util.concurrent.Executors;  
  6. import java.util.concurrent.locks.Condition;  
  7. import java.util.concurrent.locks.Lock;  
  8. import java.util.concurrent.locks.ReentrantLock;  
  9.  
  10. public class NumberPrinter {  
  11.  
  12.     private Lock lock = new ReentrantLock();  
  13.  
  14.     private Condition c1 = lock.newCondition();  
  15.     private Condition c2 = lock.newCondition();  
  16.     private Condition c3 = lock.newCondition();  
  17.  
  18.     private Map<Integer, Condition> condtionContext =   
  19.         new HashMap<Integer, Condition>();  
  20.  
  21.     public NumberPrinter() {  
  22.         condtionContext.put(Integer.valueOf(0), c1);  
  23.         condtionContext.put(Integer.valueOf(1), c2);  
  24.         condtionContext.put(Integer.valueOf(2), c3);  
  25.     }  
  26.       
  27.     private int count = 0;     
  28.       
  29.     public void print(int id) {  
  30.         lock.lock();  
  31.         try {  
  32.             while(count*5 < 75) {  
  33.                 int curID = calcID();  
  34.                 if (id == curID) {  
  35.                     for (int i = 1; i<=5; i++) {  
  36.                         System.out.print(count*5 +i+ ",");  
  37.                     }  
  38.                     System.out.println();  
  39.                     count++;  
  40.                     int nextID = calcID();  
  41.                     Condition nextCondition = condtionContext.get(  
  42.                             Integer.valueOf(nextID));  
  43.                     //通知下一线程  
  44.                     nextCondition.signal();  
  45.                 } else {  
  46.                     Condition condition = condtionContext.get(  
  47.                             Integer.valueOf(id));  
  48.                     condition.await();  
  49.                 }  
  50.             }  
  51.             //通知线程结束  
  52.             for(Condition c : condtionContext.values()) {  
  53.                 c.signal();  
  54.             }  
  55.         } catch (Exception e) {  
  56.             e.printStackTrace();  
  57.         } finally {  
  58.             lock.unlock();  
  59.         }  
  60.     }  
  61.       
  62.     private int calcID() {  
  63.         // TODO Auto-generated method stub  
  64.         return count % 3;  
  65.     }  
  66.  
  67.  
  68.     /**  
  69.      * @param args  
  70.      */ 
  71.     public static void main(String[] args) {  
  72.         ExecutorService executor = Executors.newFixedThreadPool(3);  
  73.         final CountDownLatch latch = new CountDownLatch(1);     
  74.         final NumberPrinter printer = new NumberPrinter();   
  75.         for (int i = 0; i < 3; i++) {     
  76.             final int id = i;  
  77.             executor.submit(new Runnable() {  
  78.                 @Override 
  79.                 public void run() {  
  80.                     // TODO Auto-generated method stub  
  81.                     try {  
  82.                         latch.await();  
  83.                     } catch (InterruptedException e) {  
  84.                         // TODO Auto-generated catch block  
  85.                         e.printStackTrace();  
  86.                     }  
  87.                     printer.print(id);  
  88.                 }  
  89.             });  
  90.         }  
  91.         System.out.println("三个任务开始顺序打印数字。。。。。。");   
  92.         latch.countDown();  
  93.         executor.shutdown();  
  94.     }  

原文链接:http://www.cnblogs.com/sodmecai/archive/2012/05/17/2506230.html

【编辑推荐】

  1. Java的Comparable接口的一个陷阱
  2. Java程序设计:图形与多媒体处理
  3. 详解Java类的生命周期
  4. Java理论与实践: Web层的状态复制
  5. Apache CXF实战之三:传输Java对象

  推荐阅读

  Hibernate中的merge使用详情解说

merge的作用是:新new一个对象,如果该对象设置了ID,则这个对象就当作游离态处理:当ID在数据库中不能找到时,用update的话肯定会报异常,然而用merge的话,就会insert。当ID在数据库中能找到的时候,update与merge>>>详细阅读


本文标题:一道多线程题目的解决方案

地址:http://www.17bianji.com/kaifa2/Java/243.html

关键词: 探索发现

乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

网友点评
自媒体专栏

评论

热度

精彩导读
栏目ID=71的表不存在(操作类型=0)