how many threads to run in java?

前端 未结 9 600
时光取名叫无心
时光取名叫无心 2021-02-03 16:05

I had this brilliant idea to speed up the time needed for generating 36 files: use 36 threads!! Unfortunately if I start one connection (one j2ssh connection object

相关标签:
9条回答
  • 2021-02-03 16:49

    One way to tune the numebr of threads to the size of the machine is to use

    int processors = Runtime.getRuntime().availableProcessors();
    int threads = processors * N; // N could 1, 2 or more depending on what you are doing.
    ExecutorService es = Executors.newFixedThreadPool(threads);
    
    0 讨论(0)
  • 2021-02-03 16:51

    Brute force: Profile incrementally. Increase the number of threads gradually and check the performance. As the number to connections is just 36, its should be easy

    0 讨论(0)
  • 2021-02-03 16:53

    Your Intel i5 has two cores; hyperthreading makes them look like four. So you only get four cores' worth of parallelization; the rest of your threads are time sliced.

    Assume 1MB RAM per thread just for thread creation, then add the memory that each thread requires to process the file. That will give you an idea about why you're getting out of memory errors. How big are the files you're dealing with? You can see that you'll have a problem if they're very large to have them in memory at the same time.

    I'll assume that the server receiving the files can accept multiple connections, so there's value in trying this.

    I'd benchmark with 1 thread and then increase them until I found that the performance curve was flattening out.

    0 讨论(0)
提交回复
热议问题