Java Unit 3

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 39

UNIT III

Exception Handling in Java


Exception Hierarchy

All exception and errors types are sub classes of class Throwable, which is base class
of hierarchy.

Error:
An Error indicates serious problem that a reasonable application should not
try to catch.Error is irrecoverable
for e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.

Exception:
An exception is an unwanted or unexpected event, which occurs during the
execution of a program i.e at run time that disrupts the normal flow of the
program’s instructions
Types of Java Exceptions:

There are mainly two types of exceptions:

⮚ checked Exceptions

⮚ unchecked Exception

1
⮚ Checked Exception:

● The classes which are directly inherit from Throwable class ,which are
checked at Compilation Process are known as checked exceptions.
● Checked exceptions are checked at compile-time.

for e.g. IOException, SQLException etc.

⮚ Unchecked Exception

● The classes which are inherit from Throwable classes ,which are
occurred at Runtime(Execution) Process are known as unchecked
exceptions.
● Unchecked exceptions are not occurred at checked at compile-time, but
they are checked at runtime are also known as Runtime Exceptions

for e.g. ArithmeticException, NullPointerException,


ArrayIndexOutOfBoundsException etc.

⮚ The java.lang package defines several classes and exceptions.


Some of these classes are unchecked while some other classes are checked.

CHECKE
EXCEPTIONS DESCRIPTION UNCHECKED
D
Arithmetic errors
ArithmeticException such as a divide - YES
by zero
Arrays index is
ArrayIndexOutOfBoundsEx
not within - YES
ception
array.length
Related Class not
ClassNotFoundException YES -
found
InputOuput field
IOException YES -
not found
Illegal argument
IllegalArgumentException when calling a - YES
method
One thread has
InterruptedException been interrupted YES -
by another thread

2
Nonexistent
NoSuchMethodException YES -
method
Invalid use of null
NullPointerException - YES
reference
Invalid string for
NumberFormatException conversion to - YES
number

Exception Handling

Exception handling is a framework that is used to handle runtime errors


only, compile time errors are not handled by exception handling in java.
We use specific keywords in java program to create an exception handler block.

Exception Handling Keywords

Java exception handling is managed via five keywords: try, catch, throw,
throws, and finally.

Exception Description
Keyword
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.
Throw We know that if any exception occurs, an exception object
is getting created and then Java runtime starts processing
to handle them. To manually throw an exception, use the
keyword throw. The throw keyword is mainly used to
throw custom exceptions.
throws The "throws" keyword is used to declare exceptions. It
doesn't throw an exception. It specifies that there may

3
occur an exception in the method. It is always used with
method signature.

Termination or Resumptive models

⮚ When an Exception is thrown , Control is transferred to the catch block


that handles the error.
⮚ The Programmer now has two Choices, that he can print the error
message and exit from the program.this technique is called as
Termination Model.
⮚ The user can also continue Excecution by calling some other function
after printing the error message. This Technique is called as Resumptive
Model.

If Exception handling is not Provided , then in a java application the program


stops automatically after printing the exception message in try-catch block and
also continues the code after exception occurred by using try-catch block.

For Example:

class test
{
public static void main(String args[])
{
int a=10,b=0,c;
try
{
c=a/b;
System.out.println(“result=”+c);
}
catch (ArithmeticException e)
{
System.out.println(“divide by Zero”);
}
System.out.println(“After try Catch Block”);

}
}

4
Uncaught exceptions:
If Exception handling is not Provided , then in a java application the program
terminates automatically after printing the default exception message.

class Exc0 {
public static void main(String args[]) {
int d = 0;
int a = 42 / d;
}
}

In this example, we haven’t supplied any exception handlers of our own, so the
exception is caught by the default handler provided by the Java run-time
System.

Any exception that is not caught by your program will ultimately be processed
by the default handler. The default handler displays a string describing the
exception, prints a stack trace from the point at which the exception occurred,
and terminates the program.

Here is the exception generated when the above example is executed:


Output:
java.lang.ArithmeticException: / by zero
at Exc0.main(Exc0.java:4)

The stack trace will always show the sequence of method invocations that led
up to the error.

Using try and catch

● Although the default exception handler provided by the Java run-time


system is useful for debugging, you will usually want to handle an
exception yourself.

● Doing so provides two benefits.

● First, it allows you to fix the error. Second, it prevents the program from
automatically terminating.

5
● To guard against and handle a run-time error, simply enclose the code
that you want to monitor inside a try block. Immediately following the
try block, include a catch clause that specifies the exception type that
you wish to catch.

● To illustrate how easily this can be done, the following program includes
a try block and a catch clause that processes the ArithmeticException
generated by the division-by-zero error:

Example:

class Exc2 {
public static void main(String args[]) {
int d, a;
try { // monitor a block of code.
d = 0;
a = 42 / d;
System.out.println("This will not be printed.");
}
catch (ArithmeticException e)
{ // catch divide-by-zero error
System.out.println("Division by zero.");
}
System.out.println("After try-catch statement.");
}
}

Output:
Division by zero.
After try-catch statement.
Notice that the call to println( ) inside the try block is never executed. Once an
exception is thrown, program control transfers out of the try block into the
catch block.

Put differently,catch is not “called,” so execution never “returns” to the try


block from a catch. Thus, the line “This will not be printed.” is not displayed.

Once the catch statement has executed, program control continues with the
next line in the program following the entire try/catch mechanism.

Multiple catch Clauses

6
In some cases, more than one exception could be raised by a single piece of
code. To handle this type of situation, you can specify two or more catch
clauses, each catching a different type of exception.

When an exception is thrown, each catch statement is inspected in order,


and the first one whose type matches that of the exception is executed. After
one catch statement executes, the others are bypassed, and execution
continues after the try/catch block.

The following example traps two different exception types:

// Demonstrate multiple catch statements.


class MultiCatch {
public static void main(String args[]) {
try {
int a = args.length;
System.out.println("a = " + a);
int b = 42 / a;
int c[] = { 1 };
c[42] = 99;
}
catch(ArithmeticException e) {
System.out.println("Divide by 0: " + e);

}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Array index oob: " + e);
}

System.out.println("After try/catch blocks.");

}
}
This program will cause a division-by-zero exception if it is started with no
commandline arguments, since a will equal zero. It will survive the division if
you provide a command-line argument, setting a to something larger than zero.
But it will cause an ArrayIndexOutOfBoundsException, since the int array c
has a length of 1, yet the program attempts to assign a value to c[42].

Here is the output generated by running it both ways:

C:\>java MultiCatch
a=0
Divide by 0: java.lang.ArithmeticException: / by zero
7
After try/catch blocks.

C:\>java MultiCatch TestArg


a=1
Array index oob: java.lang.ArrayIndexOutOfBoundsException:42

After try/catch blocks.

Nested try Statements


The try statement can be nested. That is, a try statement can be inside the
block of another try. Each time a try statement is entered, the context of that
exception is pushed on the stack.

If an inner try statement does not have a catch handler for a particular
exception, the stack is unwound and the next try statement’s catch handlers
are inspected for a match.
This continues until one of the catch statements succeeds, or until all of the
nested try statements are exhausted.

If no catch statement matches, then the Java run-time system will handle the
exception. Here is an example that uses nested try statements:

// An example of nested try statements.

class NestTry {
public static void main(String args[]) {
try {
int a = 0;
int b = 42 / a;
System.out.println("a = " + a);
try{
int c[] = { 1 };
c[42] = 99; // generate an out-of-bounds exception
}
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Array index out-of-bounds: " + e);
}
} catch(ArithmeticException e) {
System.out.println("Divide by 0: " + e);
}
}
}
As you can see, this program nests one try block within another. The program
works as follows.

8
When you execute the program, a divide-by-zero exception is generated by the
outer try block. An array boundary exception is generated from within the
inner try block.
Using Throw:

⮚ The throw keyword in Java is used to explicitly throw an exception from


a method or any block of code. We can throw either checked or
unchecked exception.
⮚ The throw keyword is mainly used to throw custom exceptions.

Syntax:
throw new ThrowableInstance(parameter);

Here, ThrowableInstance must be an object of type Throwable or a subclass of


Throwable.

Forexample:
Exception is a sub-class of Throwable and user defined exceptions typically
extend Exception class.
example:

class MyException extends Exception {


public MyException(String msg){
//super(msg);
System.out.println(msg);
}
}

public class MyTest {


static void positiveNumberCheck(int n)
{
try{
if(n<=0)
throw new MyException("number is not positive");
else{
System.out.println(“number=”+n);
}
catch(MyException e)

9
{
System.out.println(e.getMessage());
}
}
public static void main(String args[])
{
positiveNumberCheck(-10);
}
}

Using throws:
The Java throws keyword is used to declare an exception. It gives an
information to the programmer that there may occur an exception so it is
better for the programmer to provide the exception handling code so that
normal flow can be maintained.
This Exception Handling is mainly used to handle the checked exceptions. If
there occurs any unchecked exception such as NullPointerException, it is
programmers fault that he is not performing check up before the code being
used.
Syntax:
return_type method_name() throws exception_class_name
{
//method code
}
Example:
class MyException extends Exception {
public MyException(String msg){
//super(msg);
System.out.println(msg);
}
}

public class MyTest {


static void positiveNumberCheck(int n) throws MyException
{
if(n<=0){
throw new MyException("number is not positive");
else{
System.out.println(“number=”+n);
}

10
public static void main(String[] args)
{
try{
positiveNumberCheck(-10);
}
catch(MyException e)
{
System.out.println(e.getMessage());
}
}
}
Using finally
Java finally block is a block that is used to execute important code such as
closing connection, stream etc.
Java finally block is always executed whether exception is handled or not.
Java finally block follows try or catch block.
class TestFinallyBlock{
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...");
}
}
Output:
java.lang.ArithmeticException: / by zero
finally block is always executed
rest of the code...

Java’s Built-in Exceptions

11
Inside the standard package java.lang, Java defines several exception classes.
The most general of these exceptions are subclasses of the standard type
RuntimeException.

these exceptions need not be included in any method’s throws list.


In the language of Java, these are called unchecked exceptions because the
compiler does not check to see if a method handles or throws these exceptions.

those exceptions defined by java.lang that must be included in a method’s


throws list if that method can generate one of these exceptions and does
not handle it itself. These are called checked exceptions.

Java defines several other types of exceptions that relate to its various class
libraries.

Java’s Unchecked RuntimeException Subclasses Defined in java.lang

Java’s Checked Exceptions Defined in java.lang

12
creating own exception sub classes.
If you are creating your own Exception that is known as custom exception or user-
defined exception. Java custom exceptions are used to customize the exception
according to user need.
class InvalidAgeException extends Exception{
InvalidAgeException(String s){
super(s);
}
}
class TestCustomException1{

static void validate(int age) throws InvalidAgeException


{
if(age<18)
throw new InvalidAgeException("not valid for vote");
else
System.out.println("welcome to vote");
}
public static void main(String args[]){
try{
validate(13);
}
catch(InvalidAgeException m)
{
System.out.println("Exception occured: "+m);
m.printStackTrace();

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

Threads

13
What Is a Thread?

A thread is a single sequential flow of control within a program.


A single thread also has a beginning, an end, a sequence, and at any given
time during the runtime of the thread there is a single point of execution.
However, a thread itself is not a program. It cannot run on its own, but runs
within a program.
A thread is considered lightweight because it runs within the context of a full-
blown program and takes advantage of the resources allocated for that program
and the program's environment.

Multitasking programming is of two types –

1. Process-based Multitasking

A process is an instance of a computer program that is executed


sequentially. It is a collection of instructions which are executed
simultaneously at the run time. Thus several processes may be
associated with the same program.

Example :– We can listen to music and browse internet at the same


time. The processes in this example are the music player and browser.

2. Thread-based Multitasking.

A thread is a lightweight process which exist within a program and


executed to perform a special task. Several threads of execution may be
associated with a single process. Thus a process that has only one
thread is referred to as a single-threaded process, while a process with
multiple threads is referred to as a multi-threaded process.

Example: – Using a browser we can navigate through the webpage and


at the same time download a file. In this example, navigation is one
thread and downloading is another thread. Also in a word-processing
application like MS Word, we can type text in one thread and spell
checker checks for mistakes in another thread.

14
Differences between thread-based multitasking and process-
based Multitasking.

Thread Based Multi-tasking Process Based Multi-tasking


Thread is a such part of A process is essence of program that is
multithreaded program which executing which runs in parallel
defines separate path for
execution

It doesn’t support java compiler It allows you to run java compiler


and text editor run and text editor at a same time.
simultaneously.
Both have performed different
tasks.

Thread multitasking has less Process multitasking has more overhead


overhead than process than thread multitasking.
multitasking.

It allows taking gain access over Here, it is unable to gain access


idle time taken by CPU. over idle time of CPU.

It is totally under control of It is not under control of java.


java.
Thread based multitasking is Process based multitasking is
known to be lighter weight comparatively heavyweight process
process. comparing to thread based multitasking.

Inter-thread communication is Inter-process communication is


inexpensive and context expensive and limited
switching from one thread to
other.

It has faster data rate It has slower data rate multitasking.


multithreading or tasking

Threads share same address Process requires its own address space.
space.
Thread is a smaller unit Program is a bigger unit.
In thread based multitasking In process based multitasking two or more
two or more threads can be run processes and programs can be run
concurrently. concurrently.

Java thread model


15
Java uses threads to enable the entire environment to be asynchronous
(occurring at same time). This helps reduce inefficiency by preventing the waste
of CPU cycles.

One thread can pause without stopping other parts of your program. For
example, the idle time created when a thread reads data from a network or
waits for user input can be utilized elsewhere.

Multithreading allows animation loops to sleep for a second between each


frame without causing the whole system to pause. When a thread blocks in a
Java program, only the single thread that is blocked pauses. All other threads
continue to run.

Threads exist in several states

1. A thread can be running. It can be ready to run as soon as it gets CPU


time.
2. A running thread can be suspended, which temporarily suspends its
activity.
3. A suspended thread can then be resumed, allowing it to pick up where it
left off.
4. A thread can be blocked when waiting for a resource.
5. At any time, a thread can be terminated, which halts its execution
immediately. Once terminated, a thread cannot be resumed.

Life Cycle of Thread

A thread can be in one of the five states. According to sun, there is only 4
states in thread life cycle in java new, runnable, non-runnable and
terminated. There is no running state.

The life cycle of the thread in java is controlled by JVM. The java thread states
are as follows:

● New Born

● Runnable

● Running

● Blocked

● Dead

16
Following are the stages of the life cycle –

New Born: Whenever a new thread is created, it is always in the new state. For a thread
in the new state, the code has not been run yet and thus has not begun its execution.

Active: When a thread invokes the start() method, it moves from the new state to the
active state. The active state contains two states within it: one is runnable, and the other
is running.

o Runnable: A thread, that is ready to run is then moved to the runnable state. In
the runnable state, the thread may be running or may be ready to run at any
given instant of time. It is the duty of the thread scheduler to provide the thread
time to run, i.e., moving the thread the running state.
A program implementing multithreading acquires a fixed slice of time to each
individual thread. Each and every thread runs for a short span of time and when
that allocated time slice is over, the thread voluntarily gives up the CPU to the
other thread, so that the other threads can also run for their slice of time.
Whenever such a scenario occurs, all those threads that are willing to run, waiting
for their turn to run, lie in the runnable state. In the runnable state, there is a
queue where the threads lie.
o Running: When the thread gets the CPU, it moves from the runnable to the
running state. Generally, the most common change in the state of a thread is
from runnable to running and again back to runnable.

17
Blocked or Waiting: Whenever a thread is inactive for a span of time (not permanently) then,
either the thread is in the blocked state or is in the waiting state.

For example, a thread (let's say its name is A) may want to print some data from the
printer. However, at the same time, the other thread (let's say its name is B) is using the
printer to print some data. Therefore, thread A has to wait for thread B to use the
printer. Thus, thread A is in the blocked state. A thread in the blocked state is unable to
perform any execution and thus never consume any cycle of the Central Processing Unit
(CPU). Hence, we can say that thread A remains idle until the thread scheduler
reactivates thread A, which is in the waiting or blocked state.

When the main thread invokes the join() method then, it is said that the main thread is
in the waiting state. The main thread then waits for the child threads to complete their
tasks. When the child threads complete their job, a notification is sent to the main
thread, which again moves the thread from waiting to the active state.

If there are a lot of threads in the waiting or blocked state, then it is the duty of the
thread scheduler to determine which thread to choose and which one to reject, and the
chosen thread is then given the opportunity to run.

Timed Waiting: Sometimes, waiting for leads to starvation. For example, a thread (its
name is A) has entered the critical section of a code and is not willing to leave that
critical section. In such a scenario, another thread (its name is B) has to wait forever,
which leads to starvation. To avoid such scenario, a timed waiting state is given to
thread B. Thus, thread lies in the waiting state for a specific span of time, and not
forever. A real example of timed waiting is when we invoke the sleep() method on a
specific thread. The sleep() method puts the thread in the timed wait state. After the
time runs out, the thread wakes up and start its execution from when it has left earlier.

Terminated: A thread reaches the termination state because of the following reasons:

o When a thread has finished its job, then it exists or terminates normally.
o Abnormal termination: It occurs when some unusual events such as an
unhandled exception or segmentation fault.

A terminated thread means the thread is no more in the system. In other words, the
thread is dead, and there is no way one can respawn (active after kill) the dead thread.

Creating threads:

18
In Java, an object of the Thread class can represent a thread. Thread can be
implemented through any one of two ways:

● Extending the java.lang.Thread Class

● Implementing the java.lang.Runnable Interface

There are two ways to create a thread:

1. By extending Thread class


2. By implementing Runnable interface.

1.Thread class:
Thread class provide constructors and methods to create and perform
operations on a thread.Thread class extends Object class and implements
Runnable interface.
Constructors of Thread class
1. Thread()
This constructor creates a thread object having no predefined arguments.
2. Thread(String name)
This constructor creates a thread object having String argument.
3. Thread(Runnable r)
This constructor creates a thread object having argument r of type runnable
4. Thread(Runnable r,String name)
This constructor creates a thread object having two arguments of String and
Runnable type.
To create a thread is to create a new class that extends Thread, then override
the run() method and then to create an instance of that class. The run()
method is what is executed by the thread after you call start().
Here is an example of creating a Java Thread subclass:
public class MyClass extends Thread {

19
public void run(){
System.out.println("MyClass running");
}
}
To create and start the above thread you can do so like this:
MyClass t1 = new MyClass ();
T1.start();
When the run() method executes it will print out the text " MyClass running
".
So far, we have been using only two threads: the main thread and one child
thread. However, our program can affect as many threads as it needs. Let's
see how we can create multiple threads.

Methods of Thread class:


1. public void run():
This method is used to perform needed action for a thread. This method is
called by JVM after start() method is called.
2. public void start():
This method is called by thread object to start the execution of the thread.
After this method is called, JVM automatically calls the run() method.
3. public void sleep(long milliseconds):
This method ceases the execution of currently executing thread for the time
period as specified.
4. public void join():
This method causes the current thread (which calls this method) to wait until
another thread to die.
5. public void join(long miliseconds):
This method causes the current thread (which calls this method) to wait for a
thread to die for the specified miliseconds.
6. public int getPriority():
This method returns the priority (integer value) of the thread.
7. public int setPriority(int priority):
This method sets the priority of the thread. The priority is in terms of integer
value from 1 to 10. Priority increases with integer value from 1 being lowest
and 10 being highest.
8. public String getName():
This method returns the name of the calling thread.
9. public void setName(String name):
This method changes the name of the calling thread.
10. public Thread currentThread():
This method returns the reference of current thread which is executing.
11. public int getId():
This method returns the id of the calling thread.
12. public Thread.State getState():

20
This method returns the state of the calling thread.
13. public boolean isAlive():
This method returns true if the thread is alive, else returns false.
14. public void yield():
This method pauses the currently executing thread temporarily and allows
other threads to execute.
15. public void suspend():
This method is used to put the thread in suspended state.
16. public void resume():
This method is used to resume the suspended thread.
17. public void stop():
This method is used to stop the running thread.
18. public boolean isDaemon():
This method returns true if the thread is a daemon thread, else returns false.
19. public void setDaemon(boolean b):
This method marks the thread as daemon or user thread.
20. public void interrupt():
This method interrupts the running thread.
21. public boolean isInterrupted():
This method returns true if the thread has been interrupted, else return
false.
22. public static boolean interrupted():
This method returns true if the current thread has been interrupted, else
return false.

Java Thread Example by extending Thread class


class Multi extends Thread{ Example : 2
public void run(){
System.out.println("thread is class MyThread extends Thread
running...");
{
}
String s=null;
public static void main(String
MyThread(String s1)
args[]){
{
Multi t1=new Multi();
s=s1;
t1.start();
start();
}
}
}
public void run()

21
Output:thread is running... {
System.out.println(s);
}
}
public class RunThread
{
public static void main(String
args[])
{
MyThread m1=new
MyThread("Thread started....");
}
}
Output:
Thread started....

Creation of Multiple Threads

class Mythread extends Thread{


Mythread(String s){
super(s);
start();
}
public void run(){
for(int i=0;i<5;i++){
System.out.println("Thread Name :"
+Thread.currentThread().getName());
try{
Thread.sleep(1000);
}catch(Exception e){}
}
}
}
public class threadmult{

22
public static void main(String args[]){
System.out.println("Thread Name :"
+Thread.currentThread().getName());
Mythread m1=new Mythread("My Thread 1");
Mythread m2=new Mythread("My Thread 2");
}
}

Output:
Thread Name :main
Thread Name :My Thread 2
Thread Name :My Thread 1
Thread Name :My Thread 1
Thread Name :My Thread 2
Thread Name :My Thread 1
Thread Name :My Thread 2
Thread Name :My Thread 1
Thread Name :My Thread 2
Thread Name :My Thread 2
Thread Name :My Thread 1

Runnable interface:
The Runnable interface should be implemented by any class whose
instances are intended to be executed by a thread. Runnable interface have
only one method named run().

1. public void run(): is used to perform action for a thread.

The easiest way to create a thread is to create a class that implements the
Runnable interface.

To implement Runnable interface, a class need only implement a single


method called run( ), which is declared like this:

public void run( )

Inside run( ), we will define the code that constitutes the new thread.
Example:

public class MyClass implements Runnable {


public void run(){
System.out.println("MyClass running");
}
}

23
To execute the run() method by a thread, pass an instance of MyClass to a
Thread in its constructor (A constructor in Java is a block of code similar to
a method that's called when an instance of an object is created). Here is how
that is done:

Thread t1 = new Thread(new MyClass ());


t1.start();

When the thread is started it will call the run() method of the MyClass
instance instead of executing its own run() method. The above example
would print out the text "MyClass running ".

Java Thread Example by implementing Runnable interface

class Multi3 implements Runnable{


public void run(){
System.out.println("thread is running...");
}

public static void main(String args[]){


Multi3 m1=new Multi3();
Thread t1 =new Thread(m1);
t1.start();
}
}
Output:thread is running...

Example2:

class MyThread1 implements Runnable{


Thread t;
String s=null;
MyThread1(String s1) {
s=s1;
t=new Thread(this);
t.start();

}
public void run() {
System.out.println(s);

}
}

24
public class RunableThread{
public static void main(String args[]) {

MyThread1 m1=new MyThread1("Thread started....");

}
Output: Thread started....

Creation of Multiple Threads

class MyThread1 implements Runnable{


Thread t;
MyThread1(String s) {
t=new Thread(this,s);

/*Thread(Runnable r,String name)This constructor creates a thread object


having two arguments of String and Runnable type.*/

t.start();
}

public void run() {


for(int i=0;i<5;i++) {
System.out.println("Thread Name :"+Thread.currentThread().getName());
try {
Thread.sleep(1000);
}catch(Exception e){}
}
}
}

public class threadmultirunnable{


public static void main(String args[]) {
System.out.println("Thread Name :"+Thread.currentThread().getName());
MyThread1 m1=new MyThread1("My Thread 1");
MyThread1 m2=new MyThread1("My Thread 2");
}
}

Output:
Thread Name :main
Thread Name :My Thread 1
Thread Name :My Thread 2

25
Thread Name :My Thread 1
Thread Name :My Thread 2
Thread Name :My Thread 2
Thread Name :My Thread 1
Thread Name :My Thread 2
Thread Name :My Thread 1
Thread Name :My Thread 2
Thread Name :My Thread 1

Using sleep():

class TestSleepMethod1 extends Thread{


public void run()
{
for(int i=1;i<5;i++)
{
try{
Thread.sleep(500);
}catch(InterruptedException e)
{System.out.println(e);}

System.out.println(i);
}
}

public static void main(String args[]){


TestSleepMethod1 t1=new TestSleepMethod1();
TestSleepMethod1 t2=new TestSleepMethod1();

t1.start();
t2.start();
}
}

Output:
1
1
2
2
3
3
4
4

26
Thread priority in java
A thread is a part or entity of a process that is scheduled for execution.

As we know java is a multithreading language which means JVM allow an


application to have multiple thread running concurrently. In java each and
every thread has priority , priority means which decides the order of execution
of that thread . Thread with higher priority will run first with thread with lower
priority, and thread with equal priority will be treated same by scheduler and
they will follow FCFS(First Cum First Serve) algorithm. We can also set the
priority of thread by using setPriority() method as follows:

ThreadName.setPriority(int number);

Here number is an integer value between 1 to 10, here 1 is minimum priority


and 10 is maximum priority.
Thread class defines some priority constant as follows:
MIN_PRIORITY = 1
NORM_PRIORITY = 5
MAX_PRIORITY = 10;
In any thread NORM_PRIORITY is default one.
In java, each and every thread has a priority which is defined by a number
between, 1 to 10,
with 1 being the lowest priority,
10 being the highest priority,
A default priority for a thread is 5 but you can even set the priority for a
thread.

Example:

class A extends Thread


{
public void run()
{
System.out.println("Thread A strated");
for(int i=0;i<=5;i++)
{
System.out.println("For thread A = "+i);
}
System.out.println("Exit from Thread A");
}

27
class B extends Thread
{
public void run()
{
System.out.println("Thread B strated");
for(int i=0;i<=5;i++)
{
System.out.println("For thread B = "+i);
}
System.out.println("Exit from Thread B");
}

public class ThreadPriority


{
public static void main(String args[])
{
A th1=new A();
B th2=new B();
th1.setPriority(Thread.NORM_PRIORITY); //largest value be the first
th2.setPriority(th1.getPriority()-1); //lowest value be the second
System.out.println("Start thread A main");
th1.start();
System.out.println("Start Thread B main");
th2.start();
System.out.println("End of Main Thread main ");
}
}
Output:
Start thread A main
Thread A started
For thread A = 0
For thread A = 1
For thread A = 2
For thread A = 3
For thread A = 4
For thread A = 5
Exit from Thread A

Start Thread B main


Thread B started
For thread B = 0
For thread B = 1
28
For thread B = 2
For thread B = 3
For thread B = 4
For thread B = 5
Exit from Thread B
End of Main Thread main

synchronizing threads

When we start two or more threads within a program, there may be a situation
when multiple threads try to access the same resource and finally they can
produce unforeseen result due to concurrency issues. For example, if multiple
threads try to write within a same file then they may corrupt the data because
one of the threads can override data or while one thread is opening the same
file at the same time another thread might be closing the same file.

So there is a need to synchronize the action of multiple threads and make sure
that only one thread can access the resource at a given point in time. This is
implemented using a concept called monitors. Each object in Java is
associated with a monitor, which a thread can lock or unlock. Only one thread
at a time may hold a lock on a monitor.

Key to synchronization is the concept of the monitor (also called a semaphore).


A monitor is an object that is used as a mutually exclusive lock, or mutex. Only
one thread can own a monitor at a given time. When a thread acquires a lock,
it is said to have entered the monitor.

All other threads attempting to enter the locked monitor will be suspended
until the first thread exits the monitor. These other threads are said to be
waiting for the monitor. A thread that owns a monitor can reenter the same
monitor if it so desires.
Synchronization in java is the capability to control the access of multiple
threads to any shared resource,by using synchronized blocks. You keep
shared resources within this block. Following is the general form of the
synchronized statement −
Syntax

synchronized(objectidentifier) {
// Access shared variables and other shared resources
}
Here, the objectidentifier is a reference to an object whose lock associates
with the monitor that the synchronized statement represents.

29
Java Synchronization is better option where we want to allow only one thread
to access the shared resource.

The synchronization is mainly used to

1. To prevent thread interference.


2. To prevent consistency problem.

Mutual Exclusive

Mutual Exclusive helps keep threads from interfering with one another while
sharing data. This can be done by three ways in java:
1. by synchronized method
2. by synchronized block
3. by static synchronization

In this example, there is no synchronization, so output is inconsistent. Let's


see the example:
class Table{
void printTable(int n){//method not synchronized
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){System.out.println(e);}
}

}
}

class MyThread1 extends Thread{


Table t;
MyThread1(Table t){
this.t=t;
}
public void run(){
t.printTable(5);
}

}
class MyThread2 extends Thread{
Table t;
MyThread2(Table t){
this.t=t;

30
}
public void run(){
t.printTable(100);
}
}
class TestSynchronization1{
public static void main(String args[]){
Table obj = new Table();//only one object
MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
t1.start();
t2.start();
}
}
Output:

5
100
10
200
15
300
20
400
25
500
Java synchronized method

 If you declare any method as synchronized, it is known as synchronized


method.
 Synchronized method is used to lock an object for any shared resource.
 When a thread invokes a synchronized method, it automatically acquires
the lock for that object and releases it when the thread completes its
task.

//example of java synchronized method

31
class Table{
synchronized void printTable(int n){//synchronized method
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){System.out.println(e);}
}

}
}

class MyThread1 extends Thread{


Table t;
MyThread1(Table t){
this.t=t;
}
public void run(){
t.printTable(5);
}

}
class MyThread2 extends Thread{
Table t;
MyThread2(Table t){
this.t=t;
}
public void run(){
t.printTable(100);
}
}

public class TestSynchronization2{


public static void main(String args[]){
Table obj = new Table();//only one object
MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
t1.start();
t2.start();
}
}

Output:
5
10

32
15
20
25
100
200
300
400
500
Synchronized block in java
 Synchronized block can be used to perform synchronization on any
specific resource of the method.
 Suppose you have 50 lines of code in your method, but you want to
synchronize only 5 lines, you can use synchronized block.
 If you put all the codes of the method in the synchronized block, it will
work same as the synchronized method.
class Table{

void printTable(int n){


synchronized(this){//synchronized block
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){System.out.println(e);}
}
}
}//end o
f the method
}
class MyThread1 extends Thread{
Table t;
MyThread1(Table t){
this.t=t;
}
public void run(){
t.printTable(5);
}
}

33
class MyThread2 extends Thread{
Table t;
MyThread2(Table t){
this.t=t;
}
public void run(){
t.printTable(100);
}
}
public class TestSynchronizedBlock1{
public static void main(String args[]){
Table obj = new Table();//only one object
MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
t1.start();
t2.start();
}
}
Output:

5
10
15
20
25
100
200
300
400
500

Example of static synchronization


In this example we are applying synchronized keyword on the static method to
perform static synchronization.
class Table{

synchronized static void printTable(int n){


for(int i=1;i<=10;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){}
}
}

34
}

class MyThread1 extends Thread{


public void run(){
Table.printTable(1);
}
}

class MyThread2 extends Thread{


public void run(){
Table.printTable(10);
}
}

class MyThread3 extends Thread{


public void run(){
Table.printTable(100);
}
}

class MyThread4 extends Thread{


public void run(){
Table.printTable(1000);
}
}

public class TestSynchronization4{


public static void main(String t[]){
MyThread1 t1=new MyThread1();
MyThread2 t2=new MyThread2();
MyThread3 t3=new MyThread3();
MyThread4 t4=new MyThread4();
t1.start();
t2.start();
t3.start();
t4.start();
}
}

Deadlock in java
Deadlock in java is a part of multithreading. Deadlock can occur in a situation
when a thread is waiting for an object lock, that is acquired by another thread
and second thread is waiting for an object lock that is acquired by first thread.

35
Since, both threads are waiting for each other to release the lock, the condition
is called deadlock.
Deadlock describes a situation where two or more threads are blocked forever,
waiting for each other. Deadlock occurs when multiple threads need the same
locks but obtain them in different order. A Java multithreaded program may
suffer from the deadlock condition because the synchronized keyword causes
the executing thread to block while waiting for the lock, or monitor, associated
with the specified object.

Inter-thread communication in Java

Cooperation (Inter-thread communication) is a mechanism in which a thread is


paused running in its critical section and another thread is allowed to enter (or
lock) in the same critical section to be executed.

Multithreading replaces event loop programming by dividing your tasks into


discrete, logical units. Threads also provide a secondary benefit: they do away
with polling. Polling is usually implemented by a loop that is used to check
some condition repeatedly. Once the condition is true, appropriate action is
taken. This wastes CPU time.

For example, consider the classic queuing problem, where one thread is
producing some data and another is consuming it. To make the problem more
interesting, suppose that the producer has to wait until the consumer is
finished before it generates more data. In a polling system, the consumer would
waste many CPU cycles while it waited for the producer to produce. Once the
producer was finished, it would start polling, wasting more CPU cycles waiting
for the consumer to finish, and so on. Clearly, this situation is undesirable.

To avoid polling, Java includes an elegant interprocess communication


mechanism via the wait( ), notify( ), and notifyAll( ) methods. These methods
are implemented as final methods in Object, so all classes have them. All three
methods can be called only from within a synchronized context.

36
● wait()

● notify()

● notifyAll()

wait() method
Causes the current thread to wait until another thread invokes the notify().

Method Description
public final void wait()throws waits until object is notified.
InterruptedException
public final void wait(long timeout)throws waits for the specified
InterruptedException amount of time.

public void notify()

Wakes up a single thread that is waiting on this object's monitor.

public void notifyAll()

Wakes up all the threads that called wait( ) on the same object.

Difference between wait and sleep.


wait() sleep()
wait() method releases the lock sleep() method doesn't release the lock.
is the method of Object class is the method of Thread class
is the non-static method is the static method
should be notified by notify() or after the specified amount of time, sleep
notifyAll() methods is completed.

Example :

class Customer{

int amount=10000;

synchronized void withdraw(int amt){

System.out.println("going to withdraw...");

if(amount<amt){

System.out.println("Less balance; waiting for deposit...");

37
try{

wait();

catch(Exception e){ }

amount=amount-amt;

System.out.println("withdraw completed...");

synchronized void deposit(int amt){

System.out.println("going to deposit...");

amount=amount+amt;

System.out.println("deposit completed... ");

notify();

class Producer implements Runnable

Customer c;

Producer(Customer c)

this.c=c;

new Thread(this,”Producer”).start();

public void run()

c.deposit(10000);

38
class Consumer implements Runnable

Customer c;

Consumer(Customer c)

this.c=c;

new Thread(this,”Consumer”).start();

public void run()

c.withdraw(15000);

public class InterThreadDemo{

public static void main(String args[]){

Customer c=new Customer();

new Producer(c);

new Consumer(c);

output:

going to withdraw...

Less balance; waiting for deposit...

going to deposit...

deposit completed...

withdraw completed..

39

You might also like