Simply put, a thread is a program's path of execution. Most programs written today run as a single thread, causing problems when multiple events or actions need to occur at the same time. Let's say, for example, a program is not capable of drawing pictures while reading keystrokes. The program must give its full attention to the keyboard input lacking the ability to handle more than one event at a time. The ideal solution to this problem is the seamless execution of two or more sections of a program at the same time. Threads allows us to do this.
Multithreaded applications deliver their potent power by running many threads concurrently within a single program. From a logical point of view, multithreading means multiple lines of a single program can be executed at the same time, however, it is not the same as starting a program twice and saying that there are multiple lines of a program being executed at the same time. In this case, the operating system is treating the programs as two separate and distinct processes. Under Unix, forking a process creates a child process with a different address space for both code and data. However, fork() creates a lot of overhead for the operating system, making it a very CPU-intensive operation. By starting a thread instead, an efficient path of execution is created while still sharing the original data area from the parent. The idea of sharing the data area is very beneficial, but brings up some areas of concern that we'll discuss later.
Java's creators have graciously designed two ways of creating threads: implementing an interface and extending a class. Extending a class is the way Java inherits methods and variables from a parent class. In this case, one can only extend or inherit from a single parent class. This limitation within Java can be overcome by implementing interfaces, which is the most common way to create threads. (Note that the act of inheriting merely allows the class to be run as a thread. It is up to the class to start() execution, etc.)
Interfaces provide a way for programmers to lay the groundwork of a class. They are used to design the requirements for a set of classes to implement. The interface sets everything up, and the class or classes that implement the interface do all the work. The different set of classes that implement the interface have to follow the same rules.
There are a few differences between a class and an interface. First, an interface can only contain abstract methods and/or
static final variables (constants). Classes, on the other hand, can implement methods and contain variables that are not constants.
Second, an interface cannot implement any methods. A class that implements an interface must implement all methods defined
in that interface. An interface has the ability to extend from other interfaces, and (unlike classes) can extend from multiple
interfaces. Furthermore, an interface cannot be instantiated with the new operator; for example, Runnable a=new Runnable(); is not allowed.
The first method of creating a thread is to simply extend from the Thread class. Do this only if the class you need executed as a thread does not ever need to be extended from another class. The Thread class is defined in the package java.lang, which needs to be imported so that our classes are aware of its definition.
import java.lang.*;
public class Counter extends Thread
{
public void run()
{
....
}
}
The above example creates a new class Counter that extends the Thread class and overrides the Thread.run() method for its own implementation. The run() method is where all the work of the Counter class thread is done. The same class can be created by implementing Runnable:
import java.lang.*;
public class Counter implements Runnable
{
Thread T;
public void run()
{
....
}
}
Here, the abstract run() method is defined in the Runnable interface and is being implemented. Note that we have an instance of the Thread class as a variable of the Counter class. The only difference between the two methods is that by implementing Runnable, there is greater flexibility in the creation of the class Counter. In the above example, the opportunity still exists to extend the Counter class, if needed. The majority of classes created that need to be run as a thread will implement Runnable since they probably are extending some other functionality from another class.
Do not think that the Runnable interface is doing any real work when the thread is being executed. It is merely a class created to give an idea on the design of the Thread class. In fact, it is very small containing only one abstract method. Here is the definition of the Runnable interface directly from the Java source:
package java.lang;
public interface Runnable {
public abstract void run();
}
That is all there is to the Runnable interface. An interface only provides a design upon which classes should be implemented. In the case of the Runnable interface, it forces the definition of only the run() method. Therefore, most of the work is done in the Thread class. A closer look at a section in the definition of the Thread class will give an idea of what is really going on:
public class Thread implements Runnable {
...
public void run() {
if (target != null) {
target.run();
}
}
...
}
From the above code snippet it is evident that the Thread class also implements the Runnable interface. Thread.run() checks to make sure that the target class (the class that is going to be run as a thread) is not equal to null, and then executes the run() method of the target. When this happens, the run() method of the target will be running as its own thread.
Since the different ways to create an instance of a thread are now apparent, we will discuss the implementation of threads beginning with the ways available to start and stop them using a small applet containing a thread to illustrate the mechanics:
CounterThread Example and Source code
The above applet will start counting from 0 displaying its output to both the screen and the console. A quick glance might give the impression that the program will start counting and display every number, but this is not the case. A closer examination of the execution of this applet will reveal its true identity.
threadsBy Anonymous on January 24, 2010, 10:35 pmjust 4 fun...
Reply | Read entire comment
deprecatedBy Anonymous on December 28, 2009, 1:49 amboth .resume() and .suspend() are deprecated due to the fact that they are inherently unsafe.
Reply | Read entire comment
Java threads referenceBy Anonymous on November 14, 2009, 5:35 amhttp://www.roseindia.net/java/beginners/Threads.shtml else contact me mohammadbilal.shaikh@gmail.com
Reply | Read entire comment
using isAlive() method of thread you can find out the no of alivBy Anonymous on October 6, 2009, 12:23 pmusing isAlive() method of thread you can find out the no of alive threads in program...
Reply | Read entire comment
counting alive threadsBy Anonymous on October 1, 2009, 12:34 pmCan you suggest a way for finding out the number of alive threads in a program..
Reply | Read entire comment
View all comments