aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread and main() 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 and main()" Watch "Thread and main()" New topic
Author

Thread and main()

Vivian Josh
Ranch Hand

Joined: Oct 31, 2006
Posts: 112
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

Joined: Aug 03, 2002
Posts: 7729
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.


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

Joined: Feb 28, 2007
Posts: 1707
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 ]

cmbhatt
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi,
You can refer about the Daemon Threads here.



Regards
Nik
SCJP 1.5
Vivian Josh
Ranch Hand

Joined: Oct 31, 2006
Posts: 112
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

Joined: Oct 31, 2006
Posts: 112
Originally posted by nik arora:
Hi,
You can refer about the Daemon Threads here.






Thanks for the link nik ! Its useful.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread and main()