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 }
时间: 2024-10-01 02:46:00