We can define a Thread in the following 2 ways.
1. By extending Thread class.
2. By implementing Runnable interface.
Defining a Thread by extending "Thread class":
Case 1: Thread Scheduler:
1. By extending Thread class.
2. By implementing Runnable interface.
Defining a Thread by extending "Thread class":
class
ThreadDemo
{
public static void main(String[] args)
{
MyThread t=new
MyThread();//Instantiation of a Thread
t.start();//starting
of a Thread
for(int
i=0;i<5;i++)
{
System.out.println("main
thread");
}
}
}
|
Case 1: Thread Scheduler:
- If multiple Threads are waiting to execute then which Thread will execute 1st is decided by "Thread Scheduler" which is part of JVM.
- Which algorithm or behavior followed by Thread Scheduler we can't expect exactly it is the JVM vendor dependent hence in multi threading examples we can't expect exact execution order and exact output.
- The following are various possible outputs for the above program.
Case 2: Difference between t.start() and t.run() methods:
- In the case of t.start() a new Thread will be created which is responsible for the execution of run() method.
- But in the case of t.run() no new Thread will be created and run() method will be executed just like a normal method by the main Thread.
- In the above program if we are replacing t.start() with t.run() the following is the output.
Output:
child thread
child thread
child thread
child thread
child thread
child thread
child thread
child thread
child thread
child thread
main thread
main thread
main thread
main thread
main thread
child thread
child thread
child thread
child thread
child thread
child thread
child thread
child thread
child thread
child thread
main thread
main thread
main thread
main thread
main thread
- Entire output produced by only main Thread.
- For every Thread the required mandatory activities like registering the Thread with Thread Scheduler will takes care by Thread class start() method and programmer is responsible just to define the job of the Thread inside run() method.
- That is start() method acts as best assistant to the programmer.
start()
{
- Register Thread with Thread Scheduler.
- All other mandatory low level activities.
- Invoke or calling run() method.
We can conclude that without executing Thread class start() method there is no chance of starting a new Thread in java. Due to this start() is considered as heart of multithreading.
Case 4: Overloading of run() method:
- We can overload run() method but Thread class start() method always invokes no argument run() method.
- The other overload run() methods we have to call explicitly then only it will be executed just like normal method.
class MyThread extends Thread
{
public
void run()
{
System.out.println("no
arg method");
}
public
void run(int i)
{
System.out.println("int
arg method");
}
}
class ThreadDemo
{
public
static void main(String[] args)
{
MyThread
t=new MyThread();
t.start();
}
}
|
Output:
No arg method
Case 5: If we are not overriding run() method:
- If we are not overriding run() method then Thread class run() method will be executed which has empty implementation and hence we won't get any output.
class MyThread extends Thread
{}
class ThreadDemo
{
public static void main(String[] args)
{
MyThread
t=new MyThread(); t.start();
}
}
|
Output:
NO OUTPUT
NOTE:- It is highly recommended to override run() method. Otherwise don't go for multi threading concept.
Case 6: overriding of start() method:
- If we override start() method then our start() method will be executed just like a normal method call and no new Thread will be started.
class MyThread extends Thread
{
public
void start()
{
System.out.println("start
method");
}
public
void run()
{
System.out.println("run
method");
}
}
class ThreadDemo
{
public
static void main(String[] args)
{
MyThread
t=new MyThread(); t.start(); System.out.println("main method");
}
}
|
Output:start method
main method
- Entire output produced by only main Thread
- Note: It is never recommended to override start() method otherwise don’t go for multi threading concept.
Example-1:
Example-2:
Output:
Case 8: life cycle of the Thread:
- Once we created a Thread object then the Thread is said to be in new state or born state.
- Once we call start() method then the Thread will be entered into Ready or Runnable state.
- If Thread Scheduler allocates CPU then the Thread will be entered into running state.
- Once run() method completes then the Thread will entered into dead state.
- After starting a Thread we are not allowed to restart the same Thread once again otherwise we will get run time exception saying "IllegalThreadStateException".
MyThread t=new MyThread();
t.start();//valid
//Some statements
t.start();//we will get run time exception saying:
IllegalThreadStateException
|
Thanks..!!
No comments:
Post a Comment