Unit V ExceptionHandling&Multithreading
Unit V ExceptionHandling&Multithreading
Such an abnormal condition may at times corrupt some private or confidential files or
may make the data inconsistent.
So when an exception occurs, we have to see that the controls comes out of the program
smoothly without corrupting any data, or leaving the objects in inconsistent state.
And java provides the exception handling techiniques to deal with the exceptions.
Throwable(class)
Error Exception
RuntimeException IOExceptions
ArithmeticException ClassNotFoundException
ArrayIndexOutOfBoundsException NoSuchFieldException
NumberFormatExcption NoSuchMethodException
NullPointerException InterruptedException
StringIndexOutOfBoundsException IllegalAccessException
ClassCastException CloneNotSupportedException
IllegalStateException IllegalAccessException
SecurityException
NegativeArraySizeException
The Throwable class is the super class of all the exception classes and exists in java.lang
package. As java.lang package is the default package available to all the classes, all these
exception classes can be directly used in the programs.
Exception Handling: The process of dealing with the abnormal conditions of trying
to handle the caused exception is called exception handling.
In java exceptions are objects. There is a predefined class called Exception in java.lang
package.
Ex:
Line 1 class ExceptionDemo{
2 public static void main(String[] args){
3 System.out.println(“Dividing 5 by 0”);
4 int a = 5/0;
5 System.out.println(“A = “+ a);
6 }
7 }
The division of any value by zero is not defined in java, so this line will throw the Divide
by zero exception.
At line no. 4, an ArithmeticException class object is created and thrown out of the main
method, and the execution stops. So the line no. 5 doesn’t print the given string to the
console.
Ex:
class ExceptionDemo{
public static void main(String[] args){
int a= 10;
System.out.println(“Start”);
try{
while(true){
int b = 100/a;
System.out.println(“A = “+ a+ “ B= “+b);
a-=2;
}
}catch(ArithmeticException ae){
System.out.println(“Exception handled :+ ”ae.getMessage());
}
finally{
System.out.println(“End”);
}
}
}
int a = 5/ 0;
int b=’c’;
}catch(ArithmeticException ae){
System.out.println(“Arithmetic Exception: ”+ae.getMessage());
}catch(NullPointerException np){
System.out.println(“ NullPointerExceptionException:”
+ae.getMessage());
}catch(NumberFormatException nf){
System.out.println(“Number format Exception:”+
ae.getMessage());
}
finally{
System.out.println(“End”);
}
}
}
The code in the try block can raise Arithmetic Exception, Null pointer exception of the
number format exception.
In this case only one of the catch blocks will get executed. This depends on the exception
that is thrown first of the try block. So as per the code, NullPointerException object is
thrown from the try block.
If there is no matching catch block then the exception is handled by the java runtime
environment which is the JVM. Instead of giving it to runtime environment, we can
place a catch block of type catch(Exception e){ …} . Will handle any kind of exception
as Exception is the super class of all other types of exceptions.
The throws keyword: Instead of using the try catch blocks, we can use the throws
keyword with the method signature to postpone the exception handling by passing it to
the calling method.
A throws clause lists he type of exceptions that a method might throw.
Ex: class ThrowsDemo{
int a = 5/ 0;
int b=’c’;
}
The throw keyword:Exceptions are thrown using the throw keyword. When ever an
exception is raised, the respective exception object is created and thrown using thow.
Ex:
try{
ArithmeticException ae = new ArithmeticException() ;’
throw ae;
}catch(Exception e){..}
A checked exception is any subclass of Exception (or Exception itself), excluding class
RuntimeException and its subclasses
The compiler forces the programmers to specify the list of exceptions that might be
thrown by the corresponding methods.
Unchecked exceptions are RuntimeException and any of its subclasses. Class Error and
its subclasses also are unchecked. The unchecked exception are not checked for
declaration by compiler.
Creating own Exception subclasses : We can create our own custom exceptions.
Throwable is the super class of all the exception subclases.
To create an Exception of our own type
class MyException extends Exception{
…..
}
Since MyException class extends an Exception , it is also an exception.
Ex:
class AgeException extends Exception{
int age;
Multithreading :
Definition of a thread: A thread is a single sequential flow of control within a program.
Multithreading: The process of executing multiple such threads with separate flow of
controls simultaneously is called multithreading.
Run
Dea
d
Creating a thread: Threads are also objects in java. There is a Thread class defined
in java.lang package. To create threads in java
1) create objects of this class or
2) create a class make it a subclass of Thread class or
3) Create a class and implement Runnable interface.
String threadName;
public MyThread(String name){
threadName = name;
start();
}
class ThreadDemo{
public static void main(String args[]){
MyThread thread1 = new MyThread(“Thread1);
MyThread thread2 = new MyThread(“Thread2);
MyThread thread3 = new MyThread(“Thread3);
}
}
String threadName;
public MyThread(String name){
threadName = name;
start();
}
class RunnableDemo{
public static void main(String args[]){
MyThread myThread1 = new MyThread(“Thread1”);
Thread t1 = new Thread(myThread1);
}
}
In java we can the prioritize the threads execution. This can be done by the api’s defined
in the Thread class.
Methods:
final void setPriority(int priority) : Sets the given priority to the thread
final int getPriority() : Returns the thread priority.
Priority is an integer value between 1 and 10. 1 is the minimum priority and 10 is the
maximum priority.
Ex:
MyThread thread1 = new MyThread(“Thread1);
MyThread thread2 = new MyThread(“Thread2);
MyThread thread3 = new MyThread(“Thread3);
thread1.setPriority(1);
thread2.setPriority(9);
thread3.setPriority(4);
or
thread1.setPriority(Thread.NORM_PRIORITY);
The higher priority threads get allocation of the maximum CPU time, where the low
priority threads get the allocation of minimum CPU time.
Synchronization: When two or more threads are trying to access the same shared
resource, then there should be some kind of coordination, which takes care of the
allocating the shared resource to single thread at a time.
When a thread enters the monitor it acquires the monitor, and no other thread can enter
the monitor, until the first thread exits the monitor. The other threads will be in the
waiting state.
If we use the synchronized keyword with a method declaration, the method cannot be
access by multiple threads at the same time.
Ex:
class ThreadSynchronization{
public static void main(String[] args){
MyThread thread1 = new MyThread(“Thread1 : “);
MyThread thread2 = new MyThread(“Thread2 : “);
thread1.start();
thread2.start();
do{
if(thread1IsAlive && thread1.isAlive()){
thread1IsAlive = false;
System.out.println(“Thread1 : dead);
}
String threadName;
void randomWait(){
try{
sleep((long)(3000* Math.random());
}catch(InterruptedException e){
System.out.println(“Interrupted”);
}
}
}
class SynchronizedOutput{
public static void displayList(String name, String list[]) {
for(int i=0; i<list.length ; i++{
MyThread t = (MyThread) Thread.currentThread():
t.randomWait();
System.out.println(name+list[i]);
}
}
}
Now repeat the execution of the above program by just adding the synchronized keyword
in displayList() method declaration.
Declaring the displayList() method as synchronized will not allow the second thread to
enter this method until the first completes executing it. So the output is also
synchronized.
Synchronized blocks: Applying synchronized to a method will not work in all cases. For
example, we want to call API’s of some class, which are not synchronized methods and
now there is no possibility of adding the synchronized keyword. In such cases we can use
the synchronized blocks.
Syntax:
synchronized(object){
statements to be synchronized……….
}
Until the current thread completes the block execution, other calls cannot be made for the
members of the synchronized objects class.
For example in the Producer consumer problem the producer has to produce the items
and then the consumer has to consume it. If there is no synchronization the producer may
produce the items without the consumer consuming it or the producer may produce items
without the consumer or the consumer may consume the same item.
So using we need synchronization for producer and consumer to not to enter the monitor
while one is executing in it and we need communication between threads so that the
output
Inter thread communication can be achieved by using the methods wait(), notify() and
notifyAll() of object class.
Ex:
class ItemQueue{
int n;
boolean itemProduced = false;
n = item;
itemProduced = true;
System.out.println(“Produced “+n);
notify();
}
}
}
while(true){
q.put(n++);
}
}
}
class MainClass{
public static void main(String args[]){
ItemQueue q = new ItemQueue();
wait() - The thread which calls wait() method will go to the waiting/sleep state until
some other thread will go to run state and calls the notify() method.
notify() – The thread that calls this method will notify the other waiting thread for the
monitor.
notifyAll - The thread that calls this method will notify all the other waiting threads that
are in waiting state for the monitor.
Daemon Threads: A daemon thread is a thread that executes in the background and
provides services to the other threads.
It typically executes a continuous loop of instructions that wait for a service request,
perform the service, and wait for the next service request. Daemon threads continue to
execute until there are no more threads for which services can be provided. At this time
daemon thread die and the java interpreter terminates its execution. Any thread can be
changed to a daemon by setDaemon() method.
class One {
public synchronized void callA(){
try{
System.out.println(“In callA method of Thread1 class”);
Thread.sleep(2000);
}catch(InterruptedException e){
}
}
}
class Two {
public synchronized void callB(){
try{
System.out.println(“In callB method of Thread2 class”);
Thread.sleep(2000);
}catch(InterruptedException e){
}
}
}
Thread Groups: Thread groups are the objects that consists of a collection of threads.
Every thread is a member of a unique thread group. Thread groups are used to invoke
methods that apply to the threads in the thread group.
Ex:
ThreadGroup tg = new ThreadGroup(“GroupName);
Thread t1= new Thread(tg, “Thread1”);
Thread t2= new Thread(tg, “Thread2”);
Thread t3= new Thread(tg, “Thread3”);
Thread t4= new Thread(tg, “Thread4”);
tg.start();
tg.stop();
tg.setDaemon();…etc