Object Oriented Programming: Lecture-23 - 24 Instructor Name
Object Oriented Programming: Lecture-23 - 24 Instructor Name
Object Oriented Programming: Lecture-23 - 24 Instructor Name
Instructor Name:
Lecture-23 - 24
Today’s Lecture
Multithreading
2
Multithreading
Introduction
Performing One Action at a time is nice
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.
3
Multithreading
5
Multithreading
6
Multithreading
7
Multithreading
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
9
time—called a quantum or timeslice—with which to perform its task.
Multithreading
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;
14
Creating and Executing Threads with Executor
Framework
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
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");
23
Creating and Executing Threads with Executor
Framework
// create ExecutorService to manage threads
ExecutorService threadExecutor =
Executors.newCachedThreadPool();