简体   繁体   English

多线程执行时间最小化

[英]multithreading execution time minimization

below is my code. 下面是我的代码。 I am populating a list with size 3500000 by thread. 我正在按线程填充大小为3500000的列表。 first i populated the list by one thread. 首先,我用一个线程填充了列表。 And This thread will return a list of string that contains 3500000 items. 并且此线程将返回包含3500000个项目的字符串列表。

This Process Takes 5 seconds to execute. 此过程需要5秒钟才能执行。

Then, I created another Thread and divided the entire task by two and distributed them to the threads. 然后,我创建了另一个线程,并将整个任务一分为二,然后将它们分配给线程。

First thread will populate the list of string of 1900000 items, second thread will return (3500000-1900000=1600000) items. 第一个线程将填充1900000个项目的字符串列表,第二个线程将返回(3500000-1900000 = 1600000)个项目。 The two process are running in parallel. 这两个进程正在并行运行。 So, the should take less time. 因此,应该花费更少的时间。 But, for this case, the total computing time is also 5 seconds. 但是,在这种情况下,总的计算时间也是5秒。

Please anybody help me out to find out where I am doing wrong? 请有人帮我找出我在哪里做错了吗?

I badly need to minimize the execution time. 我非常需要尽量减少执行时间。 How I can minimize the time? 如何减少时间?

package callablefutures;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.Calendar;
import java.util.concurrent.TimeUnit;
import java.util.Date;

public class CallableFutures {

  private static final int NTHREDS = 10;
  public static void main(String[] args) {

  ExecutorService executor = Executors.newFixedThreadPool(NTHREDS);
  List<Future<List<String>>> list = new ArrayList<Future<List<String>>>();


    List<List<String>> lst=new ArrayList();
    List<String> list1=new ArrayList();
    List<String> list2=new ArrayList();


  Runtime rt = Runtime.getRuntime();
  long prevFree = rt.freeMemory();
  long startTime=System.currentTimeMillis();


      Callable<List<String>> worker = new MyCallable(list1,0,1900000);
      Future<List<String>> submit = executor.submit(worker);
      list.add(submit);

      Callable<List<String>> worker1 = new MyCallable(list2,1900000,3500000);
      Future<List<String>> submit1 = executor.submit(worker1);
      list.add(submit1);

    long sum = 0;
    System.out.println(list.size());

    for (Future<List<String>> future : list) {
      try {
          lst.add(future.get());
      } catch (InterruptedException e) {
        e.printStackTrace();
      } catch (ExecutionException e) {
        e.printStackTrace();
      }
    }
    executor.shutdown();
  long endTime=System.currentTimeMillis();
    System.out.println("Total Time Taken: " + (endTime-startTime)/1000%60 +" Seconds");
    System.out.println("Total Memory Taken (MB): " + ((prevFree-rt.freeMemory())/1024)/1024);
  }
}



package callablefutures;
import java.util.concurrent.Callable;
import java.util.List;
import java.util.ArrayList;

public class MyCallable implements Callable<List<String>>{
  public List<String> StrList=new ArrayList();
  public int sIndex,eIndex;
  public MyCallable(List<String> oList,int si,int ei)
  {
      this.StrList=oList;
      this.sIndex=si;
      this.eIndex=ei;
  }
  @Override
  public List<String> call() throws Exception {

    for (int i = this.sIndex; i < this.eIndex; i++) {
      this.StrList.add("ID  "+i);
    }
    return this.StrList;
    //return this.StrList;
  }

}

You are creating about 128 MB of data, which will be larger than your L3 cache so you will be pushing data out to main memory and this is typically easy to saturate with one thread. 您正在创建约128 MB的数据,该数据将大于L3缓存,因此您会将数据推出到主内存中,并且通常一个线程就很容易达到饱和。 If you want threads to run concurrently you want them to be limited to 256 KB each (as they each have their own L2 cache assuming they run on different cores) or 128 KB each if on the same core. 如果希望线程同时运行,则希望每个线程被限制为256 KB(因为如果它们在不同的内核上运行,则每个线程都有自己的L2缓存),或者如果在同一内核上,则每个线程都限制为128 KB。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM