自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

一道多線程題目的解決方案

開發(fā) 后端
在iteye上看到的一道多線程的題目,參考了一下網(wǎng)友的實現(xiàn),那Eclipse調(diào)試通過,算是對JAVA5的并發(fā)庫有個大致的了解,分享出來,歡迎園里的同學(xué)拍磚。

在iteye上看到的一道多線程的題目,參考了一下網(wǎng)友的實現(xiàn),那Eclipse調(diào)試通過,算是對JAVA5的并發(fā)庫有個大致的了解,分享出來,歡迎園里的同學(xué)拍磚。

題目:

要求用三個線程,按順序打印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個線程,需要使用并發(fā)庫的線程池

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.             //通知線程結(jié)束  
  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("三個任務(wù)開始順序打印數(shù)字。。。。。。");   
  92.         latch.countDown();  
  93.         executor.shutdown();  
  94.     }  

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

【編輯推薦】

  1. Java的Comparable接口的一個陷阱
  2. Java程序設(shè)計:圖形與多媒體處理
  3. 詳解Java類的生命周期
  4. Java理論與實踐: Web層的狀態(tài)復(fù)制
  5. Apache CXF實戰(zhàn)之三:傳輸Java對象
責(zé)任編輯:林師授 來源: 在程序的路上博客
相關(guān)推薦

2009-09-14 19:39:14

批量線程同步

2009-07-15 17:09:32

Swing線程

2021-11-10 07:47:49

Python源碼代碼

2018-03-13 16:04:45

Promise執(zhí)行順序

2025-01-07 08:20:00

2024-03-18 13:32:11

2009-08-11 10:12:07

C#算法

2025-03-03 01:25:00

SpringAOP日志

2009-03-18 09:26:23

Winform多線程C#

2015-06-17 11:18:08

C#多線程基礎(chǔ)練習(xí)題

2013-04-17 16:03:40

華為IT解決方案巡展

2021-03-02 11:29:50

算法算法分析前端

2013-04-17 15:00:38

華為巡展

2012-05-27 16:21:31

IDC華為

2018-12-03 12:17:27

Semptian解決方案

2018-12-03 11:59:42

Inventec解決方案

2018-12-03 12:13:21

Mellanox解決方案

2018-12-03 12:26:30

YADRO解決方案

2024-10-11 17:09:27

2011-08-18 09:33:23

點贊
收藏

51CTO技術(shù)棧公眾號