Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Two thread slow on dual core?

 
Saverio Miroddi
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a simple class which converts data in two different ways and checks if the output is the same.

The strange thing is I have a dual core(Centrino 1.66), and if I launch a thread only:

new Check((byte)-128, (byte)127).start();

it takes about 800 ms for every cycle, with a CPU occupation of about 50-60% total.

so i thought to launch two threads splitting the work:
new Check((byte)-128, (byte)0).start();
new Check((byte)-128, (byte)0).start();

now it takes about 600 ms for every cycle of every thread, and a CPU occupation of about 70-80%.

so I have two questions:

- why occupation doesn't rise up to 100%?
- why the JVM doesn't manage the two thread correctly, even if they're totally separated?

assertion are disable (i don't care about routine correctness).
i post the source code (very simple) down.

bye!
saverio


class Check extends Thread {
private byte start, end;
private long lastTime;

public Check(byte start, byte end) {
this.start = start;
this.end = end;
}

public void run() {
lastTime = System.currentTimeMillis();
for (byte b1 = start; b1 < end; b1++) {
long now = System.currentTimeMillis();
out.println(now - lastTime);
lastTime = now;

for (byte b2 = (byte)-128; b2 < (byte)127; b2++) {
for (byte b3 = (byte)-128; b3 < (byte)127; b3++) {
for (byte b4 = (byte)-128; b4 < (byte)127; b4++) {
byte[] byteArr = new byte[] {
b1, b2, b3, b4
};
assert(vax2long(byteArr) == vax_to_long(byteArr)):
vax2long(byteArr) + "/" + vax_to_long(byteArr);
}
}
}
}
}

public final static long vax2long(byte[] b) {
long lngTmp =
(0x0FF & b[0]) |
(0x0FF & b[1]) << 8 |
(0x0FF & b[2]) << 16 |
(0x0FF & b[3]) << 24;

return lngTmp;
}

public final static long vax_to_long(byte[] b) {
long lngTmp =
(long) (0x0ffL & b[0]) |
((0x0ffL & (long) b[1]) << 8) |
((0x0ffL & (long) b[2]) << 16) |
((0x0ffL & (long) b[3]) << 24);

return lngTmp;

}
}
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why occupation doesn't rise up to 100%?
- why the JVM doesn't manage the two thread correctly, even if they're totally separated?


1. Probably the operating system has other things to do
2. What did you expect it to do?

Bill
 
Saverio Miroddi
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was wrong about timings (two threads go faster), however CPU occupation doesn't reach full load even if there aren't any other processes working.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
even if there aren't any other processes working.


That is not necessarily true - operating sytems do many things without consulting the user. My Windows systems occasionally start doing lots of disc operations for no apparent reason for example. The task manager shows loads of mysterious processes just hanging around.

Bill
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally to max out CPU's you go into tight loops and do lots of math ...


Check your OS e.g. does your kernel support your chip ...

Check other Apps....

Check shared resources between threads ...

i.e. check calls to static members , remove them all and re time

i.e. the first thing that occurred is what is 'out' wired up to, note your outputting in a loop, any form of I/O particularly shared I/O will drop the CPU utilisation depending on how its implemented i.e. if you only have one file log / VDU think about how the CPU's OS copes with that or not. The OS'es must be damm efficient, I/O buffers whatever as most multi threaded apps seem to pour logs down the same hole, personally I always time stamped my output and put them in per thread logs, as usually I only wanted to see ones thread output at a time and could always combine them back up if needed. I know some logs are ultra fast but I'ed love to see some stats in a multi threaded environment if some one has some ??


Would love to run some test figures but don't have the (multi core)hardware :-(
 
Saverio Miroddi
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you!
however, if you pass me some class, i can do tests for you :-)

saverio
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the OS is not going to yield complete control of the CPU to your program. So sometimes your processes/threads will be suspended for other processes/threads. These other processes/threads to not have to tax the CPU 100%. They can in fact, sit idle, but they still will get some of the CPU time.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic