Why does my streaming program use only one processor?

I have a program that performs long-term calculations, so I want to speed up its work. Thus, I tried to start 3 threads at the moment, but java.exe still occupies 25% of the CPU usage (therefore only one processor is used), and it remains even if I try to use .setPriority(Thread.MAX_PRIORITY); and set java.exe priority in real time (24). I tried to use RealtimeThread , but it seems to work even slower. It would be ideal if each thread was assigned to one processor, and the total CPU usage increased to 75%, but I do not know how to do this. And here is what my code looks like right now:

 Thread g1 = new MyThread(i,j); g1.setPriority(Thread.MAX_PRIORITY); g1.run(); Thread g2 = new MyThread(j,i); g2.setPriority(Thread.MAX_PRIORITY); g2.run(); Thread g3 = new MyThread(i,j); g3.setPriority(Thread.MAX_PRIORITY); g3.run(); if (g1.isAlive()) { g1.join(); } if (g2.isAlive()) { g2.join(); } if (g3.isAlive()) { g3.join(); } 
+6
source share
3 answers

You are not actually using streams.

You need to call .start() , not .run() .

+23
source

This has nothing to do with processors - you do not actually start 3 threads, you start everything in the main thread. To start a thread, call its start() method, not run() .

+11
source

First, as others say, you are not using multiple threads. This is because you are calling the run () method, which finishes doing work on the calling thread.

Now, to solve the rest of your question, what I would like to say is how to maximize the efficiency of a multi-threaded process. This is not an easy question, but I will give you the basics. (Others, feel free to call.)

The best way to maximize the efficiency of your process is to try to make all threads approximately the same, and try to keep them from blocking. That is, your task is to "balance" the workload so that the application works efficiently.

In general, you cannot designate a thread to work on a specific CPU core; which is usually the task of the OS and the CPU itself. The OS plans the process (using the priorities that you provide), and then the CPUs can do their own planning at the instruction level. Besides setting priorities, the rest of the planning is not completely controlled.

EDITOR: I am attached to semicolons.

+6
source

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


All Articles