The
Java programming language
Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is a general-purpose programming language intended to let programmers ''write once, run an ...
and the
Java virtual machine
A Java virtual machine (JVM) is a virtual machine that enables a computer to run Java programs as well as programs written in other languages that are also compiled to Java bytecode. The JVM is detailed by a specification that formally describ ...
(JVM) have been designed to support
concurrent programming
Concurrent means happening at the same time. Concurrency, concurrent, or concurrence may refer to:
Law
* Concurrence, in jurisprudence, the need to prove both ''actus reus'' and ''mens rea''
* Concurring opinion (also called a "concurrence"), a ...
, and all execution takes place in the context of
threads
Thread may refer to:
Objects
* Thread (yarn), a kind of thin yarn used for sewing
** Thread (unit of measurement), a cotton yarn measure
* Screw thread, a helical ridge on a cylindrical fastener
Arts and entertainment
* ''Thread'' (film), 2016 ...
. Objects and resources can be accessed by many separate threads; each thread has its own path of execution but can potentially access any object in the program. The programmer must ensure read and write access to objects is properly coordinated (or "
synchronized") between threads. Thread synchronization ensures that objects are modified by only one thread at a time and that threads are prevented from accessing partially updated objects during modification by another thread. The Java language has built-in constructs to support this coordination.
Processes and threads
Most implementations of the
Java virtual machine
A Java virtual machine (JVM) is a virtual machine that enables a computer to run Java programs as well as programs written in other languages that are also compiled to Java bytecode. The JVM is detailed by a specification that formally describ ...
run as a single
process
A process is a series or set of activities that interact to produce a result; it may occur once-only or be recurrent or periodic.
Things called a process include:
Business and management
*Business process, activities that produce a specific se ...
and in the Java programming language,
concurrent programming
Concurrent means happening at the same time. Concurrency, concurrent, or concurrence may refer to:
Law
* Concurrence, in jurisprudence, the need to prove both ''actus reus'' and ''mens rea''
* Concurring opinion (also called a "concurrence"), a ...
is mostly concerned with
threads
Thread may refer to:
Objects
* Thread (yarn), a kind of thin yarn used for sewing
** Thread (unit of measurement), a cotton yarn measure
* Screw thread, a helical ridge on a cylindrical fastener
Arts and entertainment
* ''Thread'' (film), 2016 ...
(also called
lightweight processes). Multiple processes can only be realized with multiple JVMs.
Thread objects
Threads share the process's resources, including memory and open files. This makes for efficient, but potentially problematic, communication. Every application has at least one thread called the main thread. The main thread has the ability to create additional threads as
Runnable
or
Callable
objects. (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.)
Each thread can be scheduled on a different CPU core or use time-slicing on a single hardware processor, or time-slicing on many hardware processors. There is no generic solution to how Java threads are mapped to native OS threads. Every JVM implementation can do it in a different way.
Each thread is associated with an instance of the class Thread. Threads can be managed either directly using Thread objects or using abstract mechanisms such as
Executor
s and
java.util.concurrent
collections.
Starting a thread
Two ways to start a thread:
=Provide a runnable object
=
public class HelloRunnable implements Runnable
=Subclass thread
=
public class HelloThread extends Thread
Interrupts
An interrupt is an indication to a thread that it should stop what it is doing and do something else. A thread sends an interrupt by invoking interrupt on the Thread object for the thread to be interrupted. The interrupt mechanism is implemented using an internal flag known as the interrupt status. Invoking
Thread.interrupt
sets this flag. By convention, any method that exits by throwing an
InterruptedException
clears interrupt status when it does so. However, it's always possible that interrupt status will immediately be set again, by another thread invoking interrupt.
Joins
The
Thread.join
methods allow one thread to wait for the completion of another.
Exceptions
Uncaught exceptions thrown by code will terminate the thread. The
main
thread prints exceptions to the console, but user-created threads need a handler registered to do so.
Memory model
The
Java memory model describes how threads in the Java programming language interact through memory. On modern platforms, code is frequently not executed in the order it was written. It is reordered by the
compiler
In computing, a compiler is a computer program that translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs that ...
, the
processor and the
memory subsystem to achieve maximum performance. The Java programming language does not guarantee
linearizability
In concurrent programming, an operation (or set of operations) is linearizable if it consists of an ordered list of invocation and response events ( event), that may be extended by adding response events such that:
# The extended list can be re ...
, or even
sequential consistency
Sequential consistency is a consistency model used in the domain of concurrent computing
Concurrent computing is a form of computing in which several computations are executed '' concurrently''—during overlapping time periods—instead of ...
, when reading or writing fields of shared objects, and this is to allow for
compiler optimization
In computing, an optimizing compiler is a compiler that tries to minimize or maximize some attributes of an executable computer program. Common requirements are to minimize a program's execution time, memory footprint, storage size, and power c ...
s (such as
register allocation
In compiler optimization, register allocation is the process of assigning local automatic variables and expression results to a limited number of processor registers.
Register allocation can happen over a basic block (''local register allocat ...
,
common subexpression elimination In compiler theory, common subexpression elimination (CSE) is a compiler optimization that searches for instances of identical expressions (i.e., they all evaluate to the same value), and analyzes whether it is worthwhile replacing them with a sin ...
, and
redundant read elimination) all of which work by reordering memory reads—writes.
Synchronization
Threads communicate primarily by sharing access to fields and the objects that 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.
''Reorderings'' can come into play in incorrectly
synchronized multithreaded programs, where one thread is able to observe the effects of other threads, and may be able to detect that variable accesses become visible to other threads in a different order than executed or specified in the program.
Most of the time, one thread doesn't care what the other is doing. But when it does, that's what synchronization is for.
To synchronize threads, Java uses
monitors, which are a high-level mechanism for allowing only one thread at a time to execute a region of code protected by the monitor. The behavior of monitors is explained in terms of
locks; there is a lock associated with each object.
Synchronization has several aspects. The most well-understood is
mutual exclusion
In computer science, mutual exclusion is a property of concurrency control, which is instituted for the purpose of preventing race conditions. It is the requirement that one thread of execution never enters a critical section while a concurrent ...
—only one thread can hold a monitor at once, so synchronizing on a monitor means that once one thread enters a synchronized block protected by a monitor, no other thread can enter a block protected by that monitor until the first thread exits the synchronized block.
But there is more to synchronization than mutual exclusion. Synchronization ensures that memory writes by a thread before or during a synchronized block are made visible in a predictable manner to other threads which synchronize on the same monitor. After we exit a synchronized block, we release the monitor, which has the effect of flushing the cache to main memory, so that writes made by this thread can be visible to other threads. Before we can enter a synchronized block, we acquire the monitor, which has the effect of invalidating the local processor cache so that variables will be reloaded from main memory. We will then be able to see all of the writes made visible by the previous release.
Reads—writes to fields are
linearizable if either the field is
volatile, or the field is protected by a unique
lock
Lock(s) may refer to:
Common meanings
*Lock and key, a mechanical device used to secure items of importance
*Lock (water navigation), a device for boats to transit between different levels of water, as in a canal
Arts and entertainment
* ''Lock ...
which is acquired by all readers and writers.
Locks and synchronized blocks
A thread can achieve mutual exclusion either by entering a synchronized block or method, which acquires an implicit lock, or by acquiring an explicit lock (such as the ReentrantLock from the java.util.concurrent.locks package). Both approaches have the same implications for memory behavior. If all accesses to a particular field are protected by the same lock, then reads—writes to that field are
linearizable (atomic).
Volatile fields
When applied to a field, the Java
volatile
guarantees that:
# ''(In all versions of Java)'' There is a global ordering on the reads and writes to a volatile variable. This implies that every
thread
Thread may refer to:
Objects
* Thread (yarn), a kind of thin yarn used for sewing
** Thread (unit of measurement), a cotton yarn measure
* Screw thread, a helical ridge on a cylindrical fastener
Arts and entertainment
* ''Thread'' (film), 2016 ...
accessing a volatile field will read its current value before continuing, instead of (potentially) using a cached value. (However, there is no guarantee about the relative ordering of volatile reads and writes with regular reads and writes, meaning that it's generally not a useful threading construct.)
# ''(In Java 5 or later)'' Volatile reads and writes establish a
happens-before relationship, much like acquiring and releasing a mutex.
[
Section 17.4.4: Synchronization Order
] This relationship is simply a guarantee that memory writes by one specific statement are visible to another specific statement.
Volatile fields are linearizable. Reading a volatile field is like acquiring a lock: the working memory is invalidated and the volatile field's current value is reread from memory. Writing a volatile field is like releasing a lock: the volatile field is immediately written back to memory.
Final fields
A field declared to be final cannot be modified once it has been initialized. An object's final fields are initialized in its constructor. If the constructor follows certain simple rules, then the correct value of any final fields will be visible to other threads without synchronization. The rule is simple: the
this
reference must not be released from the constructor before the constructor returns.
History
Since
JDK 1.2, Java has included a standard set of collection classes, the
Java collections framework
Doug Lea, who also participated in the Java collections framework implementation, developed a concurrency
package, comprising several concurrency primitives and a large battery of collection-related classes.
This work was continued and updated as part of
JSR 166 which was chaired by Doug Lea.
JDK 5.0 incorporated many additions and clarifications to the Java concurrency model. The concurrency APIs developed by JSR 166 were also included as part of the JDK for the first time.
JSR 133 provided support for well-defined atomic operations in a multithreaded/multiprocessor environment.
Both the
Java SE 6
The Java language has undergone several changes since JDK 1.0 as well as numerous additions of classes and packages to the standard library. Since J2SE 1.4, the evolution of the Java language has been governed by the Java Communit ...
and
Java SE 7
The Java language has undergone several changes since JDK 1.0 as well as numerous additions of classes and packages to the standard library. Since J2SE 1.4, the evolution of the Java language has been governed by the Java Community ...
releases introduced updated versions of the JSR 166 APIs as well as several new additional APIs.
See also
*
Concurrency (computer science)
In computer science, concurrency is the ability of different parts or units of a program, algorithm, or problem to be executed out-of-order or in partial order, without affecting the outcome. This allows for parallel execution of the concur ...
*
Concurrency pattern In software engineering, concurrency patterns are those types of design patterns that deal with the multi-threaded programming paradigm.
Examples of this class of patterns include:
* Active Object
* Balking pattern
* Barrier
* Double-checked l ...
*
Fork–join model
*
Memory barrier
In computing, a memory barrier, also known as a membar, memory fence or fence instruction, is a type of barrier instruction that causes a central processing unit (CPU) or compiler to enforce an ordering constraint on memory operations issued ...
*
Memory models
*
Thread safety Thread safety is a computer programming concept applicable to multi-threaded code. Thread-safe code only manipulates shared data structures in a manner that ensures that all threads behave properly and fulfill their design specifications without un ...
*
ThreadSafe
*
Java ConcurrentMap
Notes
References
*
*{{cite book
, first = Doug
, last = Lea
, year = 1999
, title = Concurrent Programming in Java: Design Principles and Patterns
, publisher = Addison Wesley
, isbn = 0-201-31009-0
, url = https://archive.org/details/concurrentprogra00lead_0
External links
Oracle's Java concurrency tutorialWilliam Pugh's Java memory model pageJava Concurrency Animations by Victor GraziThread safety checker for Java classes
Concurrent computing
Java (programming language)
Sun Microsystems