Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Thread

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

Thread

You can define and instantiate a thread in one of two ways:

 Extend the java.lang.Thread class.


 Implement the Runnable interface.

Extending java.lang.Thread

The simplest way to define code to run in a separate thread is


to

 Extend the java.lang.Thread class.


 Override the run() method.

EX:-
class MyThread extends Thread {
public void run() {
System.out.println("Important job running in
MyThread");
}
}

Implementing java.lang.Runnable

Ex:-
class MyRunnable implements Runnable {
public void run() {
System.out.println("Important job running in
MyRunnable");
}
}
Thread States

A thread can be only in one of five states

 New This is the state the thread is in after the Thread


instance has been created, but the start() method has not
been invoked on the thread. It is a live Thread object, but
not yet a thread of execution.

 Runnable This is the state a thread is in when it's eligible


to run, but the scheduler has not selected it to be the
running thread. A thread first enters the runnable state
when the start() method is invoked, but a thread can also
return to the runnable state after either running or
coming back from a blocked, waiting, or sleeping state.
When the thread is in the runnable state, it is considered
alive.

 Running This is the state a thread is in when the thread


scheduler selects it to be the currently executing process.
one way to get to the running state: the scheduler chooses
a thread from the runnable pool.

 Dead A thread is considered dead when its run() method


completes. It may still be a viable Thread object, but it is
no longer a separate thread of execution. Once a thread is
dead, it can never be brought back to life. If you invoke
start()on a dead Thread instance, you'll get a runtime
exception.
Thread Priority

Thread.MIN_PRIORITY (1)
Thread.NORM_PRIORITY (5)
Thread.MAX_PRIORITY (10)

OR

threadobj.setPriority(int value);
threadobj.getPriority();

Thread Synchronization

When a thread is already action on an object, preventing any


other thread from acting on the same object is called Thread
synchronization or Thread Safe.

There two ways of doing so.

 Using Synchronize Block


Ex:-
synchronized (object) {

Statement ;
}

 Using synchronized keyword in method declaration.

Ex:-
synchronized void display() {

Ststement;
}

sleep() Method

The sleep() method is a static method of class Thread.


You use it in your code to "slow a thread down" by forcing it
to go into a sleep mode before coming back to runnable .

You do this by invoking the static Thread.sleep() method,


giving it a time in milliseconds as follows:

try {
Thread.sleep(5*60*1000); // Sleep for 5 minutes
}catch (InterruptedException ex) { }

Notice that the sleep() method can throw a checked


InterruptedException

The yield( ) Method

The yield() method is supposed to do is make the currently


running thread head back to runnable to allow other threads
of the same priority to get their turn. So the intention is to use
yield() to promote graceful turn-taking among equal-priority
threads.

Thread.yield();// yield is static method of Thread class.


The join( ) Method

The non-static join() method of class Thread lets one thread


"join onto the end" of another thread. If you have a thread B
that can't do its work until another thread A has completed its
work, then you want thread B to "join" thread A. This means
that thread B will not become runnable until A has finished
(and entered the dead state).

 The methods wait() , notify(), and notifyAll() are methods


of
java.lang.Object.

 The methods static :-- sleep() and yield(), and which are
not static:--join() and start()are methods of
java.lang.Thread.

 The method run()is a method of java.lang.Runnable


Interface.

You might also like