MULTITHREADING
Multitasking:-Executing several task
simultaneously is known as multitasking, there are two types of multitasking.
1. Process
Based Multitasking
2. Thread
Based Multitasking
Process Based Multitasking:-Executing several task simultaneously
where each task is separate independent program (Process) is called process
based multitasking.
Ex-: While typing a
java program in the editor
Same time we
can listen audio from the same system
At the same
time we can download your file.
All these task will be executed simultaneously and independent each other hence it is process based multitasking.
All these task will be executed simultaneously and independent each other hence it is process based multitasking.
Note:-It
is best suitable for Operating System (O.S) level.
Thread Based Multitasking:-Executing several tasks
simultaneously where each task separate and independent part of same program
(Process) is called Thread Based Multitasking.
And each independent part is called a Thread.
And each independent part is called a Thread.
Q1.What is
the Thread?
Ans.
Thread is a separate flow of execution.
Defining a Thread:-
We can
define a thread in the following two ways.
1. By
Extending Thread Class.
2. By
Implementing Runnable Interface.
By Extending Thread Class:-
Case1-:Thread Scheduler
It is the
part of JVM .It is responsible to schedule Threads. If multiple thread are
waiting to get chance of execution then in which order threads will be executed
is decided by thread scheduler.
We can’t
accept exact algorithm followed by thread Scheduler it varied from JVM to JVM
.Hence we can’t accept threads execution order and exact Output.
Whenever
situations comes to multithreading there is no guarantee exact output but we
can provide several output.
class MyThread extends Thread{
public void run(){
for(int
i=0;i<10;i++){
System.out.println("Child
Thread");
}
}
}
public
class ThreadDemo{
public static void main(String[]
args){
MyThread t = new MyThread();
t.start();
for(int
i=0;i<10;i++){
System.out.println("main
Thread");
}
}
}
Case2:-Difference
Between t. start() and t.run();[Impotant]
In the
case of t.start() a new thread will be created which is responsible of run
method.
But in the
case of t.run() a new thread does not created and run method will be executed
just like a normal method call by main Thread.
In above
program if you replace t.start() with t.run() then the no Thread will be
created and total output produce by main Thread.
Note:-main method is executed by the
main Thread.
Case3:-Important of thread class
start() method
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 are no chance starting a
new thread in Java.
Due to
this thread class start() method is
consider as heart of multithreading.
Start(){
1.Register
this thread with thread Scheduler
2.Perform
all other mandatory activities.
3.Invoke
run();
}
Case4:- Overload run() method
Yes,We can
overload 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.
Ex- class
MyThread extends Thread {
Public
void run(){
System.out.println(“No
arg Run”);
}
Public
void run(int i){
System.out.println(“int
arg Run”);
}
}
class
ThreadDemo{
public
static void main(String [] arg ){
MyThread
t = new MyThread();
t.start();
}}
Case5:- Overriding run() method
If you are
not overriding run () method then thread class run() method will be executed .which has empty
implementation .
Hence we
don’t get any output.
Note:-so it is highly recommended
to override run() method otherwise don’t go for multithreading.
Case6:-Override start() method :-
If we
override the start() method then our
start() method will be executed just like a normal method call no thread will
be start().
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 [] arg ){
MyThread
t = new MyThread();
t.start();
t.run();
}
}
Output:- start method
run
method [this output provided by main
thread]
Note:-It is not recommended to
override start() method otherwise don’t go multithreading concept.
Case7:- In above example ,in override start() method we use super.start() statement then define thread will be start.
class
MyThread extends Thread {
Public void start(){
Super.start();//this line call thread class
start method
System.out.println(“start
method”);
}
}
class ThreadDemo{
public
static void main(String [] arg ){
MyThread
t = new MyThread();
t.start();
}
}
Case8:- Thread Life Cycle
Case9:- After starting a thread if we are
trying to restart the same thread then we will runtime exception.
Runtime Exception :- Illegal Thread State Exception ;
Ex:- MyThread t = new MyThread();
t.start();
:
t.start();//This line
generate Illegal Thread State Exception .
Comments