SlideShare a Scribd company logo
Lecture-23 - 24
Instructor Name:
Object Oriented Programming
Today’s Lecture
➢ Multithreading
2
Multithreading
Introduction
➢ Performing One Action at a time is nice
➢ Human Body performs variety of actions in parallel – concurrent
➢ Java makes concurrency available to you through the language and APIs.
➢ Java programs can have multiple threads of execution, where each thread
has its own method-call stack and program counter, allowing it to execute
concurrently with other threads while sharing with them application-wide
resources such as memory.
➢ This capability is called multithreading
3
Multithreading
Introduction – Concurrent Programming Uses
➢ When downloading a large file (e.g., an image, an audio clip or a
video clip) over the Internet, the user may not want to wait until the
entire clip downloads before starting the playback.
➢ To solve this problem, multiple threads can be used—one to
download the clip, and another to play it.
➢ These activities proceed concurrently.
➢ To avoid choppy playback, the threads are synchronized(that is, their
actions are coordinated) so that the player thread doesn’t begin until
there’s a sufficient amount of the clip in memory to keep the player
thread busy.
➢ The Java Virtual Machine (JVM) creates threads to run programs and
threads to perform housekeeping tasks such as garbage collection.
4
Multithreading
Introduction – Concurrent Programming is Difficult
➢ Writing multithreaded programs can be tricky.
➢ Why
➢ Try to read three books concurrently
➢ After this experiment, you’ll appreciate many of the challenges of
multithreading
▪ Switching between the books,
▪ reading briefly,
▪ remembering your place in each book,
▪ moving the book you’re reading closer so that you can see it and
pushing the books you’re not reading aside— and many more
5
Multithreading
Introduction – Use Prebuilt Classes
➢ Programming concurrent applications is difficult and error prone.
➢ If you must use synchronization in a program, you should follow
some simple guidelines.
➢ Use existing classes from the Concurrency APIs
➢ If you need even more complex capabilities, use interfaces Lock and
Condition
6
Multithreading
Thread State: Life Cycle of Thread
➢ At any time, a thread is said to be in one of severalthread states
➢ We actually need to understand what’s going on “under the hood” in
a Java multithreaded environment.
7
Multithreading
Thread State: Life Cycle of Thread
➢ New: A new thread begins its life cycle in the new state. It remains in this
state until the program starts the thread. It is also referred to as a born
thread.
➢ Runnable: After a newly born thread is started, the thread becomes
runnable. A thread in this state is considered to be executing its task.
➢ Waiting: Sometimes, a thread transitions to the waiting state while the
thread waits for another thread to perform a task. A thread transitions back
to the runnable state only when another thread signals the waiting thread to
continue executing.
➢ Timed waiting: A runnable thread can enter the timed waiting state for a
specified interval of time. A thread in this state transitions back to the
runnable state when that time interval expires or when the event it is
waiting for occurs. Timed waiting and waiting threads cannot use a
processor, even if one is available.
➢ Terminated ( Dead ): A runnable thread enters the terminated state when it 8
Multithreading
Operating System View of Runnable State
➢ The operating system hides these states from the Java Virtual Machine (JVM)
➢ When a thread first transitions to the runnable state from the new state, it’s
in the ready state.
➢ A ready thread enters the running state (i.e., begins executing) when the
operating system assigns it to a processor—also known as dispatching the
thread.
➢ In most operating systems, each thread is given a small amount of processor
time—called a quantum or timeslice—with which to perform its task.
9
Multithreading
Operating System View of Runnable State
➢ Deciding how large the quantum should be is a key topic in operating
systems courses so we don’t need to go into details.
➢ When thread’s quantum expires, the thread returns to the ready state, and
the operating system assigns another thread to the processor.
➢ Transitions between the ready and running states are handled solely by the
operating system.
➢ The JVM does not “see” the transitions—it simply views the thread as being
runnable
➢ The process that an operating system uses to determine which thread to
dispatch is called thread scheduling and is dependent on thread priorities.
10
Creating Thread by Implementing runnable
Interface
Step 1
➢ As a first step you need to implement a run() method provided by Runnable
interface.
➢ This method provides entry point for the thread and you will put you
complete business logic inside this method.
➢ Following is simple syntax of run() method:
public void run()
Step 2
➢ Instantiate a Thread object using the following constructor:
Thread(Runnable threadObj, String threadName);
➢ Where, threadObj is an instance of a class that implements the Runnable
interface and threadName is the name given to the new thread.
Step 3
➢ Once Thread object is created, you can start it by calling start( ) method,
which executes a call to run( ) method.
void start( ); 11
Creating Thread by Implementing runnable
Interface
class RunnableDemo implements Runnable {
private Thread t;
private String threadName;
RunnableDemo( String name){
threadName = name;
System.out.println("Creating " + threadName );
}
public void run() {
System.out.println("Running " + threadName );
try {
for(int i = 4; i > 0; i--) {
System.out.println("Thread: " + threadName
+ ", " + i);
// Let the thread sleep for a while.
Thread.sleep(50);
}
}
12
Creating Thread by Implementing runnable
Interface
catch (InterruptedException e) {
System.out.println("Thread " + threadName + "
interrupted.");
}
System.out.println("Thread " + threadName + "
exiting.");
}
public void start ()
{
System.out.println("Starting " + threadName );
if (t == null)
{
t = new Thread (this, threadName);
t.start ();
}
}
}
13
Creating Thread by Implementing runnable
Interface
public class TestThread {
public static void main(String args[]) {
RunnableDemo R1 = new RunnableDemo( "Thread-1");
R1.start();
RunnableDemo R2 = new RunnableDemo( "Thread-2");
R2.start();
RunnableDemo R3 = new RunnableDemo( "Thread-3");
R3.start();
}
}
14
Creating and Executing Threads with Executor
Framework
Creating and Executing
Threads
with
Executor Framework
15
Creating and Executing Threads with Executor
Framework
Creating Concurrent Tasks with the Runnable Interface
➢ You implement the Runnable interface (of package java.lang) to specify a task
that can execute concurrently with other tasks.
➢ The Runnable interface declares the single method run, which contains the
code that defines the task that a Runnable object should perform.
Executing Runnable Obects with an Executor
➢ To allow a Runnable to perform its task, you must execute it.
➢ An Executor object executes Runnables.
➢ An Executor does this by creating and managing a group of threads called a
thread pool.
➢ When an Executor begins executing a Runnable, theExecutor calls the
Runnable object’s run method, which executes in the new thread
16
Creating and Executing Threads with Executor
Framework
Executing Runnable Obects with an Executor
➢ The Executor interface declares a single method named execute which
accepts a Runnable as an argument.
➢ The Executor assigns every Runnable passed to its execute method to one of
the available threads in the thread pool.
➢ If there are no available threads, the Executor creates a new thread or waits
for a thread to become available and assigns that thread the Runnable that
was passed to method execute.
➢ Using an Executor has many advantages over creating threads yourself.
➢ Executors can reuse existing threads to eliminate the overhead of creating a
new thread for each task and can improve performance by optimizing the
number of threads to ensure that the processor stays busy, without creating
so many threads that the application runs out of resources.
17
Creating and Executing Threads with Executor
Framework
Using Class Executors to Obtain an Executor Service
➢ The ExecutorService interface (of package java.util.concurrent) extends
Executor and declares various methods for managing the life cycle of
anExecutor.
➢ An object that implements theExecutorService interface can be created using
static methods declared in class Executors(of packagejava.util.concurrent).
18
Creating and Executing Threads with Executor
Framework
Implementing the RunnableInterface
➢ Class PrintTask implements Runnable , so that multiple PrintTasks can
➢ execute concurrently
➢ Variable sleepTime stores a random integer value from 0 to 5 seconds
➢ Each thread running a PrintTask sleeps for the amount of time specified by
sleep Time, then outputs its task’s name and a message indicating that it’s
done sleeping.
➢ A PrintTask executes when a thread calls the PrintTask’s run method, display
a message indicating the name of the currently executing task and that the
task is going to sleep for sleepTime milliseconds
➢ At this point, the thread loses the processor, and the system allows another
thread to execute.
➢ In the next slides we have complete code for threading with this technique
19
Creating and Executing Threads with Executor
Framework
import java.util.Random;
public class PrintTask implements Runnable
{
private final int sleepTime; // random sleep time for thread
private final String taskName; // name of task
private final static Random generator = new Random();
// constructor
public PrintTask( String name )
{
taskName = name; // set task name
// pick random sleep time between 0 and 5 seconds
sleepTime = generator.nextInt(5000);// milliseconds
} // end PrintTask constructor 20
Creating and Executing Threads with Executor
Framework
// method run contains the code that a thread will execute
public void run()
{
try{ // put thread to sleep for sleepTime amount of time
System.out.printf("%s going to sleep for %d
milliseconds.n“, taskName, sleepTime );
} // end try
catch(Exception exception ){//InterruptedException
System.out.printf("%s %sn", taskName,
"terminated prematurely due to interruption" );
} // end catch
// print task name
System.out.printf( "%s done sleepingn", taskName );
} // end method run
} // end class PrintTask 21
Creating and Executing Threads with Executor
Framework
Using theExecutorService to Manage Threads that Execute PrintTasks
➢ Class TaskExecutor uses an ExecutorService object to manage threads that
execute PrintTasks.
➢ First create and name three PrintTasks to execute.
➢ We use Executors method newCachedThreadPool to obtain an
ExecutorService that’s capable of creating new threads as they’re needed by
the application.
➢ These threads are used by ExecutorService (threadExecutor) to execute the
Runnables.
22
Creating and Executing Threads with Executor
Framework
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
public class TaskExecutor
{
public static void main( String[] args )
{
// create and name each runnable
PrintTask task1 =new PrintTask("task1");
PrintTask task2 =new PrintTask("task2");
PrintTask task3 =new PrintTask("task3");
System.out.println( "Starting Executor");
23
Creating and Executing Threads with Executor
Framework
// create ExecutorService to manage threads
ExecutorService threadExecutor =
Executors.newCachedThreadPool();
// start threads and place in runnable state
threadExecutor.execute(task1); // start task1
threadExecutor.execute(task2); // start task2
threadExecutor.execute(task3); // start task3
// shut down worker threads when their tasks complete
threadExecutor.shutdown();
System.out.println( "Tasks started, main ends.n");
} // end main
}
24
25

More Related Content

What's hot

Java Threads and Concurrency
Java Threads and ConcurrencyJava Threads and Concurrency
Java Threads and Concurrency
Sunil OS
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
junnubabu
 
Concurrency in Java
Concurrency in  JavaConcurrency in  Java
Concurrency in Java
Allan Huang
 
Multithread Programing in Java
Multithread Programing in JavaMultithread Programing in Java
Multithread Programing in Java
M. Raihan
 
javathreads
javathreadsjavathreads
javathreads
Arjun Shanka
 
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and Concurrency
Rajesh Ananda Kumar
 
Java multi threading
Java multi threadingJava multi threading
Java multi threading
Raja Sekhar
 
Multi-Threading
Multi-ThreadingMulti-Threading
Multi-Threading
Robert MacLean
 
java threads
java threadsjava threads
java threads
Waheed Warraich
 
Threads in Java
Threads in JavaThreads in Java
Threads in Java
Gaurav Aggarwal
 
Multi threading
Multi threadingMulti threading
Multi threading
gndu
 
Multi threading
Multi threadingMulti threading
Multi threading
Mavoori Soshmitha
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Multithreading
MultithreadingMultithreading
Multithreading
sagsharma
 
Threading in java - a pragmatic primer
Threading in java - a pragmatic primerThreading in java - a pragmatic primer
Threading in java - a pragmatic primer
SivaRamaSundar Devasubramaniam
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
ducquoc_vn
 
Basics of Java Concurrency
Basics of Java ConcurrencyBasics of Java Concurrency
Basics of Java Concurrency
kshanth2101
 
Chap3 multi threaded programming
Chap3 multi threaded programmingChap3 multi threaded programming
Chap3 multi threaded programming
raksharao
 
Concurrency in java
Concurrency in javaConcurrency in java
Concurrency in java
Abhra Basak
 
Threads in java
Threads in javaThreads in java
Threads in java
mukesh singh
 

What's hot (20)

Java Threads and Concurrency
Java Threads and ConcurrencyJava Threads and Concurrency
Java Threads and Concurrency
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Concurrency in Java
Concurrency in  JavaConcurrency in  Java
Concurrency in Java
 
Multithread Programing in Java
Multithread Programing in JavaMultithread Programing in Java
Multithread Programing in Java
 
javathreads
javathreadsjavathreads
javathreads
 
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and Concurrency
 
Java multi threading
Java multi threadingJava multi threading
Java multi threading
 
Multi-Threading
Multi-ThreadingMulti-Threading
Multi-Threading
 
java threads
java threadsjava threads
java threads
 
Threads in Java
Threads in JavaThreads in Java
Threads in Java
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Threading in java - a pragmatic primer
Threading in java - a pragmatic primerThreading in java - a pragmatic primer
Threading in java - a pragmatic primer
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
Basics of Java Concurrency
Basics of Java ConcurrencyBasics of Java Concurrency
Basics of Java Concurrency
 
Chap3 multi threaded programming
Chap3 multi threaded programmingChap3 multi threaded programming
Chap3 multi threaded programming
 
Concurrency in java
Concurrency in javaConcurrency in java
Concurrency in java
 
Threads in java
Threads in javaThreads in java
Threads in java
 

Viewers also liked

Unit v
Unit vUnit v
Unit v
sharonvikky
 
Jocelynn's resume
Jocelynn's resumeJocelynn's resume
Jocelynn's resume
Jocelynn Rubio
 
Go green juhu
Go green juhuGo green juhu
Go green juhu
Hansel D'Souza
 
Timothy McGonigal Resume v9(edited)-3
Timothy McGonigal Resume v9(edited)-3Timothy McGonigal Resume v9(edited)-3
Timothy McGonigal Resume v9(edited)-3
Timothy McGonigal
 
Neoclasisismo
NeoclasisismoNeoclasisismo
Neoclasisismo
Michelle Bouvier
 
Ksi dont judge challenge
Ksi dont judge challengeKsi dont judge challenge
Ksi dont judge challenge
giantbikie
 
Method of Resolving Conflict among Religious People in North Sumatera
Method of Resolving Conflict among Religious People in North SumateraMethod of Resolving Conflict among Religious People in North Sumatera
Method of Resolving Conflict among Religious People in North Sumatera
iosrjce
 
Data science 101
Data science 101Data science 101
Data science 101
University of West Florida
 
Lecture 17
Lecture 17Lecture 17
Lecture 17
talha ijaz
 
"Workstation Up" - Docker Development at Flow by Mike Roth
"Workstation Up" - Docker Development at Flow by Mike Roth"Workstation Up" - Docker Development at Flow by Mike Roth
"Workstation Up" - Docker Development at Flow by Mike Roth
Docker, Inc.
 
Structured Container Delivery by Oscar Renalias, Accenture
Structured Container Delivery by Oscar Renalias, AccentureStructured Container Delivery by Oscar Renalias, Accenture
Structured Container Delivery by Oscar Renalias, Accenture
Docker, Inc.
 

Viewers also liked (12)

Unit v
Unit vUnit v
Unit v
 
Jocelynn's resume
Jocelynn's resumeJocelynn's resume
Jocelynn's resume
 
Go green juhu
Go green juhuGo green juhu
Go green juhu
 
Timothy McGonigal Resume v9(edited)-3
Timothy McGonigal Resume v9(edited)-3Timothy McGonigal Resume v9(edited)-3
Timothy McGonigal Resume v9(edited)-3
 
Neoclasisismo
NeoclasisismoNeoclasisismo
Neoclasisismo
 
Ksi dont judge challenge
Ksi dont judge challengeKsi dont judge challenge
Ksi dont judge challenge
 
B Tech Civil Eng
B Tech Civil EngB Tech Civil Eng
B Tech Civil Eng
 
Method of Resolving Conflict among Religious People in North Sumatera
Method of Resolving Conflict among Religious People in North SumateraMethod of Resolving Conflict among Religious People in North Sumatera
Method of Resolving Conflict among Religious People in North Sumatera
 
Data science 101
Data science 101Data science 101
Data science 101
 
Lecture 17
Lecture 17Lecture 17
Lecture 17
 
"Workstation Up" - Docker Development at Flow by Mike Roth
"Workstation Up" - Docker Development at Flow by Mike Roth"Workstation Up" - Docker Development at Flow by Mike Roth
"Workstation Up" - Docker Development at Flow by Mike Roth
 
Structured Container Delivery by Oscar Renalias, Accenture
Structured Container Delivery by Oscar Renalias, AccentureStructured Container Delivery by Oscar Renalias, Accenture
Structured Container Delivery by Oscar Renalias, Accenture
 

Similar to Lecture 23-24.pptx

Md09 multithreading
Md09 multithreadingMd09 multithreading
Md09 multithreading
Rakesh Madugula
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Kavitha713564
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Kavitha713564
 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in java
Elizabeth alexander
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Arafat Hossan
 
Java Threads: Lightweight Processes
Java Threads: Lightweight ProcessesJava Threads: Lightweight Processes
Java Threads: Lightweight Processes
Isuru Perera
 
Multithreading
MultithreadingMultithreading
Multithreading
backdoor
 
Multithreading
MultithreadingMultithreading
Multithreading
Ravi Chythanya
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
Kartik Dube
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
JanmejayaPadhiary2
 
multithreading
multithreadingmultithreading
multithreading
Rajkattamuri
 
Java
JavaJava
Java
JavaJava
Multithreading
MultithreadingMultithreading
Multithreading
F K
 
Java multithreading
Java multithreadingJava multithreading
Java multithreading
Mohammed625
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
Rajkattamuri
 
Threadnotes
ThreadnotesThreadnotes
Threadnotes
Himanshu Rajput
 
Chap2 2 1
Chap2 2 1Chap2 2 1
Chap2 2 1
Hemo Chella
 
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptx
ssuserfcae42
 
Java thread life cycle
Java thread life cycleJava thread life cycle
Java thread life cycle
Archana Gopinath
 

Similar to Lecture 23-24.pptx (20)

Md09 multithreading
Md09 multithreadingMd09 multithreading
Md09 multithreading
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Java Threads: Lightweight Processes
Java Threads: Lightweight ProcessesJava Threads: Lightweight Processes
Java Threads: Lightweight Processes
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
multithreading
multithreadingmultithreading
multithreading
 
Java
JavaJava
Java
 
Java
JavaJava
Java
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Java multithreading
Java multithreadingJava multithreading
Java multithreading
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
 
Threadnotes
ThreadnotesThreadnotes
Threadnotes
 
Chap2 2 1
Chap2 2 1Chap2 2 1
Chap2 2 1
 
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptx
 
Java thread life cycle
Java thread life cycleJava thread life cycle
Java thread life cycle
 

More from talha ijaz

Lecture 22
Lecture 22Lecture 22
Lecture 22
talha ijaz
 
Lecture 20-21
Lecture 20-21Lecture 20-21
Lecture 20-21
talha ijaz
 
Lecture 19
Lecture 19Lecture 19
Lecture 19
talha ijaz
 
Lecture 18
Lecture 18Lecture 18
Lecture 18
talha ijaz
 
Lecture 12
Lecture 12Lecture 12
Lecture 12
talha ijaz
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
talha ijaz
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
talha ijaz
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
talha ijaz
 
Visual perception
Visual perceptionVisual perception
Visual perception
talha ijaz
 
Lecture 11
Lecture 11Lecture 11
Lecture 11
talha ijaz
 
Introduction
IntroductionIntroduction
Introduction
talha ijaz
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
talha ijaz
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
talha ijaz
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
talha ijaz
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
talha ijaz
 

More from talha ijaz (15)

Lecture 22
Lecture 22Lecture 22
Lecture 22
 
Lecture 20-21
Lecture 20-21Lecture 20-21
Lecture 20-21
 
Lecture 19
Lecture 19Lecture 19
Lecture 19
 
Lecture 18
Lecture 18Lecture 18
Lecture 18
 
Lecture 12
Lecture 12Lecture 12
Lecture 12
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Visual perception
Visual perceptionVisual perception
Visual perception
 
Lecture 11
Lecture 11Lecture 11
Lecture 11
 
Introduction
IntroductionIntroduction
Introduction
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 

Recently uploaded

Collective Mining | Corporate Presentation - June 2024
Collective Mining  | Corporate Presentation - June 2024Collective Mining  | Corporate Presentation - June 2024
Collective Mining | Corporate Presentation - June 2024
CollectiveMining1
 
Cleades robinson:The Diplomat is Blue
Cleades robinson:The Diplomat is BlueCleades robinson:The Diplomat is Blue
Cleades robinson:The Diplomat is Blue
Cleades Robinson
 
Collective Mining | Corporate Presentation - June 2024
Collective Mining | Corporate Presentation - June 2024Collective Mining | Corporate Presentation - June 2024
Collective Mining | Corporate Presentation - June 2024
CollectiveMining1
 
Cyberagent_For New Investors_EN_240424.pdf
Cyberagent_For New Investors_EN_240424.pdfCyberagent_For New Investors_EN_240424.pdf
Cyberagent_For New Investors_EN_240424.pdf
CyberAgent, Inc.
 
ZKsync airdrop of 3.6 billion ZK tokens is scheduled by ZKsync for next week.pdf
ZKsync airdrop of 3.6 billion ZK tokens is scheduled by ZKsync for next week.pdfZKsync airdrop of 3.6 billion ZK tokens is scheduled by ZKsync for next week.pdf
ZKsync airdrop of 3.6 billion ZK tokens is scheduled by ZKsync for next week.pdf
SOFTTECHHUB
 
Corporate Presentation Probe June 2024.pdf
Corporate Presentation Probe June 2024.pdfCorporate Presentation Probe June 2024.pdf
Corporate Presentation Probe June 2024.pdf
Probe Gold
 
快速办理(CUBoulder毕业证书)科罗拉多大学博尔德分校毕业证录取通知书一模一样
快速办理(CUBoulder毕业证书)科罗拉多大学博尔德分校毕业证录取通知书一模一样快速办理(CUBoulder毕业证书)科罗拉多大学博尔德分校毕业证录取通知书一模一样
快速办理(CUBoulder毕业证书)科罗拉多大学博尔德分校毕业证录取通知书一模一样
f3wjr2q2
 
Understanding-the-E-Way-Bill-A-Digital-Transformation-in-Logistics.pptx
Understanding-the-E-Way-Bill-A-Digital-Transformation-in-Logistics.pptxUnderstanding-the-E-Way-Bill-A-Digital-Transformation-in-Logistics.pptx
Understanding-the-E-Way-Bill-A-Digital-Transformation-in-Logistics.pptx
cosmo-soil
 
UnityNet World Environment Day Abraham Project 2024 Press Release
UnityNet World Environment Day Abraham Project 2024 Press ReleaseUnityNet World Environment Day Abraham Project 2024 Press Release
UnityNet World Environment Day Abraham Project 2024 Press Release
LHelferty
 
Methanex Investor Presentation - April 2024
Methanex Investor Presentation - April 2024Methanex Investor Presentation - April 2024
Methanex Investor Presentation - April 2024
Methanex Corporation
 
AGM Presentation Probe June 11 Final.pdf
AGM Presentation Probe June 11 Final.pdfAGM Presentation Probe June 11 Final.pdf
AGM Presentation Probe June 11 Final.pdf
Probe Gold
 
ppt-namibia-worldeconomy-standardbank.ppt
ppt-namibia-worldeconomy-standardbank.pptppt-namibia-worldeconomy-standardbank.ppt
ppt-namibia-worldeconomy-standardbank.ppt
TestOrg1
 
Osisko Gold Royalties Ltd - Corporate Presentation, June 12, 2024
Osisko Gold Royalties Ltd - Corporate Presentation, June 12, 2024Osisko Gold Royalties Ltd - Corporate Presentation, June 12, 2024
Osisko Gold Royalties Ltd - Corporate Presentation, June 12, 2024
Osisko Gold Royalties Ltd
 
Mandalay Resouces June 2024 Investor Relations PPT
Mandalay Resouces June 2024 Investor Relations PPTMandalay Resouces June 2024 Investor Relations PPT
Mandalay Resouces June 2024 Investor Relations PPT
MandalayResources
 

Recently uploaded (14)

Collective Mining | Corporate Presentation - June 2024
Collective Mining  | Corporate Presentation - June 2024Collective Mining  | Corporate Presentation - June 2024
Collective Mining | Corporate Presentation - June 2024
 
Cleades robinson:The Diplomat is Blue
Cleades robinson:The Diplomat is BlueCleades robinson:The Diplomat is Blue
Cleades robinson:The Diplomat is Blue
 
Collective Mining | Corporate Presentation - June 2024
Collective Mining | Corporate Presentation - June 2024Collective Mining | Corporate Presentation - June 2024
Collective Mining | Corporate Presentation - June 2024
 
Cyberagent_For New Investors_EN_240424.pdf
Cyberagent_For New Investors_EN_240424.pdfCyberagent_For New Investors_EN_240424.pdf
Cyberagent_For New Investors_EN_240424.pdf
 
ZKsync airdrop of 3.6 billion ZK tokens is scheduled by ZKsync for next week.pdf
ZKsync airdrop of 3.6 billion ZK tokens is scheduled by ZKsync for next week.pdfZKsync airdrop of 3.6 billion ZK tokens is scheduled by ZKsync for next week.pdf
ZKsync airdrop of 3.6 billion ZK tokens is scheduled by ZKsync for next week.pdf
 
Corporate Presentation Probe June 2024.pdf
Corporate Presentation Probe June 2024.pdfCorporate Presentation Probe June 2024.pdf
Corporate Presentation Probe June 2024.pdf
 
快速办理(CUBoulder毕业证书)科罗拉多大学博尔德分校毕业证录取通知书一模一样
快速办理(CUBoulder毕业证书)科罗拉多大学博尔德分校毕业证录取通知书一模一样快速办理(CUBoulder毕业证书)科罗拉多大学博尔德分校毕业证录取通知书一模一样
快速办理(CUBoulder毕业证书)科罗拉多大学博尔德分校毕业证录取通知书一模一样
 
Understanding-the-E-Way-Bill-A-Digital-Transformation-in-Logistics.pptx
Understanding-the-E-Way-Bill-A-Digital-Transformation-in-Logistics.pptxUnderstanding-the-E-Way-Bill-A-Digital-Transformation-in-Logistics.pptx
Understanding-the-E-Way-Bill-A-Digital-Transformation-in-Logistics.pptx
 
UnityNet World Environment Day Abraham Project 2024 Press Release
UnityNet World Environment Day Abraham Project 2024 Press ReleaseUnityNet World Environment Day Abraham Project 2024 Press Release
UnityNet World Environment Day Abraham Project 2024 Press Release
 
Methanex Investor Presentation - April 2024
Methanex Investor Presentation - April 2024Methanex Investor Presentation - April 2024
Methanex Investor Presentation - April 2024
 
AGM Presentation Probe June 11 Final.pdf
AGM Presentation Probe June 11 Final.pdfAGM Presentation Probe June 11 Final.pdf
AGM Presentation Probe June 11 Final.pdf
 
ppt-namibia-worldeconomy-standardbank.ppt
ppt-namibia-worldeconomy-standardbank.pptppt-namibia-worldeconomy-standardbank.ppt
ppt-namibia-worldeconomy-standardbank.ppt
 
Osisko Gold Royalties Ltd - Corporate Presentation, June 12, 2024
Osisko Gold Royalties Ltd - Corporate Presentation, June 12, 2024Osisko Gold Royalties Ltd - Corporate Presentation, June 12, 2024
Osisko Gold Royalties Ltd - Corporate Presentation, June 12, 2024
 
Mandalay Resouces June 2024 Investor Relations PPT
Mandalay Resouces June 2024 Investor Relations PPTMandalay Resouces June 2024 Investor Relations PPT
Mandalay Resouces June 2024 Investor Relations PPT
 

Lecture 23-24.pptx

  • 1. Lecture-23 - 24 Instructor Name: Object Oriented Programming
  • 3. Multithreading Introduction ➢ Performing One Action at a time is nice ➢ Human Body performs variety of actions in parallel – concurrent ➢ Java makes concurrency available to you through the language and APIs. ➢ Java programs can have multiple threads of execution, where each thread has its own method-call stack and program counter, allowing it to execute concurrently with other threads while sharing with them application-wide resources such as memory. ➢ This capability is called multithreading 3
  • 4. Multithreading Introduction – Concurrent Programming Uses ➢ When downloading a large file (e.g., an image, an audio clip or a video clip) over the Internet, the user may not want to wait until the entire clip downloads before starting the playback. ➢ To solve this problem, multiple threads can be used—one to download the clip, and another to play it. ➢ These activities proceed concurrently. ➢ To avoid choppy playback, the threads are synchronized(that is, their actions are coordinated) so that the player thread doesn’t begin until there’s a sufficient amount of the clip in memory to keep the player thread busy. ➢ The Java Virtual Machine (JVM) creates threads to run programs and threads to perform housekeeping tasks such as garbage collection. 4
  • 5. Multithreading Introduction – Concurrent Programming is Difficult ➢ Writing multithreaded programs can be tricky. ➢ Why ➢ Try to read three books concurrently ➢ After this experiment, you’ll appreciate many of the challenges of multithreading ▪ Switching between the books, ▪ reading briefly, ▪ remembering your place in each book, ▪ moving the book you’re reading closer so that you can see it and pushing the books you’re not reading aside— and many more 5
  • 6. Multithreading Introduction – Use Prebuilt Classes ➢ Programming concurrent applications is difficult and error prone. ➢ If you must use synchronization in a program, you should follow some simple guidelines. ➢ Use existing classes from the Concurrency APIs ➢ If you need even more complex capabilities, use interfaces Lock and Condition 6
  • 7. Multithreading Thread State: Life Cycle of Thread ➢ At any time, a thread is said to be in one of severalthread states ➢ We actually need to understand what’s going on “under the hood” in a Java multithreaded environment. 7
  • 8. Multithreading Thread State: Life Cycle of Thread ➢ New: A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born thread. ➢ Runnable: After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task. ➢ Waiting: Sometimes, a thread transitions to the waiting state while the thread waits for another thread to perform a task. A thread transitions back to the runnable state only when another thread signals the waiting thread to continue executing. ➢ Timed waiting: A runnable thread can enter the timed waiting state for a specified interval of time. A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs. Timed waiting and waiting threads cannot use a processor, even if one is available. ➢ Terminated ( Dead ): A runnable thread enters the terminated state when it 8
  • 9. Multithreading Operating System View of Runnable State ➢ The operating system hides these states from the Java Virtual Machine (JVM) ➢ When a thread first transitions to the runnable state from the new state, it’s in the ready state. ➢ A ready thread enters the running state (i.e., begins executing) when the operating system assigns it to a processor—also known as dispatching the thread. ➢ In most operating systems, each thread is given a small amount of processor time—called a quantum or timeslice—with which to perform its task. 9
  • 10. Multithreading Operating System View of Runnable State ➢ Deciding how large the quantum should be is a key topic in operating systems courses so we don’t need to go into details. ➢ When thread’s quantum expires, the thread returns to the ready state, and the operating system assigns another thread to the processor. ➢ Transitions between the ready and running states are handled solely by the operating system. ➢ The JVM does not “see” the transitions—it simply views the thread as being runnable ➢ The process that an operating system uses to determine which thread to dispatch is called thread scheduling and is dependent on thread priorities. 10
  • 11. Creating Thread by Implementing runnable Interface Step 1 ➢ As a first step you need to implement a run() method provided by Runnable interface. ➢ This method provides entry point for the thread and you will put you complete business logic inside this method. ➢ Following is simple syntax of run() method: public void run() Step 2 ➢ Instantiate a Thread object using the following constructor: Thread(Runnable threadObj, String threadName); ➢ Where, threadObj is an instance of a class that implements the Runnable interface and threadName is the name given to the new thread. Step 3 ➢ Once Thread object is created, you can start it by calling start( ) method, which executes a call to run( ) method. void start( ); 11
  • 12. Creating Thread by Implementing runnable Interface class RunnableDemo implements Runnable { private Thread t; private String threadName; RunnableDemo( String name){ threadName = name; System.out.println("Creating " + threadName ); } public void run() { System.out.println("Running " + threadName ); try { for(int i = 4; i > 0; i--) { System.out.println("Thread: " + threadName + ", " + i); // Let the thread sleep for a while. Thread.sleep(50); } } 12
  • 13. Creating Thread by Implementing runnable Interface catch (InterruptedException e) { System.out.println("Thread " + threadName + " interrupted."); } System.out.println("Thread " + threadName + " exiting."); } public void start () { System.out.println("Starting " + threadName ); if (t == null) { t = new Thread (this, threadName); t.start (); } } } 13
  • 14. Creating Thread by Implementing runnable Interface public class TestThread { public static void main(String args[]) { RunnableDemo R1 = new RunnableDemo( "Thread-1"); R1.start(); RunnableDemo R2 = new RunnableDemo( "Thread-2"); R2.start(); RunnableDemo R3 = new RunnableDemo( "Thread-3"); R3.start(); } } 14
  • 15. Creating and Executing Threads with Executor Framework Creating and Executing Threads with Executor Framework 15
  • 16. Creating and Executing Threads with Executor Framework Creating Concurrent Tasks with the Runnable Interface ➢ You implement the Runnable interface (of package java.lang) to specify a task that can execute concurrently with other tasks. ➢ The Runnable interface declares the single method run, which contains the code that defines the task that a Runnable object should perform. Executing Runnable Obects with an Executor ➢ To allow a Runnable to perform its task, you must execute it. ➢ An Executor object executes Runnables. ➢ An Executor does this by creating and managing a group of threads called a thread pool. ➢ When an Executor begins executing a Runnable, theExecutor calls the Runnable object’s run method, which executes in the new thread 16
  • 17. Creating and Executing Threads with Executor Framework Executing Runnable Obects with an Executor ➢ The Executor interface declares a single method named execute which accepts a Runnable as an argument. ➢ The Executor assigns every Runnable passed to its execute method to one of the available threads in the thread pool. ➢ If there are no available threads, the Executor creates a new thread or waits for a thread to become available and assigns that thread the Runnable that was passed to method execute. ➢ Using an Executor has many advantages over creating threads yourself. ➢ Executors can reuse existing threads to eliminate the overhead of creating a new thread for each task and can improve performance by optimizing the number of threads to ensure that the processor stays busy, without creating so many threads that the application runs out of resources. 17
  • 18. Creating and Executing Threads with Executor Framework Using Class Executors to Obtain an Executor Service ➢ The ExecutorService interface (of package java.util.concurrent) extends Executor and declares various methods for managing the life cycle of anExecutor. ➢ An object that implements theExecutorService interface can be created using static methods declared in class Executors(of packagejava.util.concurrent). 18
  • 19. Creating and Executing Threads with Executor Framework Implementing the RunnableInterface ➢ Class PrintTask implements Runnable , so that multiple PrintTasks can ➢ execute concurrently ➢ Variable sleepTime stores a random integer value from 0 to 5 seconds ➢ Each thread running a PrintTask sleeps for the amount of time specified by sleep Time, then outputs its task’s name and a message indicating that it’s done sleeping. ➢ A PrintTask executes when a thread calls the PrintTask’s run method, display a message indicating the name of the currently executing task and that the task is going to sleep for sleepTime milliseconds ➢ At this point, the thread loses the processor, and the system allows another thread to execute. ➢ In the next slides we have complete code for threading with this technique 19
  • 20. Creating and Executing Threads with Executor Framework import java.util.Random; public class PrintTask implements Runnable { private final int sleepTime; // random sleep time for thread private final String taskName; // name of task private final static Random generator = new Random(); // constructor public PrintTask( String name ) { taskName = name; // set task name // pick random sleep time between 0 and 5 seconds sleepTime = generator.nextInt(5000);// milliseconds } // end PrintTask constructor 20
  • 21. Creating and Executing Threads with Executor Framework // method run contains the code that a thread will execute public void run() { try{ // put thread to sleep for sleepTime amount of time System.out.printf("%s going to sleep for %d milliseconds.n“, taskName, sleepTime ); } // end try catch(Exception exception ){//InterruptedException System.out.printf("%s %sn", taskName, "terminated prematurely due to interruption" ); } // end catch // print task name System.out.printf( "%s done sleepingn", taskName ); } // end method run } // end class PrintTask 21
  • 22. Creating and Executing Threads with Executor Framework Using theExecutorService to Manage Threads that Execute PrintTasks ➢ Class TaskExecutor uses an ExecutorService object to manage threads that execute PrintTasks. ➢ First create and name three PrintTasks to execute. ➢ We use Executors method newCachedThreadPool to obtain an ExecutorService that’s capable of creating new threads as they’re needed by the application. ➢ These threads are used by ExecutorService (threadExecutor) to execute the Runnables. 22
  • 23. Creating and Executing Threads with Executor Framework import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; public class TaskExecutor { public static void main( String[] args ) { // create and name each runnable PrintTask task1 =new PrintTask("task1"); PrintTask task2 =new PrintTask("task2"); PrintTask task3 =new PrintTask("task3"); System.out.println( "Starting Executor"); 23
  • 24. Creating and Executing Threads with Executor Framework // create ExecutorService to manage threads ExecutorService threadExecutor = Executors.newCachedThreadPool(); // start threads and place in runnable state threadExecutor.execute(task1); // start task1 threadExecutor.execute(task2); // start task2 threadExecutor.execute(task3); // start task3 // shut down worker threads when their tasks complete threadExecutor.shutdown(); System.out.println( "Tasks started, main ends.n"); } // end main } 24
  • 25. 25