aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread" Watch "Thread" New topic
Author

Thread

Divakar
Greenhorn

Joined: Mar 01, 2000
Posts: 25
See the following code.

public class ThreadTest
{
public static void main(String argv[])
{
child1 c1=new child1();
child2 c2=new child2();

c1.start();
c2.start();
System.out.println("main");

}
}
class child1 extends Thread
{
public void run()
{
System.out.println("Child1");
}
}
class child2 extends Thread
{
public void run()
{
System.out.println("Child2");
}
}

This code prints
main
Child1
Child2
Why it is printing like this
Child1
Child2
main
is the correct order right??
When I am calling c1.start() it will call run method of the Child1 . This will print Child1.Similarly for Child 2. After this it will print main.
Don't know what is happening??
Please help.

Marc
Greenhorn

Joined: Mar 10, 2000
Posts: 11
Hi Divakar,
c1.start();
c2.start();
System.out.println("main");
calls to start threads are asynchronous. In this instance, this means the main() thread registers a call with the JVM to start c1 i.e. set up the c1 thread and call its run() method, immediately goes on to do the same for c2 without waiting until c1 has actually started, then immediately continues execution, i.e. prints "main", without waiting for c2 to start.
Meanwhile, the JVM allocates memory etc. for c1 and c2 when it can find the time and resources. You cannot even guarantee whether c1 will initialise before c2; it all depends on resource management. Getting the threads running will always take much longer than it takes the main() thread to rush through the println statement; hence you are getting that output first.
Hope this helps
Marc
Divakar
Greenhorn

Joined: Mar 01, 2000
Posts: 25
Thanx Marc.
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
The "lesson" to be learned here is that you cannot rely on this type of non-existent "order" for the correctness of algorithms. That's probably one of the most basic caveats that one sees when studying Threads...


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Is it possible in some cases that the display for c2 preceeds c1 just because of the thread scheduler's decision. Since the start tells the thread to be in ready state and the Scheduler determines which one to execute first or so....

Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Yup. Try the following:

If you look closely at the output, you'll probably see a few threads that appear out of order. If not, run it again - the results can be different each time.

"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread