多線程處理list


 
         
  1 package test;
  2 
  3 import java.util.LinkedList;
  4 import java.util.List;
  5 import java.util.concurrent.CountDownLatch;
  6 
  7 public class ThreadTest {
  8 
  9     /**
 10      * 多線程處理list
 11      * 
 12      * @param data
 13      *            數據LinkedList,線程安全
 14      * @param threadNum
 15      *            線程數
 16      * @throws InterruptedException
 17      */
 18     public synchronized void handleList(LinkedList<String> data, int threadNum) throws InterruptedException {
 19         int length = data.size();
 20         int tl = length % threadNum == 0 ? length / threadNum : (length / threadNum + 1);
 21         CountDownLatch latch = new CountDownLatch(100);// 多少協作
 22         long a = System.currentTimeMillis();
 23         for (int i = 0; i < threadNum; i++) {
 24             int end = (i + 1) * tl;
 25             if ((i * tl) <= length) {
 26                 // 繼承thread啟動線程
 27                 // HandleThread thread = new HandleThread("線程[" + (i + 1) +"] ",data, i * tl, end > length ? length : end, latch);
 28                 // thread.start();
 29 
 30                 // 實現Runnable啟動線程
 31                 RunnableThread thread = new RunnableThread("線程[" + (i + 1) + "] ", data, i * tl, end > length ? length : end, latch);
 32                 Thread runable = new Thread(thread);
 33                 runable.start();
 34             }
 35         }
 36         latch.await();// 等待所有工人完成工作
 37         System.out.println("結束*****************************");
 38         long b = System.currentTimeMillis();
 39         System.out.println("時間:" + (b - a) + "毫秒***********************");
 40     }
 41 
 42     // 繼承Thread
 43     class HandleThread extends Thread {
 44         private String threadName;
 45         private List<String> data;
 46         private int start;
 47         private int end;
 48         private CountDownLatch latch;
 49 
 50         public HandleThread(String threadName, List<String> data, int start, int end, CountDownLatch latch) {
 51             this.threadName = threadName;
 52             this.data = data;
 53             this.start = start;
 54             this.end = end;
 55             this.latch = latch;
 56         }
 57 
 58         public void run() {
 59             // TODO 這里處理數據
 60             List<String> l = data.subList(start, end);
 61             System.out.println(threadName + "--" + data.size() + "--" + start + "--" + end + "--");
 62             for (int i = 0; i < l.size(); i++) {
 63                 // 單個線程中的數據
 64                 System.out.println(l.get(i));
 65             }
 66             latch.countDown();// 工人完成工作,計數器減一
 67         }
 68     }
 69 
 70     // 實現Runnable
 71     class RunnableThread implements Runnable {
 72         private String threadName;
 73         private List<String> data;
 74         private int start;
 75         private int end;
 76         private CountDownLatch latch;
 77 
 78         public RunnableThread(String threadName, List<String> data, int start, int end, CountDownLatch latch) {
 79             this.threadName = threadName;
 80             this.data = data;
 81             this.start = start;
 82             this.end = end;
 83             this.latch = latch;
 84         }
 85 
 86         public void run() {
 87             // TODO 這里處理數據
 88             List<String> l = data.subList(start, end);
 89             System.out.println(threadName + "--" + data.size() + "--" + start + "--" + end + "--");
 90             for (int i = 0; i < l.size(); i++) {
 91                 // 單個線程中的數據
 92                 System.out.println(l.get(i));
 93             }
 94             latch.countDown();// 工人完成工作,計數器減一
 95         }
 96     }
 97 
 98     public static void main(String[] args) throws InterruptedException {
 99         ThreadTest test = new ThreadTest();
100 
101         // 准備數據
102         LinkedList<String> data = new LinkedList<String>();
103         for (int i = 0; i < 100; i++) {
104             data.add("item" + "  " + i);
105         }
106         test.handleList(data, 100);
107         // System.out.println(ArrayUtils.toString(data));
108 
109     }
110 }

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM