SlideShare a Scribd company logo
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 of
instructions.
 Threads are being extensively used express concurrency on both single and
multiprocessors machines.
What is Multithread?
Multithreading is the ability of a program or an
operating system process to manage its use by
more than one user at a time and to even manage
multiple requests by the same user without
having to have multiple copies of the
programming 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 override
run() 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 of
multiple threads to any shared resource.
 Java Synchronization is better option where we want to allow only one
thread 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 to
any shared resource.
 Java Synchronization is better option where we want to allow only one thread to
access 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
Ad

More Related Content

What's hot (20)

Applets in java
Applets in javaApplets in java
Applets in java
Wani Zahoor
 
Java Networking
Java NetworkingJava Networking
Java Networking
Sunil OS
 
Java multi threading
Java multi threadingJava multi threading
Java multi threading
Raja Sekhar
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
Tanmoy Barman
 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
kumar gaurav
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
Shiv Mehmi
 
Inter Thread Communicationn.pptx
Inter Thread Communicationn.pptxInter Thread Communicationn.pptx
Inter Thread Communicationn.pptx
SelvakumarNSNS
 
Basic of Multithreading in JAva
Basic of Multithreading in JAvaBasic of Multithreading in JAva
Basic of Multithreading in JAva
suraj pandey
 
javathreads
javathreadsjavathreads
javathreads
Arjun Shanka
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
Madishetty Prathibha
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
Raja Sekhar
 
History Of JAVA
History Of JAVAHistory Of JAVA
History Of JAVA
ARSLANAHMED107
 
Generics
GenericsGenerics
Generics
Ravi_Kant_Sahu
 
Java Collections
Java  Collections Java  Collections
Java Collections
Kongu Engineering College, Perundurai, Erode
 
Java Collections
Java CollectionsJava Collections
Java Collections
parag
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
Vikas Jagtap
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
Shraddha
 
Interface in java
Interface in javaInterface in java
Interface in java
Kavitha713564
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
parag
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
Kasun Madusanke
 

Viewers also liked (20)

Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Raghu nath
 
Java Multi Thead Programming
Java Multi Thead ProgrammingJava Multi Thead Programming
Java Multi Thead Programming
Nishant Mevawala
 
Threads concept in java
Threads concept in javaThreads concept in java
Threads concept in java
Muthukumaran Subramanian
 
Java Course 10: Threads and Concurrency
Java Course 10: Threads and ConcurrencyJava Course 10: Threads and Concurrency
Java Course 10: Threads and Concurrency
Anton Keks
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
kamal kotecha
 
Core java slides
Core java slidesCore java slides
Core java slides
Abhilash Nair
 
Lecture 3 and 4 threads
Lecture 3 and 4  threadsLecture 3 and 4  threads
Lecture 3 and 4 threads
Rushdi Shams
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
kamal kotecha
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
Abhijit Gaikwad
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
ducquoc_vn
 
Grand Central Dispatch Design Patterns
Grand Central Dispatch Design PatternsGrand Central Dispatch Design Patterns
Grand Central Dispatch Design Patterns
Robert Brown
 
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
julien.ponge
 
Java Day-5
Java Day-5Java Day-5
Java Day-5
People Strategists
 
ScalaMeter 2014
ScalaMeter 2014ScalaMeter 2014
ScalaMeter 2014
Aleksandar Prokopec
 
Jvm Architecture
Jvm ArchitectureJvm Architecture
Jvm Architecture
ThirupathiReddy Vajjala
 
Java Concurrency in Practice
Java Concurrency in PracticeJava Concurrency in Practice
Java Concurrency in Practice
Alina Dolgikh
 
Java virtual machine
Java virtual machineJava virtual machine
Java virtual machine
Dhanith Krishna
 
QSpiders - Memory (JVM architecture)
QSpiders - Memory (JVM architecture)QSpiders - Memory (JVM architecture)
QSpiders - Memory (JVM architecture)
Qspiders - Software Testing Training Institute
 
Thread presentation
Thread presentationThread presentation
Thread presentation
AAshish Ojha
 
Understanding JVM
Understanding JVMUnderstanding JVM
Understanding JVM
Aparna Chaudhary
 
Ad

Similar to Multithread Programing in Java (20)

Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVA
Vikram Kalyani
 
Threadnotes
ThreadnotesThreadnotes
Threadnotes
Himanshu Rajput
 
Java Threads
Java ThreadsJava Threads
Java Threads
Hamid Ghorbani
 
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
nimbalkarvikram966
 
Java unit 12
Java unit 12Java unit 12
Java unit 12
Shipra Swati
 
Module - 5 merged.docx notes about engineering subjects java
Module - 5 merged.docx notes about engineering subjects javaModule - 5 merged.docx notes about engineering subjects java
Module - 5 merged.docx notes about engineering subjects java
KaviShetty
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Arafat Hossan
 
Multithreading
MultithreadingMultithreading
Multithreading
Ravi Chythanya
 
Java Multi-threading programming
Java Multi-threading programmingJava Multi-threading programming
Java Multi-threading programming
DrRajeshreeKhande
 
Multithreading
MultithreadingMultithreading
Multithreading
backdoor
 
Concept of Java Multithreading-Partially.pptx
Concept of Java Multithreading-Partially.pptxConcept of Java Multithreading-Partially.pptx
Concept of Java Multithreading-Partially.pptx
SahilKumar542
 
Java
JavaJava
Java
mdfkhan625
 
Java multithreading
Java multithreadingJava multithreading
Java multithreading
Mohammed625
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
Rajkattamuri
 
Threads in Java
Threads in JavaThreads in Java
Threads in Java
HarshaDokula
 
Java Multithreading Interview Questions PDF By ScholarHat
Java Multithreading Interview Questions PDF By ScholarHatJava Multithreading Interview Questions PDF By ScholarHat
Java Multithreading Interview Questions PDF By ScholarHat
Scholarhat
 
Programming - Java-Threads-and-Synchronization.ppt
Programming - Java-Threads-and-Synchronization.pptProgramming - Java-Threads-and-Synchronization.ppt
Programming - Java-Threads-and-Synchronization.ppt
smychung
 
this power point presentation is about concurrency and multithreading
this power point presentation is about concurrency and multithreadingthis power point presentation is about concurrency and multithreading
this power point presentation is about concurrency and multithreading
Betty333100
 
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptx
ssuserfcae42
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
JanmejayaPadhiary2
 
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVA
Vikram Kalyani
 
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
nimbalkarvikram966
 
Module - 5 merged.docx notes about engineering subjects java
Module - 5 merged.docx notes about engineering subjects javaModule - 5 merged.docx notes about engineering subjects java
Module - 5 merged.docx notes about engineering subjects java
KaviShetty
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Arafat Hossan
 
Java Multi-threading programming
Java Multi-threading programmingJava Multi-threading programming
Java Multi-threading programming
DrRajeshreeKhande
 
Multithreading
MultithreadingMultithreading
Multithreading
backdoor
 
Concept of Java Multithreading-Partially.pptx
Concept of Java Multithreading-Partially.pptxConcept of Java Multithreading-Partially.pptx
Concept of Java Multithreading-Partially.pptx
SahilKumar542
 
Java multithreading
Java multithreadingJava multithreading
Java multithreading
Mohammed625
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
Rajkattamuri
 
Java Multithreading Interview Questions PDF By ScholarHat
Java Multithreading Interview Questions PDF By ScholarHatJava Multithreading Interview Questions PDF By ScholarHat
Java Multithreading Interview Questions PDF By ScholarHat
Scholarhat
 
Programming - Java-Threads-and-Synchronization.ppt
Programming - Java-Threads-and-Synchronization.pptProgramming - Java-Threads-and-Synchronization.ppt
Programming - Java-Threads-and-Synchronization.ppt
smychung
 
this power point presentation is about concurrency and multithreading
this power point presentation is about concurrency and multithreadingthis power point presentation is about concurrency and multithreading
this power point presentation is about concurrency and multithreading
Betty333100
 
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptx
ssuserfcae42
 
Ad

More from M. Raihan (20)

Lecture 7
Lecture 7Lecture 7
Lecture 7
M. Raihan
 
Lecture 6
Lecture 6Lecture 6
Lecture 6
M. Raihan
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
M. Raihan
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
M. Raihan
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
M. Raihan
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
M. Raihan
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
M. Raihan
 
Amplifiers and biopotential amplifiers new
Amplifiers and biopotential amplifiers newAmplifiers and biopotential amplifiers new
Amplifiers and biopotential amplifiers new
M. Raihan
 
Comparison of fnir with other neuroimaging modalities relation between eeg sy...
Comparison of fnir with other neuroimaging modalities relation between eeg sy...Comparison of fnir with other neuroimaging modalities relation between eeg sy...
Comparison of fnir with other neuroimaging modalities relation between eeg sy...
M. Raihan
 
Lecture 10
Lecture 10Lecture 10
Lecture 10
M. Raihan
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
M. Raihan
 
Lecture 8
Lecture 8Lecture 8
Lecture 8
M. Raihan
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
M. Raihan
 
Lecture 6
Lecture 6Lecture 6
Lecture 6
M. Raihan
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
M. Raihan
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
M. Raihan
 
Lecture 18
Lecture 18Lecture 18
Lecture 18
M. Raihan
 
Lecture 17
Lecture 17Lecture 17
Lecture 17
M. Raihan
 
Lecture 16
Lecture 16Lecture 16
Lecture 16
M. Raihan
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
M. Raihan
 
Amplifiers and biopotential amplifiers new
Amplifiers and biopotential amplifiers newAmplifiers and biopotential amplifiers new
Amplifiers and biopotential amplifiers new
M. Raihan
 
Comparison of fnir with other neuroimaging modalities relation between eeg sy...
Comparison of fnir with other neuroimaging modalities relation between eeg sy...Comparison of fnir with other neuroimaging modalities relation between eeg sy...
Comparison of fnir with other neuroimaging modalities relation between eeg sy...
M. Raihan
 

Recently uploaded (20)

How to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 SalesHow to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 Sales
Celine George
 
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast BrooklynBridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
i4jd41bk
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
The History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptxThe History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and GuestsLDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDM Mia eStudios
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18
Celine George
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
*"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"**"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"*
Arshad Shaikh
 
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
Dr. Nasir Mustafa
 
Overview Well-Being and Creative Careers
Overview Well-Being and Creative CareersOverview Well-Being and Creative Careers
Overview Well-Being and Creative Careers
University of Amsterdam
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
How to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 SalesHow to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 Sales
Celine George
 
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast BrooklynBridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
i4jd41bk
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and GuestsLDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDM Mia eStudios
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18
Celine George
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
*"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"**"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"*
Arshad Shaikh
 
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
Dr. Nasir Mustafa
 
Overview Well-Being and Creative Careers
Overview Well-Being and Creative CareersOverview Well-Being and Creative Careers
Overview Well-Being and Creative Careers
University of Amsterdam
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 

Multithread Programing in Java

  • 1. Multithreading M. Raihan Computer Science and Engineering Discipline , Khulna University
  • 2. 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 of instructions.  Threads are being extensively used express concurrency on both single and multiprocessors machines.
  • 3. What is Multithread? Multithreading is the ability of a program or an operating system process to manage its use by more than one user at a time and to even manage multiple requests by the same user without having to have multiple copies of the programming running in the computer.
  • 6. Threads Methods  Java has built in thread support for Multithreading  Thread Methods :
  • 7. 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
  • 8. 1st way:  Create a class by extending Thread class and override run() 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();
  • 9. 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();
  • 11. Synchronization  Synchronization in java is the capability of control the access of multiple threads to any shared resource.  Java Synchronization is better option where we want to allow only one thread to access the shared resource.  Why we use : 1. To prevent thread interference. 2. To prevent consistency problem.
  • 12. Synchronization  Synchronization in java is the capability of control the access of multiple threads to any shared resource.  Java Synchronization is better option where we want to allow only one thread to access 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.
  • 13. 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);} } } }
  • 14. 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); } }
  • 15. 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(); } }
  翻译: