Java VM thread parallelism on Red Hat

Although this is a topic that's been talked about thousands of times over, I get so much conflicting information on the forums (simply because hardware configurations and Java VMs vary perhaps).

I am running a multiprocessor/multicore Red Hat Linux box with Java 6 - the burning question is, will the Java VM execute my Java threads in PARALLEL, automatically, with no additional arguments to javac or java? I've written a simple program which spawns 4 threads, and while(true) spins and writes to standard out...this is only revealing one process using TOP. But it could be that the operations aren't expensive enough to warrant a second or third CPU kicking it: please shed some light on what it takes to have my threads run parallel in Linux. It seems difficult to test definitively. Thanks.

[810 byte] By [aliasneo777a] at [2007-11-26 16:36:16]
# 1

In general the answer is YES. But, there are several issues here.

First of all

- do You mean "in PARALLEL" on different CPU?

if Yes consider this:

- running similar threads (forked LWT) on different CPU is relatively expensive due to cache sync;

- just switch to another CPU is penalized by scheduler;

So, You will not get threads on different CPUs until one is tottaly occupied by some of the threads. As far as You have system io called in those threads it bloks and lets other threads to run on the same unit.

It seems ps fits better to watch them.

_Dima_a at 2007-7-8 23:01:12 > top of Java-index,Core,Core APIs...
# 2

> Although this is a topic that's been talked about

> thousands of times over, I get so much conflicting

> information on the forums (simply because hardware

> configurations and Java VMs vary perhaps).

>

> I am running a multiprocessor/multicore Red Hat Linux

> box with Java 6 - the burning question is, will the

> Java VM execute my Java threads in PARALLEL,

> automatically, with no additional arguments to javac

> or java? I've written a simple program which spawns 4

> threads, and while(true) spins and writes to standard

> out...this is only revealing one process using TOP.

> But it could be that the operations aren't expensive

> enough to warrant a second or third CPU kicking it:

> please shed some light on what it takes to have my

> threads run parallel in Linux. It seems difficult to

> test definitively. Thanks.

1. The threads may be blocked on System.out. Only do System.out once in a while in each thread.

2. Using top and program below. On my 2 dual cores, top shows cpu usage close to 100% and load close to 4.0. The load of 4.0 is a better indicator of 4 cpu usage.

Here is some code that I posted before that Spins each of your CPU and occasionally outputs to System.out. The % operator prevents it from doing System.out each time through the loop.

public class Spin implements Runnable {

public static void main(String[] args) {

int threads = Runtime.getRuntime().availableProcessors();

if (args.length > 0) {

threads = Integer.parseInt(args[0]);

}

for (int i=0; i<threads; i++) {

new Thread(new Spin(),"Spin " + i).start();

}

}

public void run() {

for(long i=0; i><10000000000; i++) {

if (i % 10000000 == 0) {

System.out.println("Thread \"" + Thread.currentThread().getName() + "\" is on iteration " + i);

}

}

}

}

Caffeine0001a at 2007-7-8 23:01:12 > top of Java-index,Core,Core APIs...
# 3

And as _Dima_ stated you can also use ps.

Examples below: Use your process id for java instead of 13178

This will show CPU used as a percentage usage since it started.

ps -o%cpu,time -p 13178

This will show each thread and what processor each thread is on.

ps -L -olwp,nlwp,psr,%cpu,time -p 13178

Caffeine0001a at 2007-7-8 23:01:12 > top of Java-index,Core,Core APIs...
# 4

> Although this is a topic that's been talked about

> thousands of times over, I get so much conflicting

> information on the forums (simply because hardware

> configurations and Java VMs vary perhaps).

>

> I am running a multiprocessor/multicore Red Hat Linux

> box with Java 6 - the burning question is, will the

> Java VM execute my Java threads in PARALLEL,

> automatically, with no additional arguments to javac

> or java? I've written a simple program which spawns 4

> threads, and while(true) spins and writes to standard

> out...this is only revealing one process using TOP.

> But it could be that the operations aren't expensive

> enough to warrant a second or third CPU kicking it:

> please shed some light on what it takes to have my

> threads run parallel in Linux. It seems difficult to

> test definitively. Thanks.

You are running multiple threads, not multiple processes. So you will not see new processes using TOP. There is probably a way to show load on each CPU. Other than that, you are correct, you can not see for sure that your processes are running on multiple CPU simultaneously in a preemptive multitasking OS. Its abstracted away from you.

_dnoyeBa at 2007-7-8 23:01:12 > top of Java-index,Core,Core APIs...
# 5

> You are running multiple threads, not multiple

> processes. So you will not see new processes using

> TOP. There is probably a way to show load on each

> CPU.

With the TOP I am using, I can break out the CPU usage per CPU by pressing the '1' key while TOP is running

Caffeine0001a at 2007-7-8 23:01:12 > top of Java-index,Core,Core APIs...
# 6

Sun's JVM uses native threads on all platforms. If your OS schedules native threads across multicores/multiprocessors then you get that in the JVM. What actually happens in the run of a program depends on all the things others have discussed. But you don't need to do anything special when invoking java.

davidholmesa at 2007-7-8 23:01:12 > top of Java-index,Core,Core APIs...