Java Quick Reference
  Language Fundamentals
  Operators and Assignments
  Flow Control and Exceptions
  Declarations and Access Control
  Garbage Collection
  Overloading and Overriding
  Threads
  The java.lang Package
  The java.util Package
  The java.awt Package
  The java.io Package
  References
  Miscellaneous Notes
  Tips & Traps
  Mock Exams

Threads - Ending a Thread

  • a thread normally ends when it's execution completes
  • there are other methods of stopping it, some of which should not be used

interrupt()

  • interrupting a thread tells it that you want it to pay attention
  • it does not force the thread to halt although it will wake up a sleeping thread
  • isInterrupted checks to see if a thread is in an interrupted state
  • the static method interrupted can be used to clear a thread's interrupted state
  • if a thread is waiting or sleeping and the thread is interrupted, the methods wait() and sleep() will throw an InterruptedException

join()

  • one thread can wait for another to complete using the join() method
  • invoking join() guarantees that the method will not return until the threads run() method has completed
  • join() will also take a milliseconds argument which will cause it to wait until the thread completes for the designated time period

destroy()

  • destroy() kills a thread dead without releasing any of it's locks which could leave other threads blocked forever
  • it's use should be avoided

stop()

  • you can force a thread to end by calling stop() which in turn throws the Error ThreadDeath
  • you can also throw ThreadDeath yourself
  • ThreadDeath SHOULD NOT BE CAUGHT!
  • NOTE: stop() is a deprecated method and should not be used!!!

suspend() and resume()

  • both methods are deprecated and should not be used!!

setDaemon(true)

  • if you want your thread to stop executing when main() completes, set it's daemon status to true using setDaemon(true)

yeild()

  • Java does not time-slice ie it will not preempt a currently executing thread to allow another thread of the same priority to run
  • the operating system may use time-slicing but you should not rely on time-slicing when creating threads
  • a well behaved thread will use the yield() method to voluntarily yield to the CPU, giving it a chance to run another thread of the same priority.
  • if no threads of the same priority are available, execution of the yielding thread continues.
  • Note: lower priority threads are ignored.
  • the yield() method only hints to the JVM that if there are other runnable threads the scheduler should run one in place of the current thread. The JVM may interpret this hint any way it likes ie how the yield is handled is dependent on the JVM implementation for the operating system

Also see

Sun Tech Tip: Handling Interrupted Exceptions

Overview Thread Class Runnable Interface Thread States Scheduling Ending a Thread
  Execution Synchronization Locking Protocols synchronized keyword wait() notify(), notifyAll()
  Thread Mechanics