• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

When does a Thread STOP executing?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,
I am a bit confused about this because I get different answers at different places.
Would be glad to know your views.
Regards,
Sandeep.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer depends on how you define 'stop'.

  • A thread can temporarily stop executing when it yields, waits, sleeps or when it is interrupted.
  • A thread can temporarily stop executing if for some reason the thread scheduler decides its time for the other threads to run - ie., for example in a time-slice scheduler this happens when the running thread has exhausted its quota of CPU time. In a priority based scheduler this can happen if there is another thread with a higher priority. In general, it is under the mercy of the scheduler.
  • A thread can permanently stop executing if there is an unhandled exception. This ofcourse is considered as anbormal termination.
  • A thread can gracefully stop executing when the run method completes.


  • ------------------
    Ajith Kallambella M.
    Sun Certified Programmer for the Java�2 Platform.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your sandeep_meher does not comply with the JavaRanch naming policy. Please choose one that meets the requirements.

------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
a thread stops
  • when a call is made to System.exit()

  • when another thread is given higher priority

  • when a call is made to the stop() method of the Thread class
  • Note: stop() is deprecated but I have still seen it is still a valid answer in the many of the mock exams.
    If anyone else can share some light on the whole stop() issue that'd be great.
    Hope this helps anyway
    Brian
     
    Ajith Kallambella
    Sheriff
    Posts: 5782
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    If it offers any comfort, you shouldnt be getting questions that involves depricated methods, atleast not in the revamped certification exam.
    stop() is a depricated method and there is a very nice article that tells you why stop has been depricated. You can get to the article from the Java API documentation for the stop method. Check it out.

    ------------------
    Ajith Kallambella M.
    Sun Certified Programmer for the Java�2 Platform.
     
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ajith,
    You said "A thread can temporarily stop executing when it yields, waits, sleeps or when it is interrupted."
    Do you mean interrupted using the interrupt() method?
    But I think that just sets the interrupt flag and it is upto the thread's discretion to action upon that using the method interrupted() or isInterrupted(). Will throw InterruptedException if the thread is using sleep(), join(), or wait() but anyway does not stop the thread.
    Regards,
    Sandeep.
     
    Come have lunch with me Arthur. Adventure will follow. This tiny ad:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic