Java multithreading in a quad-core laptop

I went through a Java tutorial where it was mentioned that actual multithreading does not happen on a single processor machine. He mentioned that the OS takes a certain amount of time for the Java process, and the JVM thread scheduler collects threads to run one thread at a time for a short amount of time.

I have a laptop that has a quad-core processor - can I start a multi-threaded program faster programmatically by running one thread in each core? The reason I ask this question is because the book mentioned that only a true multiprocessor system can perform several operations at the same time.

+6
source share
4 answers

Even one processor can do β€œseveral things at once” in the free sense, but they are not always parallel. You can run 100 threads to work on one core, and they will receive temporary fragments during which each of them can execute several instructions, thereby creating the impression that they are all executed at the same time.

As I said in another SO post: multithreading on a dual core machine?

The term streams usually encompasses three layers of abstraction:

  • User threads are threads launched by applications and displayed by N: M:
  • Kernel threads , which are threads controlled by the operating system, displayed by N: M:
  • Hardware threads that are actually available physical resources.

Java threads are user threads. The 4 cores in your processor are considered hardware threads. Since N: M mapping by layers, you can see that you can have multiple user threads mapped to fewer hardware threads.

Now, having said that, there are usually two classes of streams, each with its own quirks:

  • I / O threads . These threads spend most of their time reading / writing from the stream and are blocked in the meantime (they are not scheduled to be executed until their event occurs). There is light on the processor, and many of them can work simultaneously even on one core.
  • Computing threads : These threads execute many cycles and make the most of the processor. As a rule, starting more (2x the number of available cores), such threads will degrade performance because the processor has a limited number of function blocks: ALU, FPU, etc.

The second class of the above threads allows you to see the benefits or run a multi-threaded Java program on your quad-core processor. Here is a simple example of a program that first squares the numbers 1.000.000.000, and then in parallel using a thread pool with 4 threads:

import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; class ThreadTask implements Runnable { private int total = 0; public ThreadTask(int total) { this.total = total; } @Override public void run() { int value = 0; for(int i = 0; i < total; i++) { value = i * i; } } } public class Test { public static void main(String[] args) throws InterruptedException { int total = 1000000000; long start = System.currentTimeMillis(); long value = 0; for(int i = 0; i < total; i++) { value = i * i; } long stop = System.currentTimeMillis(); System.out.println((stop - start) + " ms"); ExecutorService exec = Executors.newFixedThreadPool(4); start = System.currentTimeMillis(); for(int i = 0; i < 4; i++) { exec.submit(new ThreadTask(total / 4)); } exec.shutdown(); exec.awaitTermination(10, TimeUnit.SECONDS); stop = System.currentTimeMillis(); System.out.println((stop - start) + " ms"); } } 

Do not set the total value if it is running too fast. Now I am working on a netbook with Intel Atom, so it is not very fast.

+7
source

A multi-core processor can β€œtruly” parallelize your application to the number of cores you have. In your case, it will be 4 threads. Learn more about multi-core processors and multi-processor processors on Wikipedia . Having said that, you can realize the performance benefits using a multi-threaded algorithm on a single processor core, despite the fact that you only have one processor.

The performance improvement achieved through the use of multi-core processors is highly dependent on the software algorithms used and their implementation. In particular, the possible benefits are limited to a piece of software that can be parallelized to run on multiple cores simultaneously; this effect is described by Amdahl's law. in the best case, the so-called awkwardly parallel problems can realize acceleration factors near the number of cores, or even more if the problem is divided enough to enter each kernel cache, avoiding the use of much slower main system memory. However, most applications have not accelerated so much if the programmers did not invest in redistributing the whole problem 2 . Parallelization software is an important research topic.

Also see this fooobar.com/questions/86888 / ... question.

+2
source

Even with one processor, several threads can speed up your program, it all depends on the work you are trying to speed up. For example, if your threads are waiting for I / O. If it is purely computational, then you probably want to limit your threads to your number of cores.

Measure it using experiments.

+1
source

I can confirm that on my i3 laptop, algorithms that run in parallel are almost twice as fast as the serial algorithm.

more context added below ...

These are highly computational algorithms without I / O. In principle, the calculation of statistics on N large arrays, where each array can be performed independently. I believe that using a pool of threads from 2-4 threads gives about the same increase in speed - 2X. Moving to 8 or more topics, things start to slow down a bit as you get more debate (and use more memory). On a processor with a large number of cores, these values ​​will change.

+1
source

Source: https://habr.com/ru/post/907196/


All Articles