Multithread Programing in Java

16
Multithreading M. Raihan Computer Science and Engineering Discipline , Khulna University

Transcript of Multithread Programing in Java

Page 1: Multithread Programing in Java

Multithreading

M. Raihan

Computer Science and Engineering Discipline , Khulna University

Page 2: Multithread Programing in Java

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.

Page 3: Multithread Programing in Java

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.

Page 4: Multithread Programing in Java

A Single Thread

Page 5: Multithread Programing in Java

A Multithread Program

Page 6: Multithread Programing in Java

Threads Methods

Java has built in thread support for Multithreading

Thread Methods :

Page 7: Multithread Programing in Java

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

Page 8: Multithread Programing in Java

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();

Page 9: Multithread Programing in Java

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();

Page 10: Multithread Programing in Java

Life Circle

Page 11: Multithread Programing in Java

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.

Page 12: Multithread Programing in Java

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.

Page 13: Multithread Programing in Java

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);}

}

}

}

Page 14: Multithread Programing in Java

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);

}

}

Page 15: Multithread Programing in Java

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();

}

}

Page 16: Multithread Programing in Java

Thank You