Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

dual core?  RSS feed

 
Gotham Sewani
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created the following java code:
class MyThread implements Runnable {
MyThread() {
Thread t=new Thread(this,"MyThread");
t.start();
}

public void run() {
for(long i=0;i<200000000L;i++);
}


}

class ThreadTest {
public static void main(String args[]) throws Exception {
long a=System.currentTimeMillis();
//line1 MyThread mt=new MyThread();
//line2 MyThread mt2=new MyThread();


for(long i=0;i<200000000L;i++);

long b=System.currentTimeMillis();
System.out.println((b-a));
}
}

Got the output at 1198.
Then I removed the comment at line 1 and the output was 1218.
Then I removed the comment at line 2 and the output was 1843.
Now my question is,there was'nt much difference in the running time of the program when I created one extra thread...but there was a large difference when I created two extra threads.Why is that?
Is it because I am running this on a dual core processor?
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
em ...

Your timing how long to create and start two other threads (but not complete them) and then complete a loop competing for CPU resources with the other two/one new threads and your OS. To be even half fair to the JVM shouldn't you at least join on the other threads and time all three threads completion, even then your into what the rest of your OS wants CPU time for. The amount of work the other two threads do prior to the system.out is a bit unpredictable particularly if you have mor ethan one CPU.

Yes Dual core would explain the times but I'ed like to see join used to make the test meaningful (at least to me)
 
Gotham Sewani
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey chris..sorry for the missing join statements..actually I had created two 'versions' of this..and by mistake I posted the earlier one..

But I tried with the join() statements also,and the results are the same.Maybe I am gonna try this on a single core pc to check if my idea is correct..but surprisingly they are very difficult to find nowadays.
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well assuming your join is were I'ed expect to be wouldn't that just be as expected then i.e. you have two cores so with two threads your getting similar-ish times (ignoring thread creation) but adding a third thread means a thread has to wait for CPU time.

It looks like the OS is preforming quite well then normally with dual core its a complaint that there is not enough of a preformance gain ;-)

Presuming your on Windows I believe task manger should show 50 per cent for such a tight loop with 1 and 2 removed when you add 1 it should hit 100 per cent and still be the same for adding three. Note as has been explained in other message threads here other processes OS tasks will obviously make a big difference to what you actually see depening on whats running.
 
Chris Beckey
Ranch Hand
Posts: 116
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I took your example and did a little experiment comparing single versus dual core machines and from 1 to 50 threads. I changed your example a bit and had one "master" thread that just waited for the "worker" threads to complete (there were from 1 to 50 workers). Anyway, the results were basically linear growth in execution time with the single core machine almost exactly half as fast. The dual-core machine showed a distinctive flattening from 1 to 2 threads that the single-core machine did not show (i.e. 1 and 2 threads were about the same, 3 threads and up gre linearly).
Basically it was exactly as would be expected ... which is always pleasently surprising.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!