zoukankan      html  css  js  c++  java
  • 多线程处理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 }

      

  • 相关阅读:
    IOS使用 swizzle 解决一些错误
    Objective-C的hook方案(一): Method Swizzling
    jmeter录制Chrome浏览器https请求进行压力测试
    FIDDLER导出JMX文件,JMETER打开导出的JMX报错的解决方式
    Fiddler的PC端与手机端抓包配置步骤
    初识中间件之消息队列--提高服务性能
    Python虚拟环境配置应用
    jmeter三种阶梯式加压
    JMETER-正则表达式提取与查看变量是否提取正确
    jmeter的线程数,并发用户数,TPS,RPS 关系解说
  • 原文地址:https://www.cnblogs.com/-lpf/p/4633673.html
Copyright © 2011-2022 走看看