Aim:
To implement multi threading and exception handling using java program.
CREATION OF THREADING IN JAVA:
How to create thread
There are two ways to create a thread:
- By extending Thread class
- By implementing Runnable interface.
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.
Commonly used Constructors of Thread class:
- Thread()
- Thread(String name)
- Thread(Runnable r)
- Thread(Runnable r,String name)
Commonly used methods of Thread class:
- public void run(): is used to perform action for a thread.
- public void start(): starts the execution of the thread.JVM calls the run() method on the thread.
- public void sleep(long miliseconds): Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.
- public void join(): waits for a thread to die.
- public void join(long miliseconds): waits for a thread to die for the specified miliseconds.
- public int getPriority(): returns the priority of the thread.
- public int setPriority(int priority): changes the priority of the thread.
- public String getName(): returns the name of the thread.
- public void setName(String name): changes the name of the thread.
- public Thread currentThread(): returns the reference of currently executing thread.
- public int getId(): returns the id of the thread.
- public Thread.State getState(): returns the state of the thread.
- public boolean isAlive(): tests if the thread is alive.
- public void yield(): causes the currently executing thread object to temporarily pause and allow other threads to execute.
- public void suspend(): is used to suspend the thread(depricated).
- public void resume(): is used to resume the suspended thread(depricated).
- public void stop(): is used to stop the thread(depricated).
- public boolean isDaemon(): tests if the thread is a daemon thread.
- public void setDaemon(boolean b): marks the thread as daemon or user thread.
- public void interrupt(): interrupts the thread.
- public boolean isInterrupted(): tests if the thread has been interrupted.
- public static boolean interrupted(): tests if the current thread has been interrupted.
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.
Code language: JavaScript (javascript)
Starting a thread:
start() method of Thread class is used to start a newly created thread. It performs following tasks:
- A new thread starts(with new callstack).
- The thread moves from New state to the Runnable state.
When the thread gets a chance to execute, its target run() method will run.
1) Java Thread Example by extending Thread class
class Multi extends Thread{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi t1=new Multi();
t1.start();
}
}
Code language: JavaScript (javascript)
2) 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();
}
}
Code language: PHP (php)
If you are not extending the Thread class,your class object would not be treated as a thread object.So you need to explicitely create Thread class object.We are passing the object of your class that implements Runnable so that your class run() method may execute.
Aim:
To implement multi threading and exception handling using java program.
Algorithm:
- Declare class Th1 extend from the thread.
- Declare try, catch with data.
- Declare another 2 classes Th2, Th3 define method.
- Declare main classes Th demo and create object T1, T2 and t3 for each class. Access it.
- Display the result.
Program:
import java.io.*; class th1 extends Thread { private String name; th1(String n) { this.name=n; } public void run() { try { if(this.name=="th2") { th2 A=new th2(); A.compute(); System.out.println("THREAD "+this.name+ " calculates sum of first 5 integers="+A.result ); } if(this.name=="th3") { th3 A=new th3(); A.compute(); System.out.println("THREAD "+this.name+ " calculates product of first 5 integers="+A.result ); } } catch (Exception e) { System.out.println("Exception is caught"); } } } class th2{ int result=0; void compute() { for(int i=1;i<=5;i++) { result=result+i; } } } class th3{ int result=1; void compute() { for(int i=1;i<=5;i++) { result=result*i; } } } public class Main { public static void Main(String []args){ String a="th2"; String b="th3"; th1 r1= new th1(a); th1 r2=new th1(b); r1.start(); r2.start(); } }
Execution:
THREAD th3 calculates product of first 5 integers = 120 THREAD th2 calculates sum of first 5 integers = 15