SlideShare une entreprise Scribd logo
1  sur  12
Télécharger pour lire hors ligne
Java Concurrency
                                           lecture notes
                                               Anton Shchastnyi
                                                  11/24/2011




A brief overview of the Java Concurrency. Main definitions, liveness issues, high level concurrency objects of
the Java Platform, Standard Edition.
Java Concurrency - Lecture Notes |2

Table of Contents

Contents
Table of Contents ..................................................................................................................................................... 2
Concurrency Definitions ........................................................................................................................................... 3
   Process VS Thread ................................................................................................................................................ 3
   Concurrency in Java .............................................................................................................................................. 3
   The Java Memory Model ...................................................................................................................................... 3
   Atomic Operation ................................................................................................................................................. 3
   Volatile.................................................................................................................................................................. 3
   Nonblocking Algorithms ....................................................................................................................................... 4
Liveness Issues .......................................................................................................................................................... 5
   Deadlock ............................................................................................................................................................... 5
   Livelock ................................................................................................................................................................. 5
   Starvation ............................................................................................................................................................. 5
   Guarded Blocks ..................................................................................................................................................... 5
   Immutable Objects ............................................................................................................................................... 5
High Level Concurrency Objects ............................................................................................................................... 6
   Locks ..................................................................................................................................................................... 6
   Executors .............................................................................................................................................................. 7
   Futures and Callables ........................................................................................................................................... 8
   java.lang.Thread VS the Executor Framework ..................................................................................................... 8
   Thread Pools ......................................................................................................................................................... 8
   Fork/Join in Java 7 ................................................................................................................................................ 9
   Concurrent Collections ....................................................................................................................................... 10
References .............................................................................................................................................................. 11
About the Author ................................................................................................................................................... 12
Java Concurrency - Lecture Notes |3

Concurrency Definitions
Concurrency is the ability to run several parts of a program in parallel.
Concurrency can highly improve the speed of a program if certain tasks could be performed asynchronously or in
parallel.

Process VS Thread
       Process: A process runs independently and isolated of other processes. It cannot directly access shared data
        in other processes. The resources of the process are allocated to it via the operating system, e.g. memory and
        CPU time.

       Thread: Threads are so called lightweight processes which have their own call stack but can access shared
        data. Every thread has its own memory cache. If a thread reads shared data it stores this data in its own
        memory cache. A thread can re-read the shared data, when this happens in Java will be explained in Java
        memory model part of this article.

Concurrency in Java
A Java application runs in its own process.
Within a Java application you work with several threads to achieve parallel processing or asynchronously behavior.

Java supports threads as part of the Java language. Java 1.5 also provides improved support for concurrency with the
in the package java.util.concurrent.

Java also provides locks to protect certain parts of the coding to be executed by several threads at the same time.

The simplest way of locking a certain method or Java class is to use the keyword "synchronized" in a method
declaration.

The Java Memory Model
The Java memory model describes
     the communication between the memory of the threads and the main memory
     which operations are atomic
     the ordering of the operations.

Atomic Operation
An atomic operation is an operation which is performed as a single unit of work without the possibility of
interference from other operations.

In Java the language specification guarantees that that reading or writing a variable is atomic (unless the variable is
of type long or double). Long and double are only atomic if they declared as volatile.

Volatile
The volatile modifier tells the JVM that writes to the field should always be synchronously flushed to memory, and
that reads of the field should always read from memory.

If a variable is declared as volatile then is guaranteed that any thread which reads the field will see the most recently
written value.

This means that fields marked as volatile can be safely accessed and updated in a multi-thread application without
using native or standard library-based synchronization.

(This does not apply to long or double fields, which may be non-atomic on some JVMs. However, it always applies to
reference-typed fields.)

The operation i++ is not atomic in Java for the primitives.
It first reads the value which is currently stored in i (atomic operations).
Then it increments it (atomic operation). But between the read and the write the value of i might have changed.
Java Concurrency - Lecture Notes |4


Since Java 1.5 the java language provides atomic variables
AtomicInteger, AtomicLong                                                    atomic methods:
                                                                                 getAndDecrement(),
                                                                                 getAndIncrement()
                                                                                 getAndSet()
BigDecimal is only about to be more precise than Float or Double


Nonblocking Algorithms
Java 5.0 provides supports for additional atomic operations. This allows to develop algorithm which are
non-blocking algorithm, e.g. which do not require synchronization, but are based on low-level atomic hardware
primitives such as compare-and-swap (CAS).

A CAS operation checks if the variable has a certain value and if it has this value it will perform this operation.

Non-blocking algorithm are usually much faster than blocking algorithms as the synchronization of threads appears
on a much lower level (hardware).

The JDK itself extensively uses non-blocking algorithms to increase the platform performance. Developing correct
non-blocking algorithm is not a trivial task.
Java Concurrency - Lecture Notes |5

Liveness Issues

Deadlock
Deadlock describes a situation where two or more threads are blocked forever, waiting for each other.

Livelock
A thread often acts in response to the action of another thread. If the other thread's action is also a response to the
action of another thread, then livelock may result.

As with deadlock, livelocked threads are unable to make further progress. However, the threads are not blocked —
they are simply too busy responding to each other to resume work.

This is comparable to two people attempting to pass each other in a corridor: Alphonse moves to his left to let Gaston
pass, while Gaston moves to his right to let Alphonse pass. Seeing that they are still blocking each other, Alphone
moves to his right, while Gaston moves to his left. They're still blocking each other, so...

Starvation
Starvation describes a situation where a thread is unable to gain regular access to shared resources and is unable to
make progress. This happens when shared resources are made unavailable for long periods by "greedy" threads.

For example, suppose an object provides a synchronized method that often takes a long time to return. If one thread
invokes this method frequently, other threads that also need frequent synchronized access to the same object will
often be blocked.

Guarded Blocks
Threads often have to coordinate their actions. The most common coordination idiom is the guarded block.

Such a block begins by polling a condition that must be true before the block can proceed.

Immutable Objects
An object is considered immutable if its state cannot change after it is constructed.

Maximum reliance on immutable objects is widely accepted as a sound strategy for creating simple, reliable code.

Immutable objects are particularly useful in concurrent applications. Since they cannot change state, they cannot be
corrupted by thread interference or observed in an inconsistent state.
Java Concurrency - Lecture Notes |6

High Level Concurrency Objects
Synchronized blocs and monitors with java.lang.Thread class are adequate for very basic tasks,
but higher-level building blocks are needed for more advanced tasks.

This is especially true for massively concurrent applications that fully exploit today's multiprocessor and multi-core
systems.

In this section we'll look at some of the high-level concurrency features introduced with Java 5.0.
Most of these features are implemented in the new java.util.concurrent packages. There are also new concurrent
data structures in the Java Collections Framework.

Lock objects                     Support locking idioms that simplify many concurrent applications.

Executors                        Define a high-level API for launching and managing threads.
                                 Executor implementations provided by java.util.concurrent 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.

Atomic variables                 Have features that minimize synchronization and help avoid memory consistency
                                 errors.
ThreadLocalRandom                Provides efficient generation of pseudorandom numbers from multiple threads
                                 (in JDK 7).

Locks
Lock implementations provide more extensive locking operations than can be obtained using synchronized methods
and statements.
Locks allow more flexible structuring, may have quite different properties, and may support multiple associated
Condition objects.

With this increased flexibility comes additional responsibility. The absence of block-structured locking removes the
automatic release of locks that occurs with synchronized methods and statements. In most cases, the following
idiom should be used:

       Lock l = ...;
       l.lock();
       try {
           // access the resource protected by this lock
       } finally {
           l.unlock();
       }

Lock implementations provide additional functionality over the use of synchronized methods and statements.

A Lock class can also provide behavior and semantics that is quite different from that of the implicit monitor lock,
such as guaranteed ordering, non-reentrant usage, or deadlock detection. If an implementation provides such
specialized semantics then the implementation must document those semantics.
Java Concurrency - Lecture Notes |7

java.util.concurrent.locks
Interface Lock
Method Summary
          void lock()
                    Acquires the lock (similar to entering the synchronized section).
          void lockInterruptibly()
                    Acquires the lock unless the current thread is interrupted.
    Condition newCondition()
                 Returns a new Condition instance that is bound to this Lock instance.
      boolean tryLock()
                   Acquires the lock only if it is free at the time of invocation.
      boolean tryLock(long time, TimeUnit unit)
                   Acquires the lock if it is free within the given waiting time and the current thread has not been
              interrupted.
          void unlock()
                   Releases the lock.
All Known Implementing Classes

               ReentrantLock, ReentrantReadWriteLock.ReadLock, ReentrantReadWriteLock.WriteLock



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.

Executors Types
Executor Interfaces     Executor                        A simple interface that supports launching new tasks.

                                                        (new Thread(r)).start();             →     e.execute(r);

                        ExecutorService                 A subinterface of Executor, which adds features that help
                                                        manage the lifecycle, both of the individual tasks and of the
                                                        executor itself.
                                                         execute() accepts Runnable;
                                                         submit() accepts Runnable and Callable.
                                                                        Returns a Future object, which is used to
                                                                        retrieve the Callable return value and to
                                                                        manage the status of both Callable and
                                                                        Runnable tasks.
                                                         Provides methods for submitting large collections of
                                                                        Callable objects.
                                                         Provides a number of methods for managing the shutdown
                                                                        of the executor. To support immediate
                                                                        shutdown, tasks should handle interrupts
                                                                        correctly.


                        ScheduledExecutorService        A subinterface of ExecutorService, supports future and/or
                                                        periodic execution of tasks.

                                                        The ScheduledExecutorService interface supplements the
                                                        methods of its parent ExecutorService with schedule, which
                                                        executes a Runnable or Callable task after a specified delay. In
Java Concurrency - Lecture Notes |8

                                                            addition, the interface defines scheduleAtFixedRate and
                                                            scheduleWithFixedDelay, which executes specified tasks
                                                            repeatedly, at defined intervals.

Thread Pools                                                The most common kind of executor implementation.
Fork/Join                                                   A framework (new in JDK 7) for taking advantage of multiple
                                                            processors.

Futures and Callables
Callable<V>                                                       A task that returns a result and may throw an exception.

method:                                                           Implementors define a single method with no arguments
      V call() throws Exception                                   called call().

Computes a result, or throws an exception if unable to do so.     The Callable interface is similar to Runnable, in that both
                                                                  are designed for classes whose instances are potentially
                                                                  executed by another thread. A Runnable, however, does
                                                                  not return a result and cannot throw a checked
                                                                  exception.

                                                                  The Executors class contains utility methods to convert
                                                                  from other common forms to Callable classes.

java.lang.Thread VS the Executor Framework
Using Threads directly has the following disadvantages:
     Creating a new thread causes some performance overhead
     Too many threads can lead to reduced performance, as the CPU needs to switch between these threads
     You cannot easily control the number of threads, therefore you may run into out of memory errors due to
        too many threads

The java.util.concurrent package offers improved support for concurrency compared to threads.

                      java.lang.Thread                                                 Threads pools
                                                                              with the Executor Framework
new Thread(new( RunnableTask() ).start()                          Executor executor = anExecutor;
for each of a set of tasks                                        executor.execute(new RunnableTask1());
                                                                  executor.execute(new RunnableTask2());

Thread pool manages a pool of worker threads. The thread pool contains a work queue which holds tasks waiting to
get executed.

The Executor framework provides example implementation of the java.util.concurrent.Executor.
The ExecutorService adds lifecycle methods to the Executor, which allows to shutdown the Executor and to wait for
termination.


Thread Pools
Using worker threads minimizes the overhead due to thread creation. Thread objects use a significant amount of
memory, and in a large-scale application, allocating and deallocating many thread objects creates a significant
memory management overhead.

One common type of thread pool is the fixed thread pool. This type of pool always has a specified number of threads
running; if a thread is somehow terminated while it is still in use, it is automatically replaced with a new thread.
Tasks are submitted to the pool via an internal queue, which holds extra tasks whenever there are more active tasks
than threads.
Java Concurrency - Lecture Notes |9

Executor                          An object that executes submitted Runnable tasks
ExecutorService                   An Executor that provides methods to manage termination and
                                  methods that can produce a Future for tracking progress of one or more
                                  asynchronous tasks.

                                  All Known Implementing Classes:
                                  AbstractExecutorService,
                                  ScheduledThreadPoolExecutor,
                                  ThreadPoolExecutor

                                  The ThreadPoolExecutor class provides an extensible thread pool implementation.
                                  The Executors class provides convenient factory methods for these Executors.

An important advantage of the fixed thread pool is that applications using it degrade gracefully.

To understand this, consider a web server application where each HTTP request is handled by a separate thread. If
the application simply creates a new thread for every new HTTP request, and the system receives more requests
than it can handle immediately, the application will suddenly stop responding to all requests when the overhead of
all those threads exceed the capacity of the system.

With a limit on the number of the threads that can be created, the application will not be servicing HTTP requests as
quickly as they come in, but it will be servicing them as quickly as the system can sustain.

java.util.concurrent.Executors class                Methods to create an executors that use threas pools

newFixedThreadPool()                     Creates an executor with a fixed thread pool.

newCachedThreadPool()                    Creates an executor with an expandable thread pool.
                                         This executor is suitable for applications that launch many short-lived
                                         tasks.

newSingleThreadExecutor()                Creates an executor that executes a single task at a time.


If none of the executors provided by the above factory methods meet your needs, constructing instances of
java.util.concurrent.ThreadPoolExecutor or java.util.concurrent.ScheduledThreadPoolExecutor will give you
additional options.


Fork/Join in Java 7
New in the Java SE 7 release, the fork/join framework is an implementation of the ExecutorService interface that
helps you take advantage of multiple processors.

The fork/join framework allows you to distribute a certain task on several workers and then wait for the result.
It is designed for work that can be broken into smaller pieces recursively. The goal is to use all the available
processing power to make your application wicked fast.

For Java 6 you have to add this framework manually (jsr166.jar from http://g.oswego.edu/dl/concurrency-
interest/).
J a v a C o n c u r r e n c y - L e c t u r e N o t e s | 10

  Concurrent Collections
  The java.util.concurrent package includes a number of additions to the Java Collections Framework. These are most
  easily categorized by the collection interfaces provided.
  BlockingQueue                                             Defines a FIFO data structure that blocks or times out
                                                            when you attempt to add to a full queue, or retrieve from
                                                            an empty queue.

  ConcurrentMap                                             The interface defines useful atomic operations.

                                                            These operations remove or replace a key-value pair
                                                            only if the key is present, or add a key-value pair only if
                                                            the key is absent.

                                                            Making these operations atomic helps avoid
                                                            synchronization.

  ConcurrentNavigableMap                                    This interface supports approximate matches.



                                                 java.util.Map


                                                ConcurrentMap



                                                                 ConcurrentNavigableMap


A concurrent analog of      ConcurrentHashMap                     ConcurrentSkipListMap       A concurrent analog of
HashMap                                                                                       TreeMap
J a v a C o n c u r r e n c y - L e c t u r e N o t e s | 11

References
[1] Lars Vogel, Java Concurrency / Multithreading – Tutorial,
http://www.vogella.de/articles/JavaConcurrency/article.html

[2] Oracle, Inc, Java Tutorials – Concurrency,
http://download.oracle.com/javase/tutorial/essential/concurrency/

[3] B. Goetz, with T. Peierls, J. Bloch, J. Bowbeer, D. Holmes, D. Lea, Java Concurrency in Practice,
http://www.informit.com/store/product.aspx?isbn=0321349601
J a v a C o n c u r r e n c y - L e c t u r e N o t e s | 12

About the Author
Anton Shchastnyi is a software engineer living in Ukraine. He mainly focuses on developing web and desktop
applications on Java platform. He also enjoys writing, and does quite a bit of tutorials and learning materials on Java
and related technologies (http://antonshchastnyi.blogspot.com/, http://schaan.habrahabr.ru/blog).

Contenu connexe

Tendances

Class notes(week 9) on multithreading
Class notes(week 9) on multithreadingClass notes(week 9) on multithreading
Class notes(week 9) on multithreadingKuntal Bhowmick
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8Heartin Jacob
 
Concurrency Programming in Java - 01 - Introduction to Concurrency Programming
Concurrency Programming in Java - 01 - Introduction to Concurrency ProgrammingConcurrency Programming in Java - 01 - Introduction to Concurrency Programming
Concurrency Programming in Java - 01 - Introduction to Concurrency ProgrammingSachintha Gunasena
 
Java Course 12: XML & XSL, Web & Servlets
Java Course 12: XML & XSL, Web & ServletsJava Course 12: XML & XSL, Web & Servlets
Java Course 12: XML & XSL, Web & ServletsAnton Keks
 
Java- Concurrent programming - Synchronization (part 1)
Java- Concurrent programming - Synchronization (part 1)Java- Concurrent programming - Synchronization (part 1)
Java- Concurrent programming - Synchronization (part 1)Riccardo Cardin
 
Hibernate introduction
Hibernate introductionHibernate introduction
Hibernate introductionSagar Verma
 
Core java interview questions1
Core java interview questions1Core java interview questions1
Core java interview questions1Lahari Reddy
 
Java questions for interview
Java questions for interviewJava questions for interview
Java questions for interviewKuntal Bhowmick
 
Java Course 7: Text processing, Charsets & Encodings
Java Course 7: Text processing, Charsets & EncodingsJava Course 7: Text processing, Charsets & Encodings
Java Course 7: Text processing, Charsets & EncodingsAnton Keks
 
Core java 5 days workshop stuff
Core java 5 days workshop stuffCore java 5 days workshop stuff
Core java 5 days workshop stuffRajiv Gupta
 
Thread priorities in java
Thread priorities in javaThread priorities in java
Thread priorities in javaDucat India
 

Tendances (17)

Class notes(week 9) on multithreading
Class notes(week 9) on multithreadingClass notes(week 9) on multithreading
Class notes(week 9) on multithreading
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Concurrency Programming in Java - 01 - Introduction to Concurrency Programming
Concurrency Programming in Java - 01 - Introduction to Concurrency ProgrammingConcurrency Programming in Java - 01 - Introduction to Concurrency Programming
Concurrency Programming in Java - 01 - Introduction to Concurrency Programming
 
Java Course 12: XML & XSL, Web & Servlets
Java Course 12: XML & XSL, Web & ServletsJava Course 12: XML & XSL, Web & Servlets
Java Course 12: XML & XSL, Web & Servlets
 
What is Java? Presentation On Introduction To Core Java By PSK Technologies
What is Java? Presentation On Introduction To Core Java By PSK TechnologiesWhat is Java? Presentation On Introduction To Core Java By PSK Technologies
What is Java? Presentation On Introduction To Core Java By PSK Technologies
 
Java- Concurrent programming - Synchronization (part 1)
Java- Concurrent programming - Synchronization (part 1)Java- Concurrent programming - Synchronization (part 1)
Java- Concurrent programming - Synchronization (part 1)
 
Hibernate introduction
Hibernate introductionHibernate introduction
Hibernate introduction
 
Core java interview questions1
Core java interview questions1Core java interview questions1
Core java interview questions1
 
The Java Memory Model
The Java Memory ModelThe Java Memory Model
The Java Memory Model
 
Java Memory Model
Java Memory ModelJava Memory Model
Java Memory Model
 
Java questions for interview
Java questions for interviewJava questions for interview
Java questions for interview
 
Java 7
Java 7Java 7
Java 7
 
Java Course 7: Text processing, Charsets & Encodings
Java Course 7: Text processing, Charsets & EncodingsJava Course 7: Text processing, Charsets & Encodings
Java Course 7: Text processing, Charsets & Encodings
 
Core java 5 days workshop stuff
Core java 5 days workshop stuffCore java 5 days workshop stuff
Core java 5 days workshop stuff
 
Thread priorities in java
Thread priorities in javaThread priorities in java
Thread priorities in java
 
J3d hibernate
J3d hibernateJ3d hibernate
J3d hibernate
 

En vedette

Java & low latency applications
Java & low latency applicationsJava & low latency applications
Java & low latency applicationsRuslan Shevchenko
 
Robust and Scalable Concurrent Programming: Lesson from the Trenches
Robust and Scalable Concurrent Programming: Lesson from the TrenchesRobust and Scalable Concurrent Programming: Lesson from the Trenches
Robust and Scalable Concurrent Programming: Lesson from the TrenchesSangjin Lee
 
Presentation Erfolgreiche Software mit großartiger Dokumentation - Asciidoctor
Presentation Erfolgreiche Software mit großartiger Dokumentation - AsciidoctorPresentation Erfolgreiche Software mit großartiger Dokumentation - Asciidoctor
Presentation Erfolgreiche Software mit großartiger Dokumentation - AsciidoctorRobert Panzer
 
Low latency in java 8 by Peter Lawrey
Low latency in java 8 by Peter Lawrey Low latency in java 8 by Peter Lawrey
Low latency in java 8 by Peter Lawrey J On The Beach
 
IBM Java PackedObjects
IBM Java PackedObjectsIBM Java PackedObjects
IBM Java PackedObjectsMarcel Mitran
 
Was jeder Java-Entwickler über Strings wissen sollte
Was jeder Java-Entwickler über Strings wissen sollteWas jeder Java-Entwickler über Strings wissen sollte
Was jeder Java-Entwickler über Strings wissen sollteberndmueller
 
(GAM404) Hunting Monsters in a Low-Latency Multiplayer Game on EC2
(GAM404) Hunting Monsters in a Low-Latency Multiplayer Game on EC2(GAM404) Hunting Monsters in a Low-Latency Multiplayer Game on EC2
(GAM404) Hunting Monsters in a Low-Latency Multiplayer Game on EC2Amazon Web Services
 
What are the Cool Kids Doing With Continuous Delivery?
What are the Cool Kids Doing With Continuous Delivery?What are the Cool Kids Doing With Continuous Delivery?
What are the Cool Kids Doing With Continuous Delivery?CA Technologies
 
Pitfalls of migrating projects to JDK 9
Pitfalls of migrating projects to JDK 9Pitfalls of migrating projects to JDK 9
Pitfalls of migrating projects to JDK 9Pavel Bucek
 
A Post-Apocalyptic sun.misc.Unsafe World
A Post-Apocalyptic sun.misc.Unsafe WorldA Post-Apocalyptic sun.misc.Unsafe World
A Post-Apocalyptic sun.misc.Unsafe WorldChristoph Engelbert
 
Technische Schulden in Architekturen erkennen und beseitigen
Technische Schulden in Architekturen erkennen und beseitigenTechnische Schulden in Architekturen erkennen und beseitigen
Technische Schulden in Architekturen erkennen und beseitigenCarola Lilienthal
 
Low latency Java apps
Low latency Java appsLow latency Java apps
Low latency Java appsSimon Ritter
 
Workshop: Introduction to the Disruptor
Workshop: Introduction to the DisruptorWorkshop: Introduction to the Disruptor
Workshop: Introduction to the DisruptorTrisha Gee
 
Java9 and the impact on Maven Projects (JFall 2016)
Java9 and the impact on Maven Projects (JFall 2016)Java9 and the impact on Maven Projects (JFall 2016)
Java9 and the impact on Maven Projects (JFall 2016)Robert Scholte
 
Introduction to the Disruptor
Introduction to the DisruptorIntroduction to the Disruptor
Introduction to the DisruptorTrisha Gee
 
Continuous Delivery and Infrastructure as Code
Continuous Delivery and Infrastructure as CodeContinuous Delivery and Infrastructure as Code
Continuous Delivery and Infrastructure as CodeSascha Möllering
 
Java 9 – The Ultimate Feature List
Java 9 – The Ultimate Feature ListJava 9 – The Ultimate Feature List
Java 9 – The Ultimate Feature ListTakipi
 

En vedette (20)

Get Back in Control of your SQL
Get Back in Control of your SQLGet Back in Control of your SQL
Get Back in Control of your SQL
 
Java & low latency applications
Java & low latency applicationsJava & low latency applications
Java & low latency applications
 
Die Java Plattform Strategie
Die Java Plattform StrategieDie Java Plattform Strategie
Die Java Plattform Strategie
 
Robust and Scalable Concurrent Programming: Lesson from the Trenches
Robust and Scalable Concurrent Programming: Lesson from the TrenchesRobust and Scalable Concurrent Programming: Lesson from the Trenches
Robust and Scalable Concurrent Programming: Lesson from the Trenches
 
Presentation Erfolgreiche Software mit großartiger Dokumentation - Asciidoctor
Presentation Erfolgreiche Software mit großartiger Dokumentation - AsciidoctorPresentation Erfolgreiche Software mit großartiger Dokumentation - Asciidoctor
Presentation Erfolgreiche Software mit großartiger Dokumentation - Asciidoctor
 
Low latency in java 8 by Peter Lawrey
Low latency in java 8 by Peter Lawrey Low latency in java 8 by Peter Lawrey
Low latency in java 8 by Peter Lawrey
 
IBM Java PackedObjects
IBM Java PackedObjectsIBM Java PackedObjects
IBM Java PackedObjects
 
Was jeder Java-Entwickler über Strings wissen sollte
Was jeder Java-Entwickler über Strings wissen sollteWas jeder Java-Entwickler über Strings wissen sollte
Was jeder Java-Entwickler über Strings wissen sollte
 
(GAM404) Hunting Monsters in a Low-Latency Multiplayer Game on EC2
(GAM404) Hunting Monsters in a Low-Latency Multiplayer Game on EC2(GAM404) Hunting Monsters in a Low-Latency Multiplayer Game on EC2
(GAM404) Hunting Monsters in a Low-Latency Multiplayer Game on EC2
 
What are the Cool Kids Doing With Continuous Delivery?
What are the Cool Kids Doing With Continuous Delivery?What are the Cool Kids Doing With Continuous Delivery?
What are the Cool Kids Doing With Continuous Delivery?
 
Pitfalls of migrating projects to JDK 9
Pitfalls of migrating projects to JDK 9Pitfalls of migrating projects to JDK 9
Pitfalls of migrating projects to JDK 9
 
A Post-Apocalyptic sun.misc.Unsafe World
A Post-Apocalyptic sun.misc.Unsafe WorldA Post-Apocalyptic sun.misc.Unsafe World
A Post-Apocalyptic sun.misc.Unsafe World
 
Technische Schulden in Architekturen erkennen und beseitigen
Technische Schulden in Architekturen erkennen und beseitigenTechnische Schulden in Architekturen erkennen und beseitigen
Technische Schulden in Architekturen erkennen und beseitigen
 
Low latency Java apps
Low latency Java appsLow latency Java apps
Low latency Java apps
 
Workshop: Introduction to the Disruptor
Workshop: Introduction to the DisruptorWorkshop: Introduction to the Disruptor
Workshop: Introduction to the Disruptor
 
Infrastructure as Code
Infrastructure as CodeInfrastructure as Code
Infrastructure as Code
 
Java9 and the impact on Maven Projects (JFall 2016)
Java9 and the impact on Maven Projects (JFall 2016)Java9 and the impact on Maven Projects (JFall 2016)
Java9 and the impact on Maven Projects (JFall 2016)
 
Introduction to the Disruptor
Introduction to the DisruptorIntroduction to the Disruptor
Introduction to the Disruptor
 
Continuous Delivery and Infrastructure as Code
Continuous Delivery and Infrastructure as CodeContinuous Delivery and Infrastructure as Code
Continuous Delivery and Infrastructure as Code
 
Java 9 – The Ultimate Feature List
Java 9 – The Ultimate Feature ListJava 9 – The Ultimate Feature List
Java 9 – The Ultimate Feature List
 

Similaire à Java Concurrency Quick Guide

Java interview questions and answers for cognizant By Data Council Pune
Java interview questions and answers for cognizant By Data Council PuneJava interview questions and answers for cognizant By Data Council Pune
Java interview questions and answers for cognizant By Data Council PunePankaj kshirsagar
 
Unit No 4 Exception Handling and Multithreading.pptx
Unit No 4 Exception Handling and Multithreading.pptxUnit No 4 Exception Handling and Multithreading.pptx
Unit No 4 Exception Handling and Multithreading.pptxDrYogeshDeshmukh1
 
Top 371 java fa qs useful for freshers and experienced
Top 371 java fa qs useful for freshers and experiencedTop 371 java fa qs useful for freshers and experienced
Top 371 java fa qs useful for freshers and experiencedGaurav Maheshwari
 
20 most important java programming interview questions
20 most important java programming interview questions20 most important java programming interview questions
20 most important java programming interview questionsGradeup
 
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and ConcurrencyRajesh Ananda Kumar
 
Java Interview Questions
Java Interview QuestionsJava Interview Questions
Java Interview QuestionsKuntal Bhowmick
 
Concurrency in Java
Concurrency in  JavaConcurrency in  Java
Concurrency in JavaAllan Huang
 
Java Notes: Effective Programming and Interview
Java Notes: Effective Programming and InterviewJava Notes: Effective Programming and Interview
Java Notes: Effective Programming and InterviewAbhishek Upadhyay
 
Java questions and answers jan bask.net
Java questions and answers jan bask.netJava questions and answers jan bask.net
Java questions and answers jan bask.netJanbask ItTraining
 
Lecture 2 Java Virtual Machine .pptx
Lecture 2 Java Virtual Machine .pptxLecture 2 Java Virtual Machine .pptx
Lecture 2 Java Virtual Machine .pptxAnupamKumar559254
 
Multithreading and concurrency in android
Multithreading and concurrency in androidMultithreading and concurrency in android
Multithreading and concurrency in androidRakesh Jha
 
Basics of Java Concurrency
Basics of Java ConcurrencyBasics of Java Concurrency
Basics of Java Concurrencykshanth2101
 
25 java interview questions
25 java interview questions25 java interview questions
25 java interview questionsMehtaacademy
 
Java programing considering performance
Java programing considering performanceJava programing considering performance
Java programing considering performanceRoger Xia
 
jvm/java - towards lock-free concurrency
jvm/java - towards lock-free concurrencyjvm/java - towards lock-free concurrency
jvm/java - towards lock-free concurrencyArvind Kalyan
 

Similaire à Java Concurrency Quick Guide (20)

Java interview questions and answers for cognizant By Data Council Pune
Java interview questions and answers for cognizant By Data Council PuneJava interview questions and answers for cognizant By Data Council Pune
Java interview questions and answers for cognizant By Data Council Pune
 
Unit No 4 Exception Handling and Multithreading.pptx
Unit No 4 Exception Handling and Multithreading.pptxUnit No 4 Exception Handling and Multithreading.pptx
Unit No 4 Exception Handling and Multithreading.pptx
 
Top 371 java fa qs useful for freshers and experienced
Top 371 java fa qs useful for freshers and experiencedTop 371 java fa qs useful for freshers and experienced
Top 371 java fa qs useful for freshers and experienced
 
20 most important java programming interview questions
20 most important java programming interview questions20 most important java programming interview questions
20 most important java programming interview questions
 
Java tips
Java tipsJava tips
Java tips
 
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and Concurrency
 
Java Interview Questions
Java Interview QuestionsJava Interview Questions
Java Interview Questions
 
Concurrency in Java
Concurrency in  JavaConcurrency in  Java
Concurrency in Java
 
Java Notes: Effective Programming and Interview
Java Notes: Effective Programming and InterviewJava Notes: Effective Programming and Interview
Java Notes: Effective Programming and Interview
 
Java_Interview Qns
Java_Interview QnsJava_Interview Qns
Java_Interview Qns
 
Java questions and answers jan bask.net
Java questions and answers jan bask.netJava questions and answers jan bask.net
Java questions and answers jan bask.net
 
Java Threading
Java ThreadingJava Threading
Java Threading
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
Core_Java_Interview.pdf
Core_Java_Interview.pdfCore_Java_Interview.pdf
Core_Java_Interview.pdf
 
Lecture 2 Java Virtual Machine .pptx
Lecture 2 Java Virtual Machine .pptxLecture 2 Java Virtual Machine .pptx
Lecture 2 Java Virtual Machine .pptx
 
Multithreading and concurrency in android
Multithreading and concurrency in androidMultithreading and concurrency in android
Multithreading and concurrency in android
 
Basics of Java Concurrency
Basics of Java ConcurrencyBasics of Java Concurrency
Basics of Java Concurrency
 
25 java interview questions
25 java interview questions25 java interview questions
25 java interview questions
 
Java programing considering performance
Java programing considering performanceJava programing considering performance
Java programing considering performance
 
jvm/java - towards lock-free concurrency
jvm/java - towards lock-free concurrencyjvm/java - towards lock-free concurrency
jvm/java - towards lock-free concurrency
 

Dernier

Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 

Dernier (20)

Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 

Java Concurrency Quick Guide

  • 1. Java Concurrency lecture notes Anton Shchastnyi 11/24/2011 A brief overview of the Java Concurrency. Main definitions, liveness issues, high level concurrency objects of the Java Platform, Standard Edition.
  • 2. Java Concurrency - Lecture Notes |2 Table of Contents Contents Table of Contents ..................................................................................................................................................... 2 Concurrency Definitions ........................................................................................................................................... 3 Process VS Thread ................................................................................................................................................ 3 Concurrency in Java .............................................................................................................................................. 3 The Java Memory Model ...................................................................................................................................... 3 Atomic Operation ................................................................................................................................................. 3 Volatile.................................................................................................................................................................. 3 Nonblocking Algorithms ....................................................................................................................................... 4 Liveness Issues .......................................................................................................................................................... 5 Deadlock ............................................................................................................................................................... 5 Livelock ................................................................................................................................................................. 5 Starvation ............................................................................................................................................................. 5 Guarded Blocks ..................................................................................................................................................... 5 Immutable Objects ............................................................................................................................................... 5 High Level Concurrency Objects ............................................................................................................................... 6 Locks ..................................................................................................................................................................... 6 Executors .............................................................................................................................................................. 7 Futures and Callables ........................................................................................................................................... 8 java.lang.Thread VS the Executor Framework ..................................................................................................... 8 Thread Pools ......................................................................................................................................................... 8 Fork/Join in Java 7 ................................................................................................................................................ 9 Concurrent Collections ....................................................................................................................................... 10 References .............................................................................................................................................................. 11 About the Author ................................................................................................................................................... 12
  • 3. Java Concurrency - Lecture Notes |3 Concurrency Definitions Concurrency is the ability to run several parts of a program in parallel. Concurrency can highly improve the speed of a program if certain tasks could be performed asynchronously or in parallel. Process VS Thread  Process: A process runs independently and isolated of other processes. It cannot directly access shared data in other processes. The resources of the process are allocated to it via the operating system, e.g. memory and CPU time.  Thread: Threads are so called lightweight processes which have their own call stack but can access shared data. Every thread has its own memory cache. If a thread reads shared data it stores this data in its own memory cache. A thread can re-read the shared data, when this happens in Java will be explained in Java memory model part of this article. Concurrency in Java A Java application runs in its own process. Within a Java application you work with several threads to achieve parallel processing or asynchronously behavior. Java supports threads as part of the Java language. Java 1.5 also provides improved support for concurrency with the in the package java.util.concurrent. Java also provides locks to protect certain parts of the coding to be executed by several threads at the same time. The simplest way of locking a certain method or Java class is to use the keyword "synchronized" in a method declaration. The Java Memory Model The Java memory model describes  the communication between the memory of the threads and the main memory  which operations are atomic  the ordering of the operations. Atomic Operation An atomic operation is an operation which is performed as a single unit of work without the possibility of interference from other operations. In Java the language specification guarantees that that reading or writing a variable is atomic (unless the variable is of type long or double). Long and double are only atomic if they declared as volatile. Volatile The volatile modifier tells the JVM that writes to the field should always be synchronously flushed to memory, and that reads of the field should always read from memory. If a variable is declared as volatile then is guaranteed that any thread which reads the field will see the most recently written value. This means that fields marked as volatile can be safely accessed and updated in a multi-thread application without using native or standard library-based synchronization. (This does not apply to long or double fields, which may be non-atomic on some JVMs. However, it always applies to reference-typed fields.) The operation i++ is not atomic in Java for the primitives. It first reads the value which is currently stored in i (atomic operations). Then it increments it (atomic operation). But between the read and the write the value of i might have changed.
  • 4. Java Concurrency - Lecture Notes |4 Since Java 1.5 the java language provides atomic variables AtomicInteger, AtomicLong atomic methods: getAndDecrement(), getAndIncrement() getAndSet() BigDecimal is only about to be more precise than Float or Double Nonblocking Algorithms Java 5.0 provides supports for additional atomic operations. This allows to develop algorithm which are non-blocking algorithm, e.g. which do not require synchronization, but are based on low-level atomic hardware primitives such as compare-and-swap (CAS). A CAS operation checks if the variable has a certain value and if it has this value it will perform this operation. Non-blocking algorithm are usually much faster than blocking algorithms as the synchronization of threads appears on a much lower level (hardware). The JDK itself extensively uses non-blocking algorithms to increase the platform performance. Developing correct non-blocking algorithm is not a trivial task.
  • 5. Java Concurrency - Lecture Notes |5 Liveness Issues Deadlock Deadlock describes a situation where two or more threads are blocked forever, waiting for each other. Livelock A thread often acts in response to the action of another thread. If the other thread's action is also a response to the action of another thread, then livelock may result. As with deadlock, livelocked threads are unable to make further progress. However, the threads are not blocked — they are simply too busy responding to each other to resume work. This is comparable to two people attempting to pass each other in a corridor: Alphonse moves to his left to let Gaston pass, while Gaston moves to his right to let Alphonse pass. Seeing that they are still blocking each other, Alphone moves to his right, while Gaston moves to his left. They're still blocking each other, so... Starvation Starvation describes a situation where a thread is unable to gain regular access to shared resources and is unable to make progress. This happens when shared resources are made unavailable for long periods by "greedy" threads. For example, suppose an object provides a synchronized method that often takes a long time to return. If one thread invokes this method frequently, other threads that also need frequent synchronized access to the same object will often be blocked. Guarded Blocks Threads often have to coordinate their actions. The most common coordination idiom is the guarded block. Such a block begins by polling a condition that must be true before the block can proceed. Immutable Objects An object is considered immutable if its state cannot change after it is constructed. Maximum reliance on immutable objects is widely accepted as a sound strategy for creating simple, reliable code. Immutable objects are particularly useful in concurrent applications. Since they cannot change state, they cannot be corrupted by thread interference or observed in an inconsistent state.
  • 6. Java Concurrency - Lecture Notes |6 High Level Concurrency Objects Synchronized blocs and monitors with java.lang.Thread class are adequate for very basic tasks, but higher-level building blocks are needed for more advanced tasks. This is especially true for massively concurrent applications that fully exploit today's multiprocessor and multi-core systems. In this section we'll look at some of the high-level concurrency features introduced with Java 5.0. Most of these features are implemented in the new java.util.concurrent packages. There are also new concurrent data structures in the Java Collections Framework. Lock objects Support locking idioms that simplify many concurrent applications. Executors Define a high-level API for launching and managing threads. Executor implementations provided by java.util.concurrent 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. Atomic variables Have features that minimize synchronization and help avoid memory consistency errors. ThreadLocalRandom Provides efficient generation of pseudorandom numbers from multiple threads (in JDK 7). Locks Lock implementations provide more extensive locking operations than can be obtained using synchronized methods and statements. Locks allow more flexible structuring, may have quite different properties, and may support multiple associated Condition objects. With this increased flexibility comes additional responsibility. The absence of block-structured locking removes the automatic release of locks that occurs with synchronized methods and statements. In most cases, the following idiom should be used: Lock l = ...; l.lock(); try { // access the resource protected by this lock } finally { l.unlock(); } Lock implementations provide additional functionality over the use of synchronized methods and statements. A Lock class can also provide behavior and semantics that is quite different from that of the implicit monitor lock, such as guaranteed ordering, non-reentrant usage, or deadlock detection. If an implementation provides such specialized semantics then the implementation must document those semantics.
  • 7. Java Concurrency - Lecture Notes |7 java.util.concurrent.locks Interface Lock Method Summary void lock() Acquires the lock (similar to entering the synchronized section). void lockInterruptibly() Acquires the lock unless the current thread is interrupted. Condition newCondition() Returns a new Condition instance that is bound to this Lock instance. boolean tryLock() Acquires the lock only if it is free at the time of invocation. boolean tryLock(long time, TimeUnit unit) Acquires the lock if it is free within the given waiting time and the current thread has not been interrupted. void unlock() Releases the lock. All Known Implementing Classes ReentrantLock, ReentrantReadWriteLock.ReadLock, ReentrantReadWriteLock.WriteLock 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. Executors Types Executor Interfaces Executor A simple interface that supports launching new tasks. (new Thread(r)).start(); → e.execute(r); ExecutorService A subinterface of Executor, which adds features that help manage the lifecycle, both of the individual tasks and of the executor itself. execute() accepts Runnable; submit() accepts Runnable and Callable. Returns a Future object, which is used to retrieve the Callable return value and to manage the status of both Callable and Runnable tasks. Provides methods for submitting large collections of Callable objects. Provides a number of methods for managing the shutdown of the executor. To support immediate shutdown, tasks should handle interrupts correctly. ScheduledExecutorService A subinterface of ExecutorService, supports future and/or periodic execution of tasks. The ScheduledExecutorService interface supplements the methods of its parent ExecutorService with schedule, which executes a Runnable or Callable task after a specified delay. In
  • 8. Java Concurrency - Lecture Notes |8 addition, the interface defines scheduleAtFixedRate and scheduleWithFixedDelay, which executes specified tasks repeatedly, at defined intervals. Thread Pools The most common kind of executor implementation. Fork/Join A framework (new in JDK 7) for taking advantage of multiple processors. Futures and Callables Callable<V> A task that returns a result and may throw an exception. method: Implementors define a single method with no arguments V call() throws Exception called call(). Computes a result, or throws an exception if unable to do so. The Callable interface is similar to Runnable, in that both are designed for classes whose instances are potentially executed by another thread. A Runnable, however, does not return a result and cannot throw a checked exception. The Executors class contains utility methods to convert from other common forms to Callable classes. java.lang.Thread VS the Executor Framework Using Threads directly has the following disadvantages:  Creating a new thread causes some performance overhead  Too many threads can lead to reduced performance, as the CPU needs to switch between these threads  You cannot easily control the number of threads, therefore you may run into out of memory errors due to too many threads The java.util.concurrent package offers improved support for concurrency compared to threads. java.lang.Thread Threads pools with the Executor Framework new Thread(new( RunnableTask() ).start() Executor executor = anExecutor; for each of a set of tasks executor.execute(new RunnableTask1()); executor.execute(new RunnableTask2()); Thread pool manages a pool of worker threads. The thread pool contains a work queue which holds tasks waiting to get executed. The Executor framework provides example implementation of the java.util.concurrent.Executor. The ExecutorService adds lifecycle methods to the Executor, which allows to shutdown the Executor and to wait for termination. Thread Pools Using worker threads minimizes the overhead due to thread creation. Thread objects use a significant amount of memory, and in a large-scale application, allocating and deallocating many thread objects creates a significant memory management overhead. One common type of thread pool is the fixed thread pool. This type of pool always has a specified number of threads running; if a thread is somehow terminated while it is still in use, it is automatically replaced with a new thread. Tasks are submitted to the pool via an internal queue, which holds extra tasks whenever there are more active tasks than threads.
  • 9. Java Concurrency - Lecture Notes |9 Executor An object that executes submitted Runnable tasks ExecutorService An Executor that provides methods to manage termination and methods that can produce a Future for tracking progress of one or more asynchronous tasks. All Known Implementing Classes: AbstractExecutorService, ScheduledThreadPoolExecutor, ThreadPoolExecutor The ThreadPoolExecutor class provides an extensible thread pool implementation. The Executors class provides convenient factory methods for these Executors. An important advantage of the fixed thread pool is that applications using it degrade gracefully. To understand this, consider a web server application where each HTTP request is handled by a separate thread. If the application simply creates a new thread for every new HTTP request, and the system receives more requests than it can handle immediately, the application will suddenly stop responding to all requests when the overhead of all those threads exceed the capacity of the system. With a limit on the number of the threads that can be created, the application will not be servicing HTTP requests as quickly as they come in, but it will be servicing them as quickly as the system can sustain. java.util.concurrent.Executors class Methods to create an executors that use threas pools newFixedThreadPool() Creates an executor with a fixed thread pool. newCachedThreadPool() Creates an executor with an expandable thread pool. This executor is suitable for applications that launch many short-lived tasks. newSingleThreadExecutor() Creates an executor that executes a single task at a time. If none of the executors provided by the above factory methods meet your needs, constructing instances of java.util.concurrent.ThreadPoolExecutor or java.util.concurrent.ScheduledThreadPoolExecutor will give you additional options. Fork/Join in Java 7 New in the Java SE 7 release, the fork/join framework is an implementation of the ExecutorService interface that helps you take advantage of multiple processors. The fork/join framework allows you to distribute a certain task on several workers and then wait for the result. It is designed for work that can be broken into smaller pieces recursively. The goal is to use all the available processing power to make your application wicked fast. For Java 6 you have to add this framework manually (jsr166.jar from http://g.oswego.edu/dl/concurrency- interest/).
  • 10. J a v a C o n c u r r e n c y - L e c t u r e N o t e s | 10 Concurrent Collections The java.util.concurrent package includes a number of additions to the Java Collections Framework. These are most easily categorized by the collection interfaces provided. BlockingQueue Defines a FIFO data structure that blocks or times out when you attempt to add to a full queue, or retrieve from an empty queue. ConcurrentMap The interface defines useful atomic operations. These operations remove or replace a key-value pair only if the key is present, or add a key-value pair only if the key is absent. Making these operations atomic helps avoid synchronization. ConcurrentNavigableMap This interface supports approximate matches. java.util.Map ConcurrentMap ConcurrentNavigableMap A concurrent analog of ConcurrentHashMap ConcurrentSkipListMap A concurrent analog of HashMap TreeMap
  • 11. J a v a C o n c u r r e n c y - L e c t u r e N o t e s | 11 References [1] Lars Vogel, Java Concurrency / Multithreading – Tutorial, http://www.vogella.de/articles/JavaConcurrency/article.html [2] Oracle, Inc, Java Tutorials – Concurrency, http://download.oracle.com/javase/tutorial/essential/concurrency/ [3] B. Goetz, with T. Peierls, J. Bloch, J. Bowbeer, D. Holmes, D. Lea, Java Concurrency in Practice, http://www.informit.com/store/product.aspx?isbn=0321349601
  • 12. J a v a C o n c u r r e n c y - L e c t u r e N o t e s | 12 About the Author Anton Shchastnyi is a software engineer living in Ukraine. He mainly focuses on developing web and desktop applications on Java platform. He also enjoys writing, and does quite a bit of tutorials and learning materials on Java and related technologies (http://antonshchastnyi.blogspot.com/, http://schaan.habrahabr.ru/blog).