2

I am new to multithreading and wanted to know if I could offer the core working on the main thread to be used from the threadpool or does that happen automatically if I use an ExecutorService while calling invokeAll?

I came to this qestion because my performance tests showed a speedup of a maximum of about 2.7 when using 3 or 4 threads in the fixed thread pool.

But the Runtime says I have 4 available processors (due to hyperthreading on corei3) so I would like to get a speedup near 4.

Or does my OS need at least one core occupied?

MWE:

private void doParallel() {

  List<Callable<Object>> jobs = new ArrayList<Callable<Object>>(threadCount);
  for (int i = 0; i < threadCount; i++) {
    jobs.add(new Callable<Object>() {
       @Override
       public Object call() {
         // compute something heavy
         return null;
       }
    });      
  }
  // is it possible to pause the main thread here to make one
  // additional core available for the threadPool?
  threadPool.invokeAll(jobs); // TODO Exception
}
Jan
  • 2,025
  • 17
  • 27

3 Answers3

2

As a theoretical exercise:

Step 1: Add some meaningful (real world) execution logic in your thread which may also involve some kind of i/o, or network access or database access.

Step 2: Start from a thread count of 1 and go upto 10 or 20, executing and profiling your program for each thread count

Step 3: Repeat this for 3-4 times and look at the average stats for each thread count and you will find a "sweet" spot that works on your machine for a specific task.

This would only serve as a learning exercise and in most cases you are just ok with setting the parallel thread count = number of cores.

6ton
  • 4,174
  • 1
  • 22
  • 37
  • Thank you, this is a very nice suggestion, and I already did that before I posted this question with [1, 2, 4, 8, 16, 32, 64, 128] Threads. And I Learned that I could not get better than a Speedup 3.2. Of course in the future I will test and learn about the different operations that (on my machine) cause better or worse Speedups, thanks for the advise. – Jan Jul 12 '15 at 16:27
1

Assuming "main thread" refers to your OS, and not the Java main(String[] args) method:

Because it isn't possible to assign threads to cores, you can't tell your Executor to use the "main" core. In addition, threads typically aren't bound to one core specifically, so the phrase "core working on the main thread" doesn't mean a whole lot. Java doesn't know anything about "cores" -- that's the JVM's job. So the only thing you can control from Java code is the number of threads you use.

I guess that means your second guess is the correct one -- thread assignment/scheduling happens more or less automagically.

In addition, your OS is the entity handling threads (and most other system resources), so halting the OS means halting thread handling, which would likely lead to Bad Things (tm) happening.

The best alternative is probably to simply minimize all other load on your computer -- closing all other programs, not running anything, etc. Then let the OS/CPU scheduler take care of the rest.

Also, IIRC hyperthreading doesn't provide quite the same speedup as actually having the claimed number of cores, so I wouldn't be expecting 4-core performance in the first place. This depends on the workload, though; loads that cause more stalled pipelines (e.g. I/O, cache misses, etc.) would probably hyperthread better than loads which are CPU-bound.

If main thread refers to the thread running main():

Pausing your Java main thread probably wouldn't have too much of a speedup either. If you're doing all your work on worker threads, and simply waiting on the main thread, then it isn't doing any work in the first place, so there isn't much to gain from stopping it. And if it's blocked on the worker pool finishing, then it's probably sleeping anyways.

Community
  • 1
  • 1
awksp
  • 11,764
  • 4
  • 37
  • 44
  • I take this answer because it provides me the most information on my question ( and all its hidden subquestions). Also the fact that hyperthreading is specific (as also mentioned in the links from the comments) helps me to understand a bit more of the automagic. Nonetheless all posts helped me proceed further into more knowledge of parallel computing... with java. Thanks – Jan Jul 12 '15 at 16:32
0

If you realy want to just pause a thread, yield might be an option.

Causes the currently executing thread object to temporarily pause and allow other threads to execute. -java docs

Of course you would have to call it on the thread you want to be paused.

PeterErnsthaft
  • 375
  • 5
  • 14