About Me

My photo
You can get about me from my albums ... http://picasaweb.google.com/abhishek.mehta

Monday, April 2, 2007

Threads

In recent days I have worked on Threads that makes me to write a blog on it. Here I am sharing some of the basic characteristics of Threads.

Some Definitions

In an operating system a running program is known as Process which internally can have separate runnable tasks. These tasks are knows as Threads so we can say that thread is single sequential flow of process and it is also known as lightweight process.

If there are multiple programs or processes are executed it is known as Multi-Programming and when there is concurrency exists among different threads it is known as Multi-Threading.

Thread

Every Java program has at least one thread which is executed when the main() is invoked. Java also supports Multi-Threading for that it has class ( java.lang.Thread ) and interface ( java.lang.Runnable). It uses java.lang.Object class to call methods like wait() , notify() and notifyAll(). It also has concept of synchronize on an Object which internally provides a facility that only one thread can access that object at any given point in time.

Note : You can't change threads status after it has been started.

Creating a Thread

Java provides two way to instantiate a thread
1. By extending it from Thread class
class myThread extends Thread {
myThread() {
}
}

2. By implementing it from Runnable interface
class myThread implements Runnable {
myThread() {
}

public void run() {

}
}

This is helpful when your class is already extending any other class.

You can also give a particular name to Thread.
Thread myThread = new Thread("MyThread");

If you do not provide any name in that case it will classify Thread name as Thread-m where m is an integer which will vary from 0 to m.

Java also provides a facility to group particular Threads by ThreadGroup class. By this we can handle multiple threads as a unit , all threads in same group can access information about other threads within that group.

Starting a Thread
To actually start a thread you must invoke its start() method. This method internally allocate required system resources and call run() method of the Thread class. You are suppose to override run() method according to your requirement.

Each thread has life cycle and during this life cycle it will be in one of the following state :
1. New
2. Runnable
3. Not Runnable
4. Dead

Calling new will create a new Thread but it will not allocate any system resources to it until we call start() method. One thing you must notice that after calling new you can only able to call start() method otherwise it will throw IllegalThreadStateException.

once the run() method is called the state of Thread becomes Runnable

There are 3 possible way to make a thread to Non Runnable state
1. sleep()
2. wait()
3. Blocked for I/O

It will gain the Runnable state whenever it meets the required conditions. Following possible ways may change the state from Non Runnable to Runnable
1. notify() or notifyAll()
2. When called milliseconds in sleep() method elapsed.
3. When it get the access to Blocked I/O resource.

Dead state will occur when thread comes out from run() method or anyone call destroy() method for that thread.










One thing you must notice that interrupt doesn't kill any thread and any thread can jump from Runnable to Non Runnable or vice versa from two methods
1. Scheduling
2. programming control

Scheduling
It is nothing but execution of multiple threads in order. Java uses priority algorithms for scheduling. High priority threads will have preference on low priority threads. It doesnt mean that high priority thread will run all time it depends on the OS. Scheduling is of two types
1. Preemptive scheduling: In this higher thread will get preference until it gets over only other higher level thread can make that thread to wait.
2. Time slicing : In this each thread get the access for a scheduled period of time.

Ending a Thread
A thread normally ends when its execution completes but there are other methods to stop the execution of running thread.
1. interrupt()
2. join()
3. stop()
4. destroy()
5. suspend() and resume() : depricated
6. yield()
7. setDemon(true)

These are the basic characteristics of threads.

To read more on threads
http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html

Next blog will be on : How Thread Synchronization works.......

No comments: