I have a program that performs a long-time computations, so I want to speed up its performance. So I tried to launch 3 threads at the moment, but java.exe
still occupies 25% of CPU usage (so, only one CPU is used), and it's remains even if I try to use .setPriority(Thread.MAX_PRIORITY);
and set priority of java.exe
at realtime (24). I tried to use RealtimeThread
but seems like it works even slower. It would be perfect if each thread was allocated to one processor and the total CPU usage has increased to 75%, but I don't know how to do it. And that's how my code looks 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();
}
Cores is an actual hardware component whereas thread is a virtual component that manages the tasks. Cores use content switching while threads use multiple CPUs for operating numerous processes. Cores require only a signal process unit whereas threads require multiple processing units.
In single-processor systems, only a single thread of execution occurs at a given instant. The CPU quickly switches back and forth between several threads to create the illusion that the threads are executing at the same time. Single-processor systems support logical concurrency, not physical concurrency.
Instead of giving a large workload to a single core, threaded programs split the work into multiple software threads. These threads are processed in parallel by different CPU cores to save time.
Since only one processor is online, windows will assume that your CPU has only one processor. Therefore, from the Microsoft configuration window, you will only have a drop down showing one processor. This is the same scenario when you choose to run a smaller number of processors than are available for your system.
You aren't actually using threads.
You need to call .start()
, not .run()
.
This has nothing to do with CPUs - you're not actually starting 3 threads, you're running everything on the main thread. To start a thread, call its start()
method, not run()
.
First, as the others suggest, you're not really using multiple threads. This is because you're calling the run() method, which ends up doing the work in the calling thread.
Now, to address the rest of your question, which I take to mean how does one maximize the efficiency of a multithreaded process. This isn't a simple question, but I'll give you the basics. (Others, feel free to chime in.)
The best way to maximize the efficiency of your process is to try to make all of the threads do about the same amount of work, and to try to keep them from blocking. That is to say, it is your job to "balance" the workload in order to make the application run efficiently.
In general, you can't assign a thread to run on a particular CPU core; that's usually the job of the OS and the CPUs themselves. The OS schedules the process (using the priorities you provide) and then the CPUs can do their own scheduling at the instruction level. Besides setting the priorities, the rest of the scheduling is completely out of your control.
EDIT: I am addicted to semicolons.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With