File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread running behaviour Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread running behaviour" Watch "Thread running behaviour" New topic
Author

Thread running behaviour

kapil munjal
Ranch Hand

Joined: May 11, 2004
Posts: 298
Hi,

I was going through threads and tried a little code myself and found that it is quite difficult to understand the execution of main thread and child threads.

Please look at the following code...




Now, when I run this code it always prints.....

start method
Run method
Run method
Run method


Case 1.....
===========
If I change the main method like this..

public static void main(String[] args)
{

Thread t= new Thread(new StartRun());
t.start();
Thread t1= new Thread(new StartRun());
t1.start();
new StartRun().start();

Thread t2= new Thread(new StartRun());
t2.start();

}

It prints
start method
Run method
Run method
Run method

Case 2
=======

If I change the main method like this

public static void main(String[] args)
{
new StartRun().start();

Thread t= new Thread(new StartRun());
t.start();
Thread t1= new Thread(new StartRun());
t1.start();
Thread t2= new Thread(new StartRun());
t2.start();

}

It prints
start method
Run method
Run method
Run method


I am confused about the behaviour of execution of start method of StartRun class and the execution of threads.

Please someone explain this.

Thanks
Kaps
[ October 31, 2004: Message edited by: kapil munjal ]

Kapil Munjal
SCJP 1.4, SCWCD 1.4
Atul Chandran
Greenhorn

Joined: Oct 24, 2004
Posts: 22
There is no guarantee for the order in which the threads will be executed. It depends on the scheduler.
[ October 31, 2004: Message edited by: Atul Chandran ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Kaps, what do you expect that new StartRun().start(); will do? Start a new thread?
[ October 31, 2004: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
kapil munjal
Ranch Hand

Joined: May 11, 2004
Posts: 298
Barry, I am not expecting "new StartRun().start();" to start a new thread, but I am expecting that if I run "new StartRun().start()" at the end of the main method then it should print "start method" in the end and if I run the main method with this line in the middle of these threads then it should print "start method" in the middle. But it always prints "start method" in the beginning.

I have edited my last post as the output written in that was not right earlier.

I want to know,

does main thread finish first and then child threads run???(because in my example "start run" is printed always.

Or there is no specific order for that.

Actually the thing is I am confused about the order in which threads(main, child and daemon) will execute.

Barry, if you know some simple article or tutorial on that then please provide the link.

Thanks

Kaps
[ October 31, 2004: Message edited by: kapil munjal ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
OK. When your "new StartRun().start();" is before the creation and starting of the three threads it must print out before the threads (neglecting i/o buffer flushing behaviour). Why? Because it runs in the main thread and it must finish before the three threads are created and started.

If your "new StartRun().start();" is between or after the creation and starting of the threads, then you cannot predict where the output will be printed. Or even whether the threads will output in any particular order.

Thread scheduling is implementation dependent, and although you can play around with thread priorities, you cannot, in general, predict the running order of individual threads.
kapil munjal
Ranch Hand

Joined: May 11, 2004
Posts: 298
Barry, you mean to say that we can never predict which thread will execute first and which one is later. The only control mechanism we have is thread priorities.

Now, I got it.

Thanks Barry..

Kaps
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread running behaviour
 
Similar Threads
Thread code result compiler error
Thread Priority issue
start() kicks start() but not run() why?
threading issue
Will a started thread run to completion in this example?