Multithreading and Exception Handling
Multithreading and Exception Handling
Multithreading and Exception Handling
Handling
Introduction to Multithreading
Java is a multi-threaded programming
language which means we can develop multi-
threaded program using Java.
A multi-threaded program contains two or
more parts that can run concurrently and each
part can handle a different task at the same
time making optimal use of the available
resources
By definition, multitasking is when multiple
processes share common processing resources
such as a CPU. Multi-threading extends the
idea of multitasking into applications where
you can subdivide specific operations within a
single application into individual threads. Each
of the threads can run in parallel.
Life Cycle of a Thread
A thread goes through various stages in its life
cycle. For example, a thread is born, started,
runs, and then dies. The following diagram
shows the complete life cycle of a thread.
New A new thread begins its life cycle in the new
state. It remains in this state until the program starts
the thread. It is also referred to as a born thread.
Runnable After a newly born thread is started, the
thread becomes runnable. A thread in this state is
considered to be executing its task.
Waiting Sometimes, a thread transitions to the
waiting state while the thread waits for another thread
to perform a task. A thread transitions back to the
runnable state only when another thread signals the
waiting thread to continue executing.
Timed Waiting A runnable thread can enter
the timed waiting state for a specified interval
of time. A thread in this state transitions back
to the runnable state when that time interval
expires or when the event it is waiting for
occurs.
Terminated (Dead) A runnable thread enters
the terminated state when it completes its
task or otherwise terminates.
Thread Priorities
try{
int a[]=new int[5];
a[5]=4;
}catch(ArrayIndexOutOfBoundsException e){System.out.println(e);}
System.out.println("other statement);
}catch(Exception e){System.out.println("handeled");}
System.out.println("normal flow..");
}
}
CASE 1 Usage of Java Finally
Let's see the java finally example where exception doesn't occur.
class TestFinallyBlock{
public static void main(String args[]){
try{
int data=25/5;
System.out.println(data);
}
catch(NullPointerException e){System.out.println(e);}
finally{System.out.println("finally block is always executed");}
System.out.println("rest of the code...");
}
}
Test it Now Output:5 finally block is always executed rest of the code...
where exception occurs and not
handled.
class TestFinallyBlock1{
public static void main(String args[]){
try{
int data=25/0;
System.out.println(data);
}
catch(NullPointerException e){System.out.println(e);}
finally{System.out.println("finally block is always executed");}
System.out.println("rest of the code...");
}
}
Test it Now Output:finally block is always executed Exception in
thread main java.lang.ArithmeticException:/ by zero
Case 3
where exception occurs and handled
public class TestFinallyBlock2{
public static void main(String args[]){
try{
int data=25/0;
System.out.println(data);
}
catch(ArithmeticException e){System.out.println(e);}
finally{System.out.println("finally block is always executed");}
System.out.println("rest of the code...");
}
}
Test it Now Output:Exception in thread main
java.lang.ArithmeticException:/ by zero finally block is always executed
rest of the code...
Java throw keyword