0% found this document useful (0 votes)
4 views

Concurrency (ExecutorService)

The document provides an overview of the ExecutorService interface in Java's Concurrency API, detailing its role in managing threads and types of executor services such as single thread, cached thread, and fixed thread pools. It explains the differences between Callable and Runnable, introduces the Future interface for handling results from asynchronous computations, and discusses scheduling tasks with the ScheduledExecutorService. Code examples are referenced for practical implementation of these concepts.

Uploaded by

srrm.nnn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Concurrency (ExecutorService)

The document provides an overview of the ExecutorService interface in Java's Concurrency API, detailing its role in managing threads and types of executor services such as single thread, cached thread, and fixed thread pools. It explains the differences between Callable and Runnable, introduces the Future interface for handling results from asynchronous computations, and discusses scheduling tasks with the ScheduledExecutorService. Code examples are referenced for practical implementation of these concepts.

Uploaded by

srrm.nnn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Concurrency

ExecutorService

Copyright © Seán Kennedy


Overview

• ExecutorService
• types of executor service

• Callable<V> (Callable<V> versus Runnable)

• Future<V>

• Code (for the above)

• Scheduling tasks; code


2

Copyright © Seán Kennedy


ExecutorService interface
• The Concurrency API abstracts thread management for us
i.e. it enables complex processing involving threads without
us having to manage threads directly.

• The ExecutorService is an interface that provides services


for the creation and management of threads.

• The Executors utility class provides static methods that


return ExecutorService implementation.

• A “thread pool” is a set of reusable worker threads available


to execute tasks. 3

Copyright © Seán Kennedy


Types of ExecutorService
• Single thread pool executor
• a single thread is used; tasks are processed sequentially.

• Cached thread pool executor


• creates new threads as needed and reuses threads that have
become free.
• care needed as the number of threads can become very large.

• Fixed thread pool executor


• creates a fixed number of threads which is specified at
the start.

Copyright © Seán Kennedy


5

Copyright © Seán Kennedy


Submitting tasks to an ExecutorService
• A Callable<V> is very similar to a Runnable except that a
Callable can return a result and throw a checked exception.

Runnable Callable<V>
Asynchronous Yes Yes

Represents a task to be Yes Yes


executed by thread
Functional interface Yes Yes

Functional method void run() V call() throws Exception

ExecutorService void execute(Runnable)


Future<?> submit(Runnable) Future<T> submit(Callable<T>)
6

Copyright © Seán Kennedy


Future<V> interface
• A Future<V> is used to obtain the results from a Callable’s
call() method.

• A Future<V> object represents the result of an


asynchronous computation. Methods are provided to check if
the computation is complete (isDone()) and to retrieve the
result of that computation (get()).

• The result can only be retrieved using the method V get()


when the computation has completed, blocking if necessary
until it is ready.

Copyright © Seán Kennedy


Code:
RunnableTest.java, CallableTest.java,
SubmittingTaskCollections.java

Copyright © Seán Kennedy


Scheduling tasks
• Executors exists that enable us to schedule tasks to be
performed at some time in the future.

• In addition, tasks can be scheduled to occur repeatedly at a


particular interval.

• To create scheduled executors, use the Executors utility


class:
• ScheduledExecutorService newSingleThreadScheduledExecutor()
• ScheduledExecutorService newScheduledThreadPool()

Copyright © Seán Kennedy


Scheduling tasks
• The ScheduledExecutorService interface provides 4
methods to schedule tasks:

• schedule(Runnable task, long delay, TimeUnit unit)


• schedule(Callable<V> task, long delay, TimeUnit unit)
• scheduleAtFixedRate(Runnable task, long initialDelay, long
periodToWait, TimeUnit unit)
• scheduleWithFixedDelay(Runnable task, long initialDelay,
long delayBetweenEndOfOneAndStartOfNext, TimeUnit unit)

10

Copyright © Seán Kennedy


Code:
ScheduledExecutors.java

Copyright © Seán Kennedy

You might also like