Thread and their state



A Thread is a single sequence of execution that can run independently in an application. Knowledge of threads in programs is useful in term of resource utilisation of the system on which an application is running. Multithreaded programming is very useful in network and internet applications developments. Multithreaded programs support more than one concurrent thread of execution. This means they are able to simultaneously execute multiple sequences of instructions. Each instruction sequences have its own unique flow of control that is independent of all others. These independently executed instruction sequences are known as threads.
In single processor system, only a single thread of execution occurs at a given instant, but multiple threads in a program increase the utilization of CPU.



In the core of thread is an interface name Runnable which is implemented by Thread class. So in java a Thread is created in two ways:

    Create a subclass of Thread class and override run() method
    Create a class by implements Runnable interface and define it’s (only) run method.

Constructor of Thread class:

public Thread()

public Thread(String name)

public Thread(Runnable target)

public Thread(Runnable target, String name)

public Thread(ThreadGroup group, Runnable target)

target:   the object whose run method is called.

Commonly used method of Thread class:

currentThread()

Returns a reference to the currently executing thread object; return the currently executing thread.

   

    public static native Thread currentThread();

activeCount()

Returns the number of active threads in the current thread's thread; return  the number of active threads in the current thread's thread

    public static int activeCount() {

          return currentThread().getThreadGroup().activeCount();

    }

getName()

Returns this thread's name. return  this thread's name.

  #setName(String)

    public final String getName() {

          return String.valueOf(name);

    }

setName():Changes the name of this thread

public final void setName(String name)

setDaemon():Marks this thread as either a daemon thread or a user thread. The Java Virtual Machine exits when the only threads running are all daemon threads.

public final void setDaemon(boolean on)

setPriority():Changes the priority of this thread.

public final void setPriority(int newPriority)

getPriority()               [max priority-10, min priority-1, default priority-5]

Returns this thread's priority; this thread's priority.

     #setPriority  

    public final int getPriority() {

          return priority;

    }

getState()

Returns the state of this thread. This method is designed for use in monitoring of the system state,

not for synchronization control; return this thread's state.

    public State getState() {

        // get current thread state

        return sun.misc.VM.toThreadState(threadStatus);

    }

getThreadGroup()

Returns the thread group to which this thread belongs. This method returns null if this thread has died

     (been stopped).

   

    public final ThreadGroup getThreadGroup() {

          return group;

    }

interrupted()

Tests whether the current thread has been interrupted. The interrupted status of the thread is cleared by this method.

public static boolean interrupted() {

          return currentThread().isInterrupted(true);

    }

interrupt()

public void interrupt()

isDaemon()

Tests if this thread is a daemon thread.

public final boolean isDaemon() {

          return daemon;

    }

isInterrupted()

Tests whether this thread has been interrupted.  The interrupted status of the thread is unaffected by this method.

public boolean isInterrupted() {

          return isInterrupted(false);

    }

Join()

It is used to suspend the current thread till the completion of invoking thread or till the expiration of specified time.

public final synchronized void join(long millis)

public final void join() throws InterruptedException

start()

public synchronized void start()

Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

sleep(long millis)

public static native void sleep(long millis) throws InterruptedException

Causes the currently executing thread to sleep (temporarily cease  execution) for the specified number of milliseconds

run():

This is the only non-final method of thread class. It represents a thread i.e. Statements of run method are executed as a thread by the jvm.

public void run() {

          if (target != null) {

              target.run();

          }

    }

public static native void sleep(long millis) throws InterruptedException;

public synchronized void start()

yield()

Causes the currently executing thread object to temporarily pause and allow other threads to execute.

It is used by thread to volunterially realize the control without being suspended.

public static native void yield()

No comments:

Post a Comment