| | |
    Java Beginner Home
    Table of Contents
    Introduction to Java
    Getting Started with Java
    Basic Language Elements
    Java Operators
    Java Control Statements
    Java Access Modifiers
    Classes and Objects
    Java Constructors
    Object Serialization
    Java Class Inheritance
    Java Object Type Casting
    Abstract class and Interface
    Java Method Overiding
    Java toString Method
    Java String Class
    Java toString Method
    Java String Comparison
    Java StringBuffer
    Java Exceptions
    Singleton Pattern
    Java Threads Tutorial
    Java Collections Framework
    Java Date Util
    Swing Tutorial
    Feedback
    Java books

Java Threads Tutorial

Thread States

   A Java thread is always in one of several states which could be running, sleeping, dead, etc.

A thread can be in any of the following states:

  • New Thread state (Ready-to-run state)
  • Runnable state (Running state)
  • Not Runnable state
  • Dead state

New Thread

   A thread is in this state when the instantiation of a Thread object creates a new thread but does not
start it running. A thread starts life in the Ready-to-run state. You can call only the start() or stop()
methods when the thread is in this state. Calling any method besides start() or stop() causes an
IllegalThreadStateException.

Runnable

   When the start() method is invoked on a New Thread() it gets to the runnable state or running state by
calling the run() method. A Runnable thread may actually be running, or may be awaiting its turn to run.

Not Runnable

A thread becomes Not Runnable when one of the following four events occurs:

  • When sleep() method is invoked and it sleeps for a specified amount of time
  • When suspend() method is invoked
  • When the wait() method is invoked and the thread waits for notification of a free resource or waits for
    the completion of another thread or waits to acquire a lock of an object.
  • The thread is blocking on I/O and waits for its completion

Example: Thread.currentThread().sleep(1000);

Note: Thread.currentThread() may return an output like Thread[threadA,5,main]

The output shown in bold describes

  • the name of the thread,
  • the priority of the thread, and
  • the name of the group to which it belongs.

   Here, the run() method put itself to sleep for one second and becomes Not Runnable during that period.
A thread can be awakened abruptly by invoking the interrupt() method on the sleeping thread object or at the end of the period of time for sleep is over. Whether or not it will actually start running depends on its priority and the availability of the CPU.

   Hence I hereby list the scenarios below to describe how a thread switches form a non runnable to a runnable state:

  • If a thread has been put to sleep, then the specified number of milliseconds must elapse (or it must be interrupted).
  • If a thread has been suspended, then its resume() method must be invoked
  • If a thread is waiting on a condition variable, whatever object owns the variable must relinquish it by calling
    either notify() or notifyAll().
  • If a thread is blocked on I/O, then the I/O must complete.

    Dead State

       A thread enters this state when the run() method has finished executing or when the stop() method is invoked. Once in this state, the thread cannot ever run again.

     
    Page 3 of 5 Previous
    3
      Next

     

  •     Java is a trademark of Sun Microsystems, Inc.
    | | |
    © Copyright 2007-08 javabeginner.com