Multi-Threading :
Multi-tasking is the process of executing the multiple
applications at the
same time . e.g. Windows 98 , etc ... supports multi-tasking .
For example , we are running the Wordpad and winamp at the
same time.
Multi-threading refers to executing the multiple sections of the
same application
at the same time.
The section of the same application is known as the Thread .
The Life Cycle of the Thread is ,
Life Cycle of the Thread includes the following stages ,
i. Start
ii. Ready
iii Running
iv Waiting
v Dead
i. Start : When the thread is created , it automatically
entered in this stage.
ii. Ready : When the thread gathered all the resources
required for its
execution , then it enters in the Ready state , that
is, it is now
waiting for the cpu to execute it .
iii Running : When the thread is executed by the cpu , then it
enters in the running state.
iv Waiting : When the thread is dealing or interacting with the
input or output
[Link] thread enters in the waiting state .
v Dead : When the thread finishes its e
xecution , it enters in the Dea
d state.
To create the Thread , there are two different ways ,
i. Using the Thread class
ii. Using the Runnable Interface
i. Using the Thread class .
----------------------------
To create the Thread we have to first create the class on the
basis of the
Thread class , which is defined in the package [Link]
The general form is,
class ThreadName extends Thread
{
void run()
{
//body of the function
}
}
Now , we will create the object of the ThreadName class .
ThreadName objectname=new ThreadName();
Now , to start the thread executing , we will use ,
[Link]();
The main purpose of thread is used to execute a particular
section of code
after a certain duration of time .
[Link](nmilliseconds)
e.g.
[Link](1000);