Q: What is thread?
A: a flow of execution is thread.
Ways to define thread:
1) by extending Thread class
2) by implementing Runnable interface (In Next Class)
1) By Extending Thread Class
the above process of defining run() in class is called defining the Thread. so when ever we start this thread, the thread will run the for loop in thread.
Let's deal the above example with test cases:
case 1: Thread Scheduler
a) It is the part of JVM
b) It is responsible to schedule Threads i.e if multiple threads are waiting to get the chance of execution then in which order threads will be excuted is decided by Thread Scheduler.
c) We cannot expect exact algorithm followed by Thread Scheduler. It is varied from JVM to JVM. Hence, we cannot expect thread execution order and exact output.
d) Hence, whenever situation comes to multithreading there is no gurantee for exact output. But we can provide several possible outputs.
case 2: difference between t.start and t.run
a) 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(), a new thread won't be created and run method will be executed jsut like a normal mehtod call by main thread.
b) Hence, in the above program, if we replace t.start() with t.run(), then the output is
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
main thread
main thread
main thread
main thread
main thread
This total output produced by only main thread.
case 3: importance of Thread class start() method
a) Thread class start() method is responsible to register the thread with thread scheduler and all other mandatory activities. Hence, without executing thread class start() method, there is no chance of starting a new thread in java. Due to this thread class start() method is considered as heart of multithreading.
start(){
1) register this thread with thread scheduler
2) perform all other mandatory activities
3) invoke run method
}
case 4: overloading of run() method
Overloading of run() method is always possible but Thread Class start() method can invoke no argument run() method. The other overloaded method, we have to call explicitly like a normal method call.
output: no arg method
case 5:
if we are not overriding run() method then Thread class run() method will be executed which has empty implementaion. Hence, we won't get any output.
Note:
It is highly recommended to override run() method otherwise do not go for multithreading 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 new thread wouldn't be created.
output: start method
main method
output is produced by main method
case 7:
Note: it is not recommended to override start() method otherwise do not go for multithreading concept.
start method
main method
order of output can vary. dependent on Thread Scheduler
case 8: Thread Lifecycle
Thread t = new Thread();
t.start();
t.start();
RE: IllegalThreadStateException
After starting the Thread if we are trying to restart the same thread then we will get runtime exception saying
RE: IllegalThreadStateException
A: a flow of execution is thread.
Ways to define thread:
1) by extending Thread class
2) by implementing Runnable interface (In Next Class)
1) By Extending Thread Class
class MyThread extends Thread{ @override public void run(){ for(int i=0; i<10;i++) System.out.println("child Thread"); } } class ThreadDemo{ public static void main(String[]args){ MyThread t = new MyThread(); // thread instantiation from main thread t.start(); //starting of a thread for(int i=0; i<10;i++){ System.out.println("main thread"); } } }
the above process of defining run() in class is called defining the Thread. so when ever we start this thread, the thread will run the for loop in thread.
Let's deal the above example with test cases:
case 1: Thread Scheduler
a) It is the part of JVM
b) It is responsible to schedule Threads i.e if multiple threads are waiting to get the chance of execution then in which order threads will be excuted is decided by Thread Scheduler.
c) We cannot expect exact algorithm followed by Thread Scheduler. It is varied from JVM to JVM. Hence, we cannot expect thread execution order and exact output.
d) Hence, whenever situation comes to multithreading there is no gurantee for exact output. But we can provide several possible outputs.
case 2: difference between t.start and t.run
a) 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(), a new thread won't be created and run method will be executed jsut like a normal mehtod call by main thread.
b) Hence, in the above program, if we replace t.start() with t.run(), then the output is
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
main thread
main thread
main thread
main thread
main thread
This total output produced by only main thread.
case 3: importance of Thread class start() method
a) Thread class start() method is responsible to register the thread with thread scheduler and all other mandatory activities. Hence, without executing thread class start() method, there is no chance of starting a new thread in java. Due to this thread class start() method is considered as heart of multithreading.
start(){
1) register this thread with thread scheduler
2) perform all other mandatory activities
3) invoke run method
}
case 4: overloading of run() method
Overloading of run() method is always possible but Thread Class start() method can invoke no argument run() method. The other overloaded method, we have to call explicitly like a normal method call.
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 then Thread class run() method will be executed which has empty implementaion. Hence, we won't get any output.
Note:
It is highly recommended to override run() method otherwise do not go for multithreading 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 new thread wouldn't be created.
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"); } }
main method
output is produced by main method
case 7:
Note: it is not recommended to override start() method otherwise do not go for multithreading concept.
class MyThread extends Thread{ public void start(){ super.start(); System.out.println("start method"); // this line will be executed by the main thread. this line is same as writing in main thread } public void run(){ //this will execute in child thread 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: run method
start method
main method
order of output can vary. dependent on Thread Scheduler
case 8: Thread Lifecycle
Thread t = new Thread();
t.start();
t.start();
RE: IllegalThreadStateException
After starting the Thread if we are trying to restart the same thread then we will get runtime exception saying
RE: IllegalThreadStateException
0 comments:
Post a Comment