Java multithreading List data

Example 1:

Solution: how to let n threads traverse the List collection with 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 Thread count
*/
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+"Processed"+subList.size()+"Bar!");
}

}

public static void main(String[] args) {
Test_4 test = new Test_4();
// Preparation 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 multithread reads and reads the existing list object concurrently

//Test reading List About 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 List collection

Scenario: big data List set, need to compare the data in the List set with the data in the standard database, generate new, update, cancel data
Solution:

    1. List set segmentation,
    2. Dynamically create thread pool newFixedThreadPool
    3. Implementation of contrast operation in multithreading
      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 + "");
              }
              // Open one thread for every 500 data
              int threadSize = 500;
              // Total number of data
              int dataSize = list.size();
              // Thread count
              int threadNum = dataSize / threadSize + 1;
              // Definition tag,filter threadNum Integers
              boolean special = dataSize % threadSize == 0;
      
              // Create a thread pool
              ExecutorService exec = Executors.newFixedThreadPool(threadNum);
              // Define a 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("The first" + (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() + "Threading:" + listStr);
                          return 1;
                      }
                  };
                  // The task container list submitted here and the returned Future The list has a sequence corresponding relationship
                  tasks.add(task);
              }
      
              List<Future<Integer>> results = exec.invokeAll(tasks);
      
              for (Future<Integer> future : results) {
                  System.out.println(future.get());
              }
      
              // Close thread pool
              exec.shutdown();
              System.out.println("End of thread task execution");
              System.err.println("Task execution consumed:" + (System.currentTimeMillis() - start) + "Millisecond");
          }

       

Keywords: Java Apache Big Data Database

Added by edcellgavin on Thu, 05 Dec 2019 10:56:09 +0200