Upload
others
View
8
Download
0
Embed Size (px)
Citation preview
SSC - Concurrency and Multi-threading
SSC - Concurrency and Multi-threadingThread pool and executor framework
Shan He
School for Computational ScienceUniversity of Birmingham
Module 06-19321: SSC
SSC - Concurrency and Multi-threading
Outline
Outline of Topics
Review what we learned
Executors framework
SSC - Concurrency and Multi-threading
Executors framework
How to create threads
I Reminder: two ways of creating threads:I Extending thread classI Implementing Runnable interface
I Essentially two ways of doing the same thing butimplementing Runnable interface is preferred, because:
I easy to use, e.g., simply defines the unit of work that will beexecuted in a thread.
I can extend a class other than Thread
SSC - Concurrency and Multi-threading
Executors framework
Implementing the Runnable InterfaceThe steps for creating a thread by implementing the Runnable
Interface are:
I Step 1: Create a class that implements the Runnable
interface:class MyRunnable implements Runnable
I Step 2: Override the run() method in MyRunnable
class to implement the thread body of execution:public void run() { thread body... }
I Step 3: Create a MyRunnable object r :
MyRunnable r = new MyRunnable();
I Step 4: Create a thread object t and pass r as an
argument: Thread t = new Thread(r);
I Step 5: Start execution of created thread: t.start();
SSC - Concurrency and Multi-threading
Executors framework
Implementing the Runnable Interface
MyRunnable
Runnable Thread
Class
Interface
Instantiation
Implements(Realisation)
r
Object
tRunnable =
SSC - Concurrency and Multi-threading
Executors framework
To create threads or not?
I Creating threads is expensive compared with a single thread:overheads include memory allocation, e.g., stack and register
I You can use several threads to execute several tasks (units ofwork), but how about a large-scale concurrent applicationthat involves many tasks?
I Too many threads will consumes too many resources −→performance issues
I Creation, management and coordination for threads aredifficult
SSC - Concurrency and Multi-threading
Executors framework
Thread pools
I Thread pool: a managed collection of worker threads that arecreated and waiting to perform tasks.
I A thread pool also contains a job queue which holds taskswaiting to get executed.
I Benefits of thread pools:I Improved performance when executing large numbers of tasks:
reuses worker threads to reduce per-task invocation overhead.I A means of bounding the resources consumed by threads when
executing a collection of tasks.I No management of the life cycle of threads. You just need to
focus on the tasks that you want the threads to perform,instead of creating, managing and coordinating threads.
I The best tool for creating thread pools in Java is Executorsframework interface
SSC - Concurrency and Multi-threading
Executors framework
Thread Pool
Thread 1
Thread 2
Thread n
Task 1
Job queue
Task 2
Task 3
Thread pool
SSC - Concurrency and Multi-threading
Executors framework
What is Executors framework?I From Oracle: a framework for standardizing invocation,
scheduling, execution, and control of asynchronous tasksaccording to a set of execution policies.
I In plain English: a framework that decouples threadmanagement and creation from the asynchronous tasks by:
I running the Runnable/Callable (explain later) thread objectswithout creating Thread objects.
I re-using the already created threadsI Three interfaces in the Executors framework:
I Executor : A very simple interface that provides only onemethod, execute , to launch new threads
I ExecutorService : a subinterface of Executor, which addsfeatures that help manage the lifecycle, both of the individualtasks and of the executor itself
I ScheduledExecutorService : a subinterface ofExecutorService, supports periodic execution of tasks.
SSC - Concurrency and Multi-threading
Executors framework
Explanation of asynchronous and synchronous execution
Task A
Task B
Task ABlocked
Call Return
Time
Task A
Task B
Synchronous execution
Asynchronous execution Message
SSC - Concurrency and Multi-threading
Executors framework
Executors framework interfaces and classes
Executor
ExecutorService ScheduledExecutorService
InterfaceExtends
(Generalisation)
SSC - Concurrency and Multi-threading
Executors framework
A few important concepts: Callable
I Runnable: An interface should be implemented by any classwhose instances are intended to be executed by a thread.
I Callable: An interface that can be implemented as a task thatreturns a result and may throw an exception.
I Runnable vs Callable:I Similarity: both are designed for classes whose instances are
potentially executed by another thread.I Difference: Runnable does not return a result and cannot
throw an exception.
I Callable = Runnable objects with a return value.
SSC - Concurrency and Multi-threading
Executors framework
A few important concepts: Future
I Future : an interface that represents the result of anasynchronous task, which provides methods to:
I Check if the task is complete: isDone()I Wait for the task to be completed and then retrieve the result
of the task: get()
I Cancel the task: cancel()I Check if this task was cancelled before it completed normally:
isCancelled()
I Executor framework implements Future for monitoring oneor more asynchronous tasks
SSC - Concurrency and Multi-threading
Executors framework
ExecutorService interface
I To use the Executors framework, the rule of thumb isExecutorService
I ExecutorService : this interface enables you toI to launch new threadsI halt the execution process (halt the service)I invoke Callable/Runnable objects in a number of waysI return values represented by Future object, which
essentially allow for combining asynchronous task results, e.g.,results of several Callable objects.
I Ideal for creating thread pools: instantiate an implementationof the ExecutorService interface and give it a set of tasks.
SSC - Concurrency and Multi-threading
Executors framework
ExecutorService methods
ExecutorService Methods
Method Description
newFixedThreadPool(int) creates a fixed size thread pool.
execute(Runnable) Takes a Runnable object, and exe-cutes it asynchronously.
submit(Runnable) takes a Runnable object and re-turns a Future object (ONLY tocheck if the Runnable is finished).
submit(Callable) Similar to above but the Callable’sresult can be obtained via the re-turned Future object .
SSC - Concurrency and Multi-threading
Executors framework
execute(Runnable) vs submit()
I execute(Runnable) does not return anything.
I submit(Runnable/Callable) returns a Future object
I You can cancel the task prematurely, using the cancel
method in the Future object.I If you are using submit(Runnable) , you can also wait for
the completion of the Runnable and get any exception itthrows.
I If you are using submit(Callable) , you can wait for thetask to finish executing and retrieve result of asynchronouscomputation using get .
SSC - Concurrency and Multi-threading
Executors framework
How to use ExecutorService
I We use ExecutorService to create a fixed thread pool.I Steps:
I Step 1: to create an ExecutorService usingnewFixedThreadPool factory method:
ExecutorService pool = Executors.newFixedThreadPool(3);
I Step 2: to execute Runnable objects using execute(Runnable),which creates a new thread and launches it immediately
I Or Step 2 (better): to submit a Runnable/Callable object,which returns a Future object to retrieve the Callable returnvalue and to manage the status of both Callable and Runnabletasks:Future<V> future = pool.submit(callable);
I Step 3: shutdown of the ExecutorService:pool.shutdown();
Note: a factory method is a method that instantiates objects.
SSC - Concurrency and Multi-threading
Executors framework
Suggestions for your assignment
I Use JSoup to parse a webpage for image linksI Use Java Executors framework to satisfy the following three
requirements:I Putting those image links in a queue for download.I Executing multiple (>= 2) threads to download the images in
the queueI User can specify the number of threads for downloading
images in an appropriate swing component.I I will explain how to do multi-threaded GUI programming next
week.