Unit-5 Multithreading in Java
Unit-5 Multithreading in Java
UNIT – 5
I/O Programming
Syllabus: Text and Binary I/O, Binary I/O classes, Object I/O, Random Access Files. Multithreading in java:
Thread life cycle and methods, Runnable interface, Thread synchronization, Exception handling with try
catchfinally.
Streams are mainly used to move data from one place to another. The java.io package
contains a large number of stream classes that provide capabilities processing all type of
data.
1. Input stream:
Input stream is an abstract class that provides the framework from which all other
input stream is derived.
2. Output stream:
Output Stream is an abstract class that provides the framework from all output stream
is derived.
2. Writer:
Writer is an abstract class that provides that framework from which all other writer
stream are derived.
Constructor
Constructor Description
RandomAccessFile(String name, Creates a random access file stream to read from, and
String mode) optionally to write to, a file with the specified name.
Multithreading in java:
THREAD LIFE CYCLE AND METHODS
1. Newborn state: In this state, a new thread begins its life cycle. It involves the
creation of the object of the thread class.
2. Runnable state: A thread, that is ready to run is then moved to the runnable
state. After invocation of start() method the thread becomes runnable.
3. Running state: In this state, the thread is currently being executed by the CPU.
4. Blocked state: In this state, the thread is kept idle/blocked. A thread can be
blocked using suspend, sleep or wait, in order to continue other threads execution.
5. Dead state: a thread enters to dead state, when a thread completes its execution
or by calling a method stop. A thread can be killed in any state.
RUNNABLE INTERFACE
A thread can be created using two ways:
1. By extending Thread class.
2. By implementing runnable interface
Runnable interface:
Define a class that implements runnable interface. The runnable interface as only one
method run. This can be implemented in our class.
Syntax: interface runnable
{
public void run();
}
Note: We can write the code for the run method in our class.
Example program:
import java.io.*;
import java.lang.*;
class A implements runnable
{
public void run()
{
for (int i=1;i<=5; i++)
System.out.println (“Thread A:”+i);
System.out.println (“exit from A”);
}
}
}
}
public class Ex
{
public static void main(String args[ ])
{
A obj1=new A();
B obj2=new B();
Thread t1=new Thread (obj1);
Thread t2=new Thread(obj2);
t1 .start();
t2.start();
}
}
SYNCHRONIZATION
It is the process of avoiding multiple Thread to act on same data or method is called
synchronization.
Synchronization is uses while execution time, the one Thread is under execution process
there is no possibility to enter another Thread into execution.
The synchronization can be achieved in java by two ways
1. By using synchronized method
2. Synchronized block
Synchronize block :-
Synchronized block is used to synchronize block of statement in a method.
Syntax:- Synchronized(object)
{
……..
……..
……..
}
EXCEPTION HANDLING
Error: An error describes any issue that arises unexpectedly that cause a computer to
not function properly.
Basically, errors are classified into 2 types.
1. Compile time errors.
Types of Exception:
1. Checked Exceptions: The exceptions, which are listed and checked by the java
compiler during compilation.
Ex: ClassNotFoundException, NoSuchMethodException etc..
2. Unchecked Exceptions: These are exceptions, which are not listed and are
identified during runtime.
Ex: ArithmeticException, NullPointerException etc..
catch:
catch block catches the exception thrown by the try block. A catch block consists of the
keyword catch followed by a single parameter that identify the type of exception and ‘e’ is
the object for the class Exception.
Try block monitors the code, which causes the exception. Immediately following the try
block there must be a catch block which specifies exception type that you want to catch
and handle the exception.
try
{
//statements causing exception
}
catch
{
//statements handling exception
thrown
}
import java.io.*;
class demo
{
public static void main(String[] args)
{
int d, a;
try
{
d=0;
a=40/d;
System.out.println(“not printed”);
}
catch(ArithmeticException e)
{
System.out.println(“divide by zero error”);
}
}
}
Output:
Divide by zero error
finally:
finally keyword is used for a block of code that will be executed after try/catch block is
executed. Finally block will be executed whether or not the exception is thrown.
This can be useful for closing the file handles and freeing up the occupied memory.
Ex: class demo
{
public static void main(String args[ ])
{
int d,a;
try
{
d=0;
a=40/d;
System.out.println(“not printed”);
}
catch(ArithmeticException e)
{
System.out.println(“divide by zero error”);
}
finally
{
System.out.println(“final block statement executed”);
}
}
} Output:
Divide by zero error
Final block statement
executed
******