Java Chapter - 06-1
Java Chapter - 06-1
CHAPTER – 06
MULTITHREADING IN JAVA
Ex: - An MS word program can check the spelling of word while we type the document.
Java program make use of both process based and thread based multitasking.
What is a Thread. 1M
Thread: ”A single line of execution or process of execution”.
What is Multi-Threaded. 1M
Multithreaded=>”Multiple line of execution”
Daemon thread usually designed to run in the background for the purpose of servicing
user thread.
User thread: - The main thread is a user thread made a variable by JVM. This thread is a
launched in public static void main method. From there main thread we can create all
other threads. We already work with one particular thread since from our java first
program i.e. main thread
{
public void run ()
{
for (i=1; i<=5; i++) System.out.println (“thread B:”+i);
System.out.println (“exit from B”);
}
}
public class Ex
{
public static void main (string args [0])
{
Aobj.1=new A ();
B obj.2=new B ();
Obj1.start ();
Obj2.start ();
}
}
Output
Thread A: 1
Thread B: 1
Thread A: 2
Thread B: 2
Thread A: 3
Thread B: 3
Thread A: 5
Thread B: 5 Exit from B Exit from A
{
public void run();
}
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”);
}
}
class B implements runnable
{
public void run()
{
for(int i=1;i<=5; i++)
System.out.println (“Thread B:”+i);
System.out.println (“exit from B”);
}
}
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();
}
}
Born Stage
Start()
run()
sleep() Stop()
Runnable Running
wait() Dead Stage
Stop()
Notify()
suspend() resume(
Blocked Stage
During the life time of a Thread there are many stages of a Thread can enter they are
1. Born state
2. Runnable state
3. Running state
4. Blocked state
5. Dead state
1. Born state:- While creating the object for a thread class and this object is not yet
running. Thenthe Thread is new born state.
2. Runnable state :- When the start() is called the Thread object, the thread is in
the runnablestate.
3. Running state:- A thread currently being executed by the cpu is in the running state.
4. Blocked state:- When the thread is suspended, sleeping or waiting in order to
satisfied certainrequirement then the thread is in blocked state.
5. Dead state:- A running thread ends its life when it has computed its execution
(Run()), then it iscalled a natural death.
setPriority :- It is used to set the priority that can increase the chances of the execution.(Public
t1.setPriority(10);
PROGRAM:
import java.io.*;
import java.lang.*;
class A extends Thread
{
public void run ()
{
for(int i=1;i<=5;i++)
System.out println(“Thread A “+i);
System.out.println (“Exit from A”);
}
}
class B extends Thread
{
public void run ()
{
for (int i=1;i<=5;i++) System.out.println (“Thread B“+i);
System.out.println (“Exit from B”);
}
}
public class Ex
{
public static void main (String args[])
{
A obj=new A (); B obj=new B ();
obj2.setPriority (10);
obj1.setPriority (1);
obj1.setName (“1st Thread”);
obj2.setName (“2nd Thread);
obj1.start ();
obj2.start ();
System.out.println (“Thread 1 Name=”+obj1.getName ());
System.out.println (“Thread 2 Name=”+obj2.getName ());
}
}
What is Synchronization. 1M
“It is the process of avoiding multiple Thread to act on same data or method”
Synchronization is uses while execution time, the one Thread is under execution
process there is nopossibility 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
What is an Exception? 1M
“An error occurs at runtime is called an Exception.”
Exception
FileNotFoundException
illegalAccessException
try :
try
{
……………
…………… //java statements
}
The try block consists of a executable statements that can possible to throw exceptions
implicitly. try block throw the exceptions implicitly, if the exception occurs in this block.
catch block:
try
{
……
…… //java statements
}
catch(exception_type e)
{
……
}
catch(Exception_type1 e)
{
…..
…..
}
catch(Exception_type2 e)
{
……
…….
}
We can also use multiple catch blocks to handle different types of Exceptions. If the try block
throw an Exception,that will matches the catch block Exception type and processing the
Exception in the particular catch block.
Ex: try
{
int y=a[1]/a[0]; System.out.println(“Y=”+y);
a[2]=30;
}
catch(Arithmeticexception e)
{
System.out.println(“Don‟t‟t divide by zero”);
}
catch(ArrayIndexoutOfBoundsException e)
{
System.out.println(“It is Array Index Out of Bounds”);
}
catch(ArrayStoreException e)
{
System.out.println(e.getMessage());
}
}
[Note: Checked Exception classes and Unchecked Exception classes see the Text book or
browse in the Internet.]
Ex:
import java.io.*;
public class Ex
{
public static void main(string args[])
{
int size;
int[] a=new int[10];
size=Integer.parseint(args[0]);
try
{
if(size<0)
throw new negativeArraySizeException(“Plz enter positive size”);
for(int i=0;i<size;i++)
a[i]=i+1;
}
catch(NegativeArraySizeException e)
{
System.out.println(e);
}
}
throws keyword(clause)
“The throws keyword is used when the programmer doesn‟t wants to handle the exception in
the method(inside the method) and throw it out of method.”
The method must declare it using the „throws‟ keyword. „throws‟ keyword appears at the end of
a method signature ,if multiple exceptions are there ,we can separate it with comma operator(,).
Syntax:
return_typemethod_name(parameter_list)throws Exception_type1,Exception_type2,…..
……..
……..
public class Ex
{
public static void main(String args[])throws IOException,NumberFormatException
{
…….
…….
}
}
finally block
“A finally block of code always executes whether an Exception as occurred or not.” Finally
block appears at the end of the catch block or at the end of try block.
try
No Exception Exception
finally catch
finally
Syntax:
try
{
}
………
……….
catch(Exception_type e)
{
…….
…….
}
finally
{
}
……..
…….. //finally block always excecutes.
Ex:
importjava.io.*;
public class Ex
{
public static void main(String args[])
{
int size;
int[] a=new int[10];
size=Integer.parseint(args[0]);
try
{
}
finally
{
if(size<0)
throw new NegativeArraySizeException(“Plz enter positive size”);
for(int i=0;i<size;i++)
a[i]=i+1;
import java.io.*;
class Vvfgc extends NegativeArraySizeException
{
Vvfgc()
{
super(“plz enter +ve array size”);
}
}
public class Ex
{
public static void main(String args[])
{
int size;
int[] a=new int[10]; size=Integer.parseInt(args[0]);
try
{
}
if(size<0)
throw new Vvfgc();
System.out.println(“This is +ve Array Size”);
catch(Vvfgc e)
{
System.out.println(e);
}
}
}
Here we have created our own exception class called as Vvfgc, which is a sub class os
NegativeArraySizeException. By running the program with negative value, the output will
be,
Vvfgc: plz enter +ve array size.
The Collection interface (java.util.Collection) and Map interface (java.util.Map) are the two
main “root” interfaces of Java collection classes.
What is a Framework?1M
Class: A class is a user-defined blueprint or prototype from which objects are created. It
represents the set of properties or methods that are common to all objects of one type.
Interface: Like a class, an interface can have methods and variables, but the methods
declared in an interface are by default abstract (only method signature, no body).
Interfaces specify what a class must do and not how. It is the blueprint of the class.