Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Thread and main()

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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() ?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
No more fooling around. Read this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic