6

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();
}
Gray
  • 115,027
  • 24
  • 293
  • 354
uncle Lem
  • 4,954
  • 8
  • 33
  • 53
  • 2
    Use `.start()` as others have pointed out -- and then you *probably* want to leave the scheduling to the scheduler instead of playing with priorities yourself. Unless you know *quite* a lot about thread scheduling, chances of doing the scheduler's job better than it does are slim (at best). – Jerry Coffin May 08 '12 at 21:57
  • possible duplicate of [java thread - run() and start() methods](http://stackoverflow.com/questions/3027495/java-thread-run-and-start-methods) –  May 08 '12 at 22:04

3 Answers3

23

You aren't actually using threads.

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

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
11

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

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
6

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.

Tom
  • 18,685
  • 15
  • 71
  • 81