Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread and main()

 
Vivian Josh
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a question from examulator mock exam.

Q :Which of the following statements are true?

1.When the main method of a program finishes running, all other Threads started within that program will exit

2.A thread may be prevented from executing because another thread has a higher priority.

3.Both the Thread class and the Runnable interface provide versions of the yield method

4.A thread may be prevented from executing because the sleep method is called.

Author's answer : 2 and 4

My answer : 1,2 and 4


Doubt : When execution pointer is out of main() method how can any other thread continue running? Doesn't main() method is last to finish and all the threads created within main() will stop running before exiting main() ?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write a program to create some long-running (or long-sleeping) threads and let the main program exit. You will see that the threads will continue to run (or sleep) after the main thread has ended.

Then make the threads daemon threads by calling setDaemon(true) on the threads before starting them. You will notice the difference.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My answers 2,4;


Doubt : When execution pointer is out of main() method how can any other thread continue running? Doesn't main() method is last to finish and all the threads created within main() will stop running before exiting main() ?



Here I see something, that you need to study that is daemon threads (not on
the objectives of SCJP 5.0).


Thanks,
[ June 27, 2007: Message edited by: Chandra Bhatt ]
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You can refer about the Daemon Threads here.



Regards
Nik
SCJP 1.5
 
Vivian Josh
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Barry Gaunt:
Write a program to create some long-running (or long-sleeping) threads and let the main program exit. You will see that the threads will continue to run (or sleep) after the main thread has ended.

Then make the threads daemon threads by calling setDaemon(true) on the threads before starting them. You will notice the difference.



Thanks Barry !

I run the example. My doubt is clear now.
 
Vivian Josh
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by nik arora:
Hi,
You can refer about the Daemon Threads here.






Thanks for the link nik ! Its useful.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic