Hi,
Please see the code below.I have a query regarding this at the end of this code.
This is taken from complete reference book.
//create a second
thread The output of this code is:
Child thread:Thread[Demo Thread,5,main]
main thread:5
Child thread:5
Child thread:4
Child thread:3
main thread:4
Child thread:2
main thread:3
Child thread:1
Exiting Child thread
main thread:2
main thread:1
Exiting main thread
This output is not clear to me.As far as i understand when a thread is created it start when its start() method is called because the start method calls the overridden method run(). So when the demo thread is created in the NewThread constructor then the start method calls the run and hence FOR loop should be executed once before going to sleep and after that the for loop for MAIN thread should be executed once.so the output as I understand should be but it is not so Child thread:Thread[Demo Thread,5,main]
Child thread:5
main thread:5
Child thread:4
Child thread:3
main thread:4
Child thread:2
main thread:3
Child thread:1
Exiting Child thread
main thread:2
main thread:1
Exiting main thread [edit]Add code tags. CRit]
[ June 04, 2008: Message edited by: Campbell Ritchie ]