Detailed explanation of Java multithreading processing List data

  • 2021-07-09 08:00:19
  • OfStack

Example 1:

Problem solving: How to have n threads iterate through an List collection of n elements in sequence


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

public class Test_4 {
/**
*  Multithreading list
*
* @param data  Data list
* @param threadNum  Number of threads 
*/
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(" Thread [" + (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+" Handled "+subList.size()+" Article! ");
}

}

public static void main(String[] args) {
Test_4 test = new Test_4();
//  Prepare data 
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));
}
}

Example 2:

List Multithreaded Concurrent Read Reads Existing list Objects


// Test read List Thread class of, roughly 34 Seconds 
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));
  }  
}

// Thread class 
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));
    }       
  }  
}

Example 3:

Multithreaded segmented processing of List collection

Scenario: Big data List collection. It is necessary to compare the data in List collection with the data in standard library to generate new, updated and cancelled data
Solution:

List set segmentation, Dynamic creation of thread pool newFixedThreadPool Implement the comparison operation in multiple threads

public static void main(String[] args) throws Exception {

    //  Start time 
    long start = System.currentTimeMillis();
    List<String> list = new ArrayList<String>();

    for (int i = 1; i <= 3000; i++) {
      list.add(i + "");
    }
    //  Every 500 Bar data open 1 Strip thread 
    int threadSize = 500;
    //  Total number of data 
    int dataSize = list.size();
    //  Number of threads 
    int threadNum = dataSize / threadSize + 1;
    //  Definition tag , Filter threadNum Is an integer 
    boolean special = dataSize % threadSize == 0;

    //  Create 1 Thread pool 
    ExecutorService exec = Executors.newFixedThreadPool(threadNum);
    //  Definition 1 Task set 
    List<Callable<Integer>> tasks = new ArrayList<Callable<Integer>>();
    Callable<Integer> task = null;
    List<String> cutList = null;

    //  Determine the data for each thread 
    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(" No. 1 " + (i + 1) + " Group: " + cutList.toString());
      final List<String> listStr = cutList;
      task = new Callable<Integer>() {

        @Override
        public Integer call() throws Exception {
          System.out.println(Thread.currentThread().getName() + " Threads: " + listStr);
          return 1;
        }
      };
      //  The list of task containers submitted here and the returned Future There is an order corresponding relationship in the list 
      tasks.add(task);
    }

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

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

    //  Close the thread pool 
    exec.shutdown();
    System.out.println(" End of thread task execution ");
    System.err.println(" Executing the task consumes   : " + (System.currentTimeMillis() - start) + " Milliseconds ");
  }

Related articles: