Multithread Programing in Java
Transcript of Multithread Programing in Java
Multithreading
M. Raihan
Computer Science and Engineering Discipline , Khulna University
What is Thread ?
A thread is similar to a program that has a single flow of control
A thread has a beginning , a body and an end and executes commands sequentially.
Each thread is a statically ordered sequence ofinstructions.
Threads are being extensively used express concurrency on both single and multiprocessors machines.
What is Multithread?
Multithreading is the ability of a program or anoperating system process to manage its use bymore than one user at a time and to even managemultiple requests by the same user withouthaving to have multiple copies of theprogramming running in the computer.
A Single Thread
A Multithread Program
Threads Methods
Java has built in thread support for Multithreading
Thread Methods :
Thread Declaration
Thread can be declared in two ways :
Create a class that extends the Thread class
Create a class that implements the Runnable interface
1st way:
Create a class by extending Thread class and overriderun() method:
class MyThread extends Thread{public void run(){// thread body of execution}
}Create a thread:
MyThread thr1 = new MyThread();
Start Execution of threads:
thr1.start();
Create and Execute:
new MyThread().start();
2nd way: Create a class that implements the interface Runnable
and override run() method:
class MyThread implements Runnable{.....public void run(){// thread body of execution}}
Creating Object:MyThread myObject = new MyThread();Creating Thread Object:Thread thr1 = new Thread( myObject );Start Execution:thr1.start();
Life Circle
Synchronization
Synchronization in java is the capability of control the access ofmultiple threads to any shared resource.
Java Synchronization is better option where we want to allow only onethread to access the shared resource.
Why we use :
1. To prevent thread interference.
2. To prevent consistency problem.
Synchronization
Synchronization in java is the capability of control the access of multiple threads toany shared resource.
Java Synchronization is better option where we want to allow only one thread toaccess the shared resource.
Why we use :
1. To prevent thread interference.
2. To prevent consistency problem.
Synchronized method is used to lock an object for any shared resource.
Example
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);}
}
}
}
Example (Continue)class MyThread1 extends Thread{
Table t;
MyThread1(Table t){
this.t=t;
}
public void run(){
t.printTable(2);
}
}
class MyThread2 extends Thread{
Table t;
MyThread2(Table t){
this.t=t;
}
public void run(){
t.printTable(100);
}
}
Example (Continue)
public class TestSynchronization{
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();
}
}
Thank You