欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Java多線程處理List數(shù)據(jù)

 更新時(shí)間:2019年03月20日 11:37:52   作者:吃飯了嗎飯沒(méi)了秀  
這篇文章主要介紹了Java多線程處理List數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

實(shí)例1:

解決問(wèn)題:如何讓n個(gè)線程順序遍歷含有n個(gè)元素的List集合

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.ArrayUtils;

public class Test_4 {
/**
* 多線程處理list
*
* @param data 數(shù)據(jù)list
* @param threadNum 線程數(shù)
*/
public synchronized void handleList(List<String> data, int threadNum) {
int length = data.size();
int tl = length % threadNum == 0 ? length / threadNum : (length
/ threadNum + 1);

for (int i = 0; i < threadNum; i++) {
int end = (i + 1) * tl;
HandleThread thread = new HandleThread("線程[" + (i + 1) + "] ", data, i * tl, end > length ? length : end);
thread.start();
}
}

class HandleThread extends Thread {
private String threadName;
private List<String> data;
private int start;
private int end;

public HandleThread(String threadName, List<String> data, int start, int end) {
this.threadName = threadName;
this.data = data;
this.start = start;
this.end = end;
}

public void run() {
List<String> subList = data.subList(start, end)/*.add("^&*")*/;
System.out.println(threadName+"處理了"+subList.size()+"條!");
}

}

public static void main(String[] args) {
Test_4 test = new Test_4();
// 準(zhǔn)備數(shù)據(jù)
List<String> data = new ArrayList<String>();
for (int i = 0; i < 6666; i++) {
data.add("item" + i);
}
test.handleList(data, 5);
System.out.println(ArrayUtils.toString(data));
}
}

實(shí)例2:

List多線程并發(fā)讀取讀取現(xiàn)有的list對(duì)象

//測(cè)試讀取List的線程類,大概34秒
package com.thread.list;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
public class Main {
  
  public static void main(String[] args) {
    
    List<String> list = new ArrayList<String>();
    Map<Long,Integer> map = new HashMap<Long,Integer>();

    for(int i = 0;i<1000;i++){
      list.add(""+i);
    }
    
    int pcount = Runtime.getRuntime().availableProcessors();    
    long start = System.currentTimeMillis();    
    
    for(int i=0;i<pcount;i++){
      
      Thread t = new MyThread1(list,map);
      map.put(t.getId(),Integer.valueOf(i));
      t.start();
      try {
        t.join();
      } catch (InterruptedException e) {       
        e.printStackTrace();
      }      
      // System.out.println(list.get(i));
    }    
    System.out.println("----"+(System.currentTimeMillis() - start));
  }  
}

//線程類
package com.thread.list;
 
import java.util.List;
import java.util.Map;
 
public class MyThread1 extends Thread {
 
  private List<String> list;
  private Map<Long,Integer> map;
  
  public MyThread1(List<String> list,Map<Long,Integer> map){
    this.list = list;
    this.map = map;
  }
  
  @Override
  public void run() {
    
    int pcount = Runtime.getRuntime().availableProcessors();
    int i = map.get(Thread.currentThread().getId());
    
    for(;i<list.size();i+=pcount){
      System.out.println(list.get(i));
    }       
  }  
}

實(shí)例3:

多線程分段處理List集合

場(chǎng)景:大數(shù)據(jù)List集合,需要對(duì)List集合中的數(shù)據(jù)同標(biāo)準(zhǔn)庫(kù)中數(shù)據(jù)進(jìn)行對(duì)比,生成新增,更新,取消數(shù)據(jù)
解決方案:

  1. List集合分段,
  2. 動(dòng)態(tài)創(chuàng)建線程池newFixedThreadPool
  3. 將對(duì)比操作在多線程中實(shí)現(xiàn)
public static void main(String[] args) throws Exception {

    // 開(kāi)始時(shí)間
    long start = System.currentTimeMillis();
    List<String> list = new ArrayList<String>();

    for (int i = 1; i <= 3000; i++) {
      list.add(i + "");
    }
    // 每500條數(shù)據(jù)開(kāi)啟一條線程
    int threadSize = 500;
    // 總數(shù)據(jù)條數(shù)
    int dataSize = list.size();
    // 線程數(shù)
    int threadNum = dataSize / threadSize + 1;
    // 定義標(biāo)記,過(guò)濾threadNum為整數(shù)
    boolean special = dataSize % threadSize == 0;

    // 創(chuàng)建一個(gè)線程池
    ExecutorService exec = Executors.newFixedThreadPool(threadNum);
    // 定義一個(gè)任務(wù)集合
    List<Callable<Integer>> tasks = new ArrayList<Callable<Integer>>();
    Callable<Integer> task = null;
    List<String> cutList = null;

    // 確定每條線程的數(shù)據(jù)
    for (int i = 0; i < threadNum; i++) {
      if (i == threadNum - 1) {
        if (special) {
          break;
        }
        cutList = list.subList(threadSize * i, dataSize);
      } else {
        cutList = list.subList(threadSize * i, threadSize * (i + 1));
      }
      // System.out.println("第" + (i + 1) + "組:" + cutList.toString());
      final List<String> listStr = cutList;
      task = new Callable<Integer>() {

        @Override
        public Integer call() throws Exception {
          System.out.println(Thread.currentThread().getName() + "線程:" + listStr);
          return 1;
        }
      };
      // 這里提交的任務(wù)容器列表和返回的Future列表存在順序?qū)?yīng)的關(guān)系
      tasks.add(task);
    }

    List<Future<Integer>> results = exec.invokeAll(tasks);

    for (Future<Integer> future : results) {
      System.out.println(future.get());
    }

    // 關(guān)閉線程池
    exec.shutdown();
    System.out.println("線程任務(wù)執(zhí)行結(jié)束");
    System.err.println("執(zhí)行任務(wù)消耗了 :" + (System.currentTimeMillis() - start) + "毫秒");
  }

以上所述是小編給大家介紹的Java多線程處理List數(shù)據(jù)詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Java中的對(duì)象流總結(jié)(必看篇)

    Java中的對(duì)象流總結(jié)(必看篇)

    下面小編就為大家?guī)?lái)一篇Java中的對(duì)象流總結(jié)(必看篇)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-04-04
  • springboot+vue實(shí)現(xiàn)阿里云oss大文件分片上傳的示例代碼

    springboot+vue實(shí)現(xiàn)阿里云oss大文件分片上傳的示例代碼

    阿里云推出了直傳,本文主要介紹了springboot+vue實(shí)現(xiàn)阿里云oss大文件分片上傳的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-06-06
  • java實(shí)現(xiàn)實(shí)時(shí)通信聊天程序

    java實(shí)現(xiàn)實(shí)時(shí)通信聊天程序

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)實(shí)時(shí)通信聊天程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Java基于NIO實(shí)現(xiàn)群聊功能

    Java基于NIO實(shí)現(xiàn)群聊功能

    這篇文章主要為大家詳細(xì)介紹了Java基于NIO實(shí)現(xiàn)群聊功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • 深入淺出了解happens-before原則

    深入淺出了解happens-before原則

    一提到happens-before原則,就讓人有點(diǎn)“丈二和尚摸不著頭腦”。這個(gè)涵蓋了整個(gè)JMM中可見(jiàn)性原則的規(guī)則,究竟如何理解,把我個(gè)人一些理解記錄下來(lái)。下面可以和小編一起學(xué)習(xí)
    2019-05-05
  • SpringBoot整合數(shù)據(jù)庫(kù)訪問(wèn)層的實(shí)戰(zhàn)

    SpringBoot整合數(shù)據(jù)庫(kù)訪問(wèn)層的實(shí)戰(zhàn)

    本文主要介紹了SpringBoot整合數(shù)據(jù)庫(kù)訪問(wèn)層的實(shí)戰(zhàn),主要包含JdbcTemplate和mybatis框架的整合應(yīng)用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2022-03-03
  • 快速了解Spring Boot

    快速了解Spring Boot

    這篇文章主要介紹了快速了解Spring Boot,介紹了其環(huán)境準(zhǔn)備,URL中的變量以及模板渲染等內(nèi)容,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • springboot整合kaptcha生成驗(yàn)證碼功能

    springboot整合kaptcha生成驗(yàn)證碼功能

    這篇文章主要介紹了springboot整合kaptcha生成驗(yàn)證碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10
  • Ubuntu16.04安裝部署solr7的圖文詳細(xì)教程

    Ubuntu16.04安裝部署solr7的圖文詳細(xì)教程

    這篇文章主要為大家詳細(xì)介紹了Ubuntu16.04安裝部署solr7的圖文詳細(xì)教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Java工具jsch.jar實(shí)現(xiàn)上傳下載

    Java工具jsch.jar實(shí)現(xiàn)上傳下載

    這篇文章主要為大家詳細(xì)介紹了Java操作ftp的一款工具,利用jsch.jar針對(duì)sftp的上傳下載工具類,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-12-12

最新評(píng)論