Академический Документы
Профессиональный Документы
Культура Документы
RUNNABLE
NEW THREAD
BLOCKED/WAITING
RUNNING
START THREAD
DEAD
NEW
When we create a new Thread object using new operator, thread state is New thread.
RUNNABLE
When we call start() function on Thread object, it’s state is changed to runnable . The control is given to
Thread scheduler to finish its execution.
RUNNING
When thread is executing , it’s state is changed to Running . Thread scheduler pick one of the thread from
the runnable thread pool and change it’s state to running . The CPU starts executing this thread.
BLOCKED/WAITING
A thread can be waiting for other thread to finish using thread join or it can be waiting for some resources to
available.
DEAD
Once the thread finished executing, it’s state is changed to dead and it’s considered to be not alive.
HOW MANY WAYS TO CREATING THREAD
1. By extends thread class
Create a thread by a new class that extends thread class and instance of class . Extending
class must overrides run() method which is the entry point of thread.
For example –
public class mythread extends thread
{
public void run()
{
System.out.print(“thread is start to running………”);
}
public static void main(String[]args)
{
mythread mt = new mythread;
Mt.start();
}}
2.By implementing runnable interfaces
This is the easiest way to create thread by implementing runnable interface . After
runnable interface, class needs to implements the run() method which is, public void run().
For example –
public class mythread extends thread
{
public void run()
{
System.out.print(“thread is start to running………”);
}
public static void main(String[]args)
{
mythread mt = new mythread;
thread t = new thread;
t.start();
}}
MAIN THREAD
When we start any java program, one thread begins running immediately, which is called
Main Thread of that program.
The main thread is created automatically when your program is started.
class Shweta
{
public static void main(String[]args)
{
System.out.print(“main thread”);
}
}
HOW TO START THREAD
class Shweta
{
public static void main(String[]args)
{
mythread t = new mythread();
t.start();
for(int i=0;i<5;i++)
{
System.out.print(“main thred”);
}
}
}