Thread

A thread–sometimes known as an execution context or a lightweight process–is a single sequential flow of control within a process.

Definition:
A thread is a single sequential flow of control within a program

Example:
The following program is a simple Java application that creates and starts two independent threads:

class TwoThreadsTest
{
public static void main (String[] args)

{
new SimpleThread(“Jamaica”).start();
new SimpleThread(“Fiji”).start();
}
}

class SimpleThread extends Thread
{
public SimpleThread(String str)

{
super(str);
}
public void run() {
for (int i = 0; i < 10; i++)
{
System.out.println(i + ” ” + getName());
try

{
sleep((int)(Math.random() * 1000));
}
catch (InterruptedException e)
{

 

}
}
System.out.println(“DONE! ” + getName());
}
}

A thread is similar to the sequential programs described above: a single thread also has a beginning, an end, a sequence, and at any given time during the runtime of the thread there is a single point of execution.

The HotJava Web browser is an example of a multithreaded application. Within the HotJava browser we can scroll a page while it’s downloading an applet or image, play animation and sound concurrently, print a page in the background while we download a new page, or watch three sorting algorithms race to the finish.