Friday, 25 November 2011

Thread class



constructors :
Thread()
Thread(Runnable obj)
Thread(Runnable obj,String s)
Thread(String s)

Here, obj is a reference to an object that implements the Runnable interface and s is a String used to identify the Thread.



Methods of Thread class
Operation
Explanation
thread.start ( );
the thread becomes ALIVE and starts executing
its run method in a new context (PC, stack)
b = thread.isAlive ( );
returns true if the thread is in the ALIVE state
thread.interrupt ( );
either the interrupted flag is set, or, if the thread is
waiting or sleeping, throws InterruptedException
b = thread.isInterrupted ( );
returns true if the interrupted flag is set
otherThread.join ( );
otherThread.join (millis);
wait until the other thread finishes (not isAlive ( ))
or the given time passes
thread.setPriority (i);
i
= thread.getPriority ( );
set or get the thread's priority, respectively;
a priority is only a hint to the scheduler
Static Thread Methods
concern the thread that is currently running a CPU
curr = Thread.currentThread ( );
returns the current thread
b = Thread.interrupted ( );
returns true if the interrupted flag of the current thread
is set, and clears the interrupted flag
Thread.sleep (millis);
Thread.sleep (millis, nanos);
the current thread enters the BLOCKED state and sleeps
(at least) the given amount of milliseconds (+ nanos)
Thread.yield ( );
may give other threads a "chance" to run; the current
thread still remains in the
RUNNABLE state


The Main Thread

When a Java program starts up, one thread begins running immediately. This is usually called the main thread of the program, because it is the one that is executed when your program begins. The main thread is important for two reasons:

1.     It is the thread from which other “child” threads will be spawned.

2.     Often it must be the last thread to finish execution because it performs various shutdown actions.



Programmers can control the main thread by first creating a Thread object and then using method members of the Thread object to control the main thread. The  Thread object is created by calling the method currentThread( ), which is a public static member of Thread. The  general form of this method is as follow:

static Thread currentThread( )

This method returns a reference to the thread in which it is called. Once you have a reference to the main thread, you can control it just like any other thread.

Ex,
class CurrentThreadDemo
{
  public static void main(String args[])
  {
    Thread t = Thread.currentThread();

    System.out.println("Current thread: " + t);

    // change the name of the thread
   
    t.setName("My Thread");
    System.out.println("After name change: " + t);

    try
    {
      for(int n = 10; n > 0; n--)
       {
        System.out.println(n);
        Thread.sleep(1000);
      }
    }
   catch (InterruptedException e)
   {
      System.out.println("Main thread interrupted");
    }
     }
}

Posted by :Ruchita Pandya

2 comments: