Вы находитесь на странице: 1из 40

Multithreading and Exception

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

Every Java thread has a priority that helps the


operating system determine the order in which threads
are scheduled.
Java thread priorities are in the range between
MIN_PRIORITY (a constant of 1) and MAX_PRIORITY (a
constant of 10). By default, every thread is given
priority NORM_PRIORITY (a constant of 5).
Threads with higher priority are more important to a
program and should be allocated processor time before
lower-priority threads. However, thread priorities
cannot guarantee the order in which threads execute
and are very much platform dependent.
Create a Thread by Implementing a
Runnable Interface
Step 1
As a first step, you need to implement a run()
method provided by a Runnable interface.
This method provides an entry point for the
thread and you will put your complete
business logic inside this method. Following is
a simple syntax of the run() method
public void run( )
Step 2
As a second step, you will instantiate a Thread
object using the following constructor
Thread(Runnable threadObj, String
threadName);
Where, threadObj is an instance of a class that
implements the Runnable interface and
threadName is the name given to the new
thread.
Step 3
Once a Thread object is created, you can start
it by calling start() method, which executes a
call to run( ) method. Following is a simple
syntax of start() method
void start();
Example Program
Write a Java Program that prints the numbers
from 1 to 10 line by line after every 10
seconds
Class Thread
{
Int num;
Thread()
{
Start();
}
Public void run()
{
Try
{
For(int i=1;i<=10;i++)
{
System.out.println(i);
Thread.sleep(10000);
}
}
Catch(InterruptedException e)
{
System.out.println(Exception in Thread
Handling);
}}}
Public class Multi
{
Public static void main(String args[])
{
Thread t1=new Thread();
}
}
1,2,3,4,5,6,7,8,9,10
Creation of Multiple Threads by
Extending the thread class
Class thread_demo extends Thread
{
String msg;
Thread_demo(String str)
{
Msg=str;
Start();
}
Public void run()
{
For(int i=1;i<=5;i++)
{
System.out.println(msg);}
Try{
Thread.sleep(10000);
}
Catch(InterruptedException e)
{
System.out.println(Exception in Thread
Handling);}}}
Public class ManyThread
{
Public static void main(String args[])
{
Thread_demo t1=new Thread_demo(First
Thread);
Thread_demo t2=new
Thread_demo(Second);
Thread_demo t3=new Thread_demo(Third);
}
}
First Thread
First Thread
First Thread
First Thread
First Thread
Second Thread Second Thread
Second Thread
Second Thread
Third ThreadThird Thread
Third Thread
Third Thread
Third Thread
Runnable
Class thread_demo implements Runnable
{
String str, msg; Thread t;
Thread_demo(String str)
{
Msg=str;
t=new Thread(this.str);
t.Start();
}
Public void run()
{
For(int i=1;i<=5;i++)
{
System.out.println(msg);
}
Try{ Thread.sleep(10000);}
Catch(interruptedException e)
{
System.out.println(Exception in Thread handling);
}}}
Public class ManyThreadRunnable{
Public static void main(string args[])
{
Thread_demo t1=new Thread_demo(First Thread);
Thread_demo t2=new Thread_demo(Second Thread);

Thread_demo t3=new Thread_demo(Third Thread);}}


First thread
First thread
First thread
Second ThreadSecond Thread
Second Thread
Second Thread
Second Thread
First thread
First thread
Third Thread Third Thread
Third Thread
Third Thread
Third Thread
Exception Handling
Scenario where NullPointerException occurs
If we have null value in any variable,
performing any operation by the variable
occurs an NullPointerException.
String s=null;
System.out.println(s.length());//NullPointerExc
eption
1) Scenario where ArithmeticException
occurs
If we divide any number by zero, there occurs
an ArithmeticException.
int a=50/0;//ArithmeticException
Scenario where NumberFormatException occurs
The wrong formatting of any value, may occur
NumberFormatException. Suppose I have a string
variable that have characters, converting this
variable into digit will occur
NumberFormatException.
String s="abc";
int i=Integer.parseInt(s);//NumberFormatExceptio
n
4) Scenario where
ArrayIndexOutOfBoundsException occurs
If you are inserting any value in the wrong
index, it would result
ArrayIndexOutOfBoundsException as shown
below:
int a[]=new int[5];
a[10]=50; //ArrayIndexOutOfBoundsException
Java Exception Handling Keywords
There are 5 keywords used in java exception
handling.
try
catch
finally
throw
throws
Problem without exception handling
Let's try to understand the problem if we don't
use try-catch block.
public class Testtrycatch1{
public static void main(String args[]){
int data=50/0;//may throw exception
System.out.println("rest of the code...");
}
} Exception in thread main
java.lang.ArithmeticException:/ by zero
Class ExceptionDemo
{
Public static void main(String args[])
{ try {
Int a,b;
A=5;
B=a/0;
}
Catch(ArithmeticException e)
{
System.out.println(Divide by Zero);
}
System.out.println(Executed catch
statement);}}
JAVA Multi Catch block
If you have to perform different tasks at the occurrence of different
Exceptions, use java multi catch block.
public class TestMultipleCatchBlock{
public static void main(String args[]){
try{
int a[]=new int[5];
a[5]=30/0;
}
catch(ArithmeticException e){System.out.println("task1 is completed");}
catch(ArrayIndexOutOfBoundsException e){System.out.println("task 2 co
mpleted");}
catch(Exception e){System.out.println("common task completed");}

System.out.println("rest of the code...");


}
}
Test it Now Output:task1 completed rest of the code...
Nested Try block
class Excep6{
public static void main(String args[]){
try{
try{
System.out.println("going to divide");
int b =39/0;
}catch(ArithmeticException e){System.out.println(e);}

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

The Java throw keyword is used to explicitly


throw an exception.
We can throw either checked or uncheked
exception in java by throw keyword. The throw
keyword is mainly used to throw custom
exception. We will see custom exceptions later.
The syntax of java throw keyword is given below.
throw exception;
Let's see the example of throw IOException.
throw new IOException("sorry device error);
java throw keyword example
In this example, we have created the validate method that takes integer value as a parameter. If the
age is less than 18, we are throwing the ArithmeticException otherwise print a message welcome
to vote.
public class TestThrow1{
static void validate(int age){
if(age<18)
throw new ArithmeticException("not valid");
else
System.out.println("welcome to vote");
}
public static void main(String args[]){
validate(13);
System.out.println("rest of the code...");
}
}
Output:
Exception in thread main java.lang.ArithmeticException:not valid

Вам также может понравиться