• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Marc.
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....

 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic