Thread concurrent programming Callable and Future

//1-使用5个线程并发-Runnable
 private void send(List<Integer> list){
  List<DataPO> files = dataService.findByIds(list);
  files = Collections.synchronizedList(files);
  for(int i=0;i<5;i++){
   new Thread(new SendDataThread(files)).start();
  }
 }
 
 //2-使用2个线程并发-Callable+FutureTask: 使用Callable+FutureTask获取执行结果
 private void send2(List<Integer> list){
  List<DataPO> files = dataService.findByIds(list);
  //files = Collections.synchronizedList(files);
  //分隔数据
  int len = files.size();
  List<DataPO> files1 = files.subList(0, len/2);
  List<DataPO> files2 = files.subList(len/2, len);
  Map<String, List<DataPO>> map = new HashMap<String, List<DataPO>>();
  map.put("file0", files1);
  map.put("file1", files2);
  Callable[] calls = new Callable[2];
  FutureTask[] tasks = new FutureTask[2];
  try {
   for(int i=0;i<2;i++){
    calls[i] = new SendDataThread2(map.get("file" + i));
    tasks[i] = new FutureTask(calls[i]);
   }
   
   for(int i=0;i<2;i++){
    new Thread(tasks[i]).start();
   }
   System.out.println("**");
   
   List<Integer> result = null;
   for(int i=0;i<2;i++){
    result = (List<Integer>)tasks[i].get();
    System.out.println("*** result " + result.size());
   }
   System.out.println("*****");
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 
 //3-使用2个线程并发-Callable+Future: Callable+Future获取执行结果
 private void send3(List<Integer> list){
  List<DataPO> files = dataService.findByIds(list);
  int len = files.size();
  List<DataPO> files1 = files.subList(0, len/2);
  List<DataPO> files2 = files.subList(len/2, len);
  Map<String, List<DataPO>> map = new HashMap<String, List<DataPO>>();
  map.put("file0", files1);
  map.put("file1", files2);
//  files = Collections.synchronizedList(files);
  Callable[] calls = new Callable[2];
  for(int i=0;i<2;i++){
   calls[i] = new SendDataThread3(map.get("file" + i));
  }
  // 创建一个执行任务的服务
  ExecutorService es = Executors.newFixedThreadPool(2);
  try { // Submit and execute the task, a Future object is returned when the task starts,
   // If you want to get the result of the task execution or exception, you can operate on this Future object
   Future future1 = es .submit(calls[0]);
   System.out.println("*");
   // Get the result of the first task, if the get method is called, the current thread will wait for the task to finish before executing it
   //System .out.println("task1: " + future1.get());
   Future future2 = es.submit(calls[1]);
   System.out.println("**");
   // wait 5 seconds, then Stop the second task. Because the second task is in an infinite loop
   //Thread.sleep(5000);
   System.out.println("task1: " + future1.get());
   System.out.println("***");
   System.out.println("task2: " + future2.get());
   //System.out.println("task2 cancel: " + future2.cancel(true));
  } catch (Exception e) { System.out.println(e.toString()); }
  // Stop the task execution service
  System.out.println("*****");
  files.clear();
  es. shutdownNow();
 }

 

public class SendDataThread3 implements Callable<List<Integer>> {
 private List<DataPO> files;
 
 public SendDataThread3(List<DataPO> files){
  this.files = files;
 }

 public List<Integer> call() throws Exception {
  List<Integer> result = new ArrayList<Integer>();
  DataPO file = null;
  int len = files.size();
  int no = 0;
  while(no<len){
   file = files.get(no++);
//   files.remove(0);
   result.add(file.getId());
   Thread.currentThread().sleep(1000);
   System.out.println(Thread.currentThread().getName() + "    " + file.getSyscod() + ", " + file.getFilename());
  }
  return result;
 }

}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326679978&siteId=291194637