CH 5. Exception Handling & Multithreading
CH 5. Exception Handling & Multithreading
Multithreading
Chp 5
By Megha V Gupta
An exception is an unwanted or unexpected
event, which occurs during the execution of a
Exception program.
• When the java interpreter caught an error, such as divide by zero, the interpreter creates an exception object
and throws it to inform that an error has occurred.
• Checked Exception : Exceptions which are checked by the compiler at run time is called as Checked Exceptions.
• Unchecked Exception : Exceptions which are not checked by the compiler at run time is called as
Unchecked Exceptions.
Exception Handling in Java
Types of error
• Compile time error
• Runtime error
}
finally block is always executed
rest of the code...
Throw The Java throw keyword is
keyword used to explicitly throw an
exception.
try The "try" keyword is used to specify a block where we should place
exception code. The try block must be followed by either catch or finally.
It means, we can't use try block alone.
catch The "catch" block is used to handle the exception. It must be preceded by
try block which means we can't use catch block alone. It can be followed
by finally block later.
finally The "finally" block is used to execute the important code of the program.
It is executed whether an exception is handled or not.
Thread Priority
Thread Synchronization
DeadLock
Introduction to Thread
• Process and Thread are two basic units of Java
program execution.
• Process: A process is a self contained execution
environment and it can be seen as a program or
application.
• Thread: It can be called lightweight process
• Thread requires less resources to create and exists in the
process
• Thread shares the process resources
Introduction to Thread
Multithreading
A program is divided
Multithreading in java is
into two or more
a process of executing
subprograms, which can
multiple processes
be implemented at the
simultaneously
same time in parallel.
Multiprocessing and
Java Multithreading is
multithreading, both are
mostly used in games,
used to achieve
animation etc.
multitasking.
Multithreading
Multithreading
ADVANTAGE:
▪ It doesn't block the user
▪ can perform many operations together so it
saves time.
▪ Threads are independent so it doesn't
affect other threads
During the lifetime of a
Life Cycle of thread, there are many
states it can enter.
a Thread
They include:
• Newborn state
• Runnable state
• Running state
• Blocked state
• Dead state
Life Cycle of a Thread
Life Cycle of a Thread
Newborn State:
▪ The thread is born and is said to be in newborn
state.
▪ The thread is not yet scheduled for running.
▪ At this state, we can do only one of the following:
• Schedule it for running using start() method.
• Kill it using stop() method.
Life Cycle of a Thread
Runnable State:
▪ The thread is ready for execution
▪ Waiting for the availability of the processor.
▪ The thread has joined the queue
Life Cycle of Running State:
a Thread • Thread is executing
• The processor has given
its time to the thread
for its execution.
• The thread runs until it
gives up control on its
own or taken over by
other threads.
Blocked State:
Life Cycle of • A thread is said to be blocked
a Thread • It is prevented to entering into the
runnable and the running state.
• This happens when the thread is
suspended, sleeping, or waiting in
order to satisfy certain requirements.
• A blocked thread is considered "not
runnable" but not dead and
therefore fully qualified to run again.
• This state is achieved when we Invoke
suspend() or sleep() or wait()
methods.
Dead State:
Life Cycle of • Every thread has a life cycle.
a Thread • A running thread ends its life
when it has completed executing
its run( ) method. It is a natural
death.
• A thread can be killed in born, or
in running, or even in "not
runnable" (blocked) condition.
• It is called premature death.
• This state is achieved when we
invoke stop() method or the
thread completes it execution.
Creating Thread
By implementing
Runnable
Interface
Main Thread created
Creating Thread thread is running...
1. By Extending Thread class
void run() This method is the entry point of the thread. Execution of
thread starts from this method.
This method suspend the thread for mentioned time
void sleep(int sleeptime) duration in argument (sleeptime in ms)
ThreadName.setPriority(int Number);
Thread Priority
• The intNumber is an integer value to which the
thread's priority is set. The Thread class defines
several priority constants:
1. public static int MIN_PRIORITY =1
2. public static int NORM_PRIORITY = 5
3. public static int MAX_PRIORITY = 10
• The default setting is NORM_PRIORITY. Most user-
level processes should use NORM_PRIORITY.
Generally threads use their own data and
Java methods provided inside their run() methods.
Synchronization
But if we wish to use data and methods outside
the thread’s run() method, they may compete
for the same resources and may lead to serious
problems.
synchronized (lock-object)
{
.......... // code here is synchronized
}
Deadlock
• Deadlock describes a situation where two or
more threads are blocked forever, waiting for
each other.
• when two or more threads are waiting to gain
control on a resource.
For example, assume that the thread A must
access Method1 before it can release Method2, but
the thread B cannot release Method1 until it gets
holds of Method2.
Deadlock