Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my threaded program only using one CPU?

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();
}
like image 441
uncle Lem Avatar asked May 08 '12 21:05

uncle Lem


People also ask

Can a threads be run on multiple CPUs?

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.

Does only one thread run at once?

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.

Does threading use multiple cores?

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.

Why is my number of processors only 1?

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.


3 Answers

You aren't actually using threads.

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

like image 112
SLaks Avatar answered Oct 13 '22 22:10

SLaks


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().

like image 24
Michael Borgwardt Avatar answered Oct 13 '22 22:10

Michael Borgwardt


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.

like image 26
Tom Avatar answered Oct 13 '22 22:10

Tom