Sunteți pe pagina 1din 10

JAVA CONCURRENCY

java.util.concurrent - package summary

Bucharest, 15 December 2017

LOREDANA P.
Concurrency

Multi-threaded processing allows operating systems to execute threads at the same time. The property of
executing multiple threads and processes at the same time is referred to as concurrency.
The java.util.concurrent package provides utility classes for creating concurrent applications. Other often used packages
for concurrency are: java.util.concurrent.locks and java.util.concurrent.atomic.

High Level Concurrency Objects

• Executors - define a high-level API for launching and managing threads. Executor implementations from the
java.util.concurrent package provide thread pool management suitable for large-scale applications.
• Concurrent collections make it easier to manage large collections of data, and can greatly reduce the need for
synchronization.
• ThreadLocalRandom provides efficient generation of pseudorandom numbers from multiple threads.
• Lock objects support locking idioms that simplify many concurrent applications.
• Atomic variables have features that minimize synchronization and help avoid memory consistency errors.

CONCURRENCY
Executors

In large-scale applications, it makes sense to separate thread management


and creation from the rest of the application. Objects that encapsulate
these functions are known as executors.
• Executor Interfaces define the three executor object types.
• Thread Pools are the most common kind of executor implementation.
• Fork/Join is a framework for taking advantage of multiple processors.

Executor Interfaces

• Executor, a simple interface that supports launching new tasks.


• ExecutorService, a subinterface of Executor, which adds features that
help manage the lifecycle, both of the individual tasks and of the
executor itself.
• ScheduledExecutorService, a subinterface of ExecutorService, supports
future and/or periodic execution of tasks. To know when a task submitted to
an ExecutorService is complete, the Future class can be used, as it includes
methods that are useful in determining the state of a task.

EXECUTORS
Thread Pools

• Using worker threads minimizes the overhead due to thread creation.


Thread objects use a significant amount of memory, and in a largescale
application, allocating and deallocating many thread objects creates a
significant memory management overhead.
• Tasks are submitted to the pool via an internal queue, which holds
extra tasks whenever there are more active tasks than threads. Classes
ThreadPoolExecutor and ScheduledThreadPoolExecutor provide tunable,
flexible thread pools.

Fork/Join
• The fork/join framework is an implementation of the ExecutorService
interface that helps you take advantage of multiple processors. It is
designed for work that can be broken into smaller pieces recursively. The
goal is to use all the available processing power to enhance the performance
of the application.

EXECUTORS
Concurrent collections

• The concurrent collections often include performance


enhancements that avoid unnecessary synchronization.
Their methods are a superset to the non-concurrent
collection classes.
• All of these collections help avoid Memory Consistency
Errors by defining a happens-before relationship between
an operation that adds an object to the collection with
subsequent operations that access or remove that object.
A concurrent collection is thread-safe, but not governed
by a single exclusion lock. "Synchronized" classes can be
useful when you need to prevent all access to a collection
via a single lock, at the expense of poorer scalability. In
other cases in which multiple threads are expected to
access a common collection, "concurrent" versions are
normally preferable. And unsynchronized collections are
preferable when either collections are unshared, or are
accessible only when holding other locks.

CONCURRENT COLLECTIONS
Synchronization

Threads communicate primarily by sharing access to fields and the objects


reference fields refer to. This form of communication is extremely efficient, but
makes two kinds of errors possible: thread interference and memory
consistency errors. The tool needed to prevent these errors is synchronization.

Synchronization Libraries

• Semaphore - Maintain count of the number of threads allowed to pass


• CountDownLatch - Boolean conditions that are set once, ever
• CyclicBarrier - Counters that cause all threads to wait until all have finished
• Phaser - provides a more flexible form of barrier that may be used to
control phased computation among multiple threads
• Exchanger - allows two threads to exchange objects at a rendezvous point,
and is useful in several pipeline designs

SYNCHRONIZATION
ThreadLocalRandom

java.util.concurrent includes a convenience class, ThreadLocalRandom,


for applications that expect to use random numbers from multiple
threads or ForkJoinTasks.

This class also provides additional commonly used bounded random


generation methods. Instances of ThreadLocalRandom are not
cryptographically secure. Instead, use SecureRandom in security-
sensitive applications. Additionally, default-constructed instances do
not use a cryptographically random seed unless the system property
java.util.secureRandomSeed is set to true.

CONCURRENT RANDOM NUMBERS


Lock Objects

• Only one thread can own an implicit Lock object at a time. Lock
objects also support a wait/notify mechanism, through their associated
Condition objects.

• The biggest advantage of Lock objects over implicit locks is their


ability to back out of an attempt to acquire a lock. The tryLock method
backs out if the lock is not available immediately or before a timeout
expires (if specified). The lockInterruptibly method backs out if another
thread sends an interrupt before the lock is acquired.

LOCK OBJECTS
Atomic variables

The java.util.concurrent.atomic package defines classes that support


atomic operations on single variables.

Atomic is the property of an operation to be carried out as a single unit


of execution without any interference by another thread. A thread-safe
atomic version of the increment operator would be one that performed
the read and write of the variable as a single operation, not allowing
any other threads to access the variable during the operation.

All classes have get and set methods that work like reads and writes on
volatile variables.

LOCK OBJECTS
Bibliography

• https://docs.oracle.com/javase/8/docs/api/index.html?java/util/co
ncurrent/package-summary.html

• OCP – Oracle Certified Professional Java SE8 Programmer II Study


Guide - Jeanne Boyarsky, Scott Selikoff, 2016

• Illustrations: https://www.cartoonstock.com/

BIBLIOGRAPHY

S-ar putea să vă placă și