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

Thread running behaviour

 
kapil munjal
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Atul Chandran
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kaps, what do you expect that new StartRun().start(); will do? Start a new thread?
[ October 31, 2004: Message edited by: Barry Gaunt ]
 
kapil munjal
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic