<s>
The	O
Java	B-Language
programming	I-Language
language	I-Language
and	O
the	O
Java	B-Language
virtual	I-Language
machine	I-Language
(	O
JVM	B-Language
)	O
have	O
been	O
designed	O
to	O
support	O
concurrent	B-Architecture
programming	I-Architecture
,	O
and	O
all	O
execution	O
takes	O
place	O
in	O
the	O
context	O
of	O
threads	B-Operating_System
.	O
</s>
<s>
Objects	O
and	O
resources	O
can	O
be	O
accessed	O
by	O
many	O
separate	O
threads	B-Operating_System
;	O
each	O
thread	B-Operating_System
has	O
its	O
own	O
path	O
of	O
execution	O
but	O
can	O
potentially	O
access	O
any	O
object	O
in	O
the	O
program	O
.	O
</s>
<s>
The	O
programmer	O
must	O
ensure	O
read	O
and	O
write	O
access	O
to	O
objects	O
is	O
properly	O
coordinated	O
(	O
or	O
"	O
synchronized	O
"	O
)	O
between	O
threads	B-Operating_System
.	O
</s>
<s>
Thread	B-Operating_System
synchronization	O
ensures	O
that	O
objects	O
are	O
modified	O
by	O
only	O
one	O
thread	B-Operating_System
at	O
a	O
time	O
and	O
that	O
threads	B-Operating_System
are	O
prevented	O
from	O
accessing	O
partially	O
updated	O
objects	O
during	O
modification	O
by	O
another	O
thread	B-Operating_System
.	O
</s>
<s>
The	O
Java	B-Language
language	I-Language
has	O
built-in	O
constructs	O
to	O
support	O
this	O
coordination	O
.	O
</s>
<s>
Most	O
implementations	O
of	O
the	O
Java	B-Language
virtual	I-Language
machine	I-Language
run	O
as	O
a	O
single	O
process	B-Operating_System
and	O
in	O
the	O
Java	B-Language
programming	I-Language
language	I-Language
,	O
concurrent	B-Architecture
programming	I-Architecture
is	O
mostly	O
concerned	O
with	O
threads	B-Operating_System
(	O
also	O
called	O
lightweight	B-Operating_System
processes	I-Operating_System
)	O
.	O
</s>
<s>
Multiple	O
processes	O
can	O
only	O
be	O
realized	O
with	O
multiple	O
JVMs	B-Language
.	O
</s>
<s>
Threads	B-Operating_System
share	O
the	O
process	B-Operating_System
's	O
resources	O
,	O
including	O
memory	O
and	O
open	O
files	O
.	O
</s>
<s>
Every	O
application	O
has	O
at	O
least	O
one	O
thread	B-Operating_System
called	O
the	O
main	O
thread	B-Operating_System
.	O
</s>
<s>
The	O
main	O
thread	B-Operating_System
has	O
the	O
ability	O
to	O
create	O
additional	O
threads	B-Operating_System
as	O
Runnable	O
or	O
Callable	O
objects	O
.	O
</s>
<s>
(	O
The	O
Callable	O
interface	O
is	O
similar	O
to	O
Runnable	O
,	O
in	O
that	O
both	O
are	O
designed	O
for	O
classes	O
whose	O
instances	O
are	O
potentially	O
executed	O
by	O
another	O
thread	B-Operating_System
.	O
</s>
<s>
Each	O
thread	B-Operating_System
can	O
be	O
scheduled	O
on	O
a	O
different	O
CPU	O
core	O
or	O
use	O
time-slicing	O
on	O
a	O
single	O
hardware	O
processor	B-General_Concept
,	O
or	O
time-slicing	O
on	O
many	O
hardware	O
processors	O
.	O
</s>
<s>
There	O
is	O
no	O
generic	O
solution	O
to	O
how	O
Java	B-Language
threads	B-Operating_System
are	O
mapped	O
to	O
native	O
OS	O
threads	B-Operating_System
.	O
</s>
<s>
Every	O
JVM	B-Language
implementation	O
can	O
do	O
it	O
in	O
a	O
different	O
way	O
.	O
</s>
<s>
Each	O
thread	B-Operating_System
is	O
associated	O
with	O
an	O
instance	O
of	O
the	O
class	O
Thread	B-Operating_System
.	O
</s>
<s>
Threads	B-Operating_System
can	O
be	O
managed	O
either	O
directly	O
using	O
Thread	B-Operating_System
objects	O
or	O
using	O
abstract	O
mechanisms	O
such	O
as	O
Executors	O
and	O
java.util.concurrent	O
collections	O
.	O
</s>
<s>
Two	O
ways	O
to	O
start	O
a	O
thread	B-Operating_System
:	O
</s>
<s>
An	O
interrupt	O
is	O
an	O
indication	O
to	O
a	O
thread	B-Operating_System
that	O
it	O
should	O
stop	O
what	O
it	O
is	O
doing	O
and	O
do	O
something	O
else	O
.	O
</s>
<s>
A	O
thread	B-Operating_System
sends	O
an	O
interrupt	O
by	O
invoking	O
interrupt	O
on	O
the	O
Thread	B-Operating_System
object	O
for	O
the	O
thread	B-Operating_System
to	O
be	O
interrupted	O
.	O
</s>
<s>
Invoking	O
Thread.interrupt	O
sets	O
this	O
flag	O
.	O
</s>
<s>
However	O
,	O
it	O
's	O
always	O
possible	O
that	O
interrupt	O
status	O
will	O
immediately	O
be	O
set	O
again	O
,	O
by	O
another	O
thread	B-Operating_System
invoking	O
interrupt	O
.	O
</s>
<s>
The	O
Thread.join	O
methods	O
allow	O
one	O
thread	B-Operating_System
to	O
wait	O
for	O
the	O
completion	O
of	O
another	O
.	O
</s>
<s>
Uncaught	O
exceptions	O
thrown	O
by	O
code	O
will	O
terminate	O
the	O
thread	B-Operating_System
.	O
</s>
<s>
The	O
main	O
thread	B-Operating_System
prints	O
exceptions	O
to	O
the	O
console	O
,	O
but	O
user-created	O
threads	B-Operating_System
need	O
a	O
handler	O
registered	O
to	O
do	O
so	O
.	O
</s>
<s>
The	O
Java	B-General_Concept
memory	I-General_Concept
model	I-General_Concept
describes	O
how	O
threads	B-Operating_System
in	O
the	O
Java	B-Language
programming	I-Language
language	I-Language
interact	O
through	O
memory	O
.	O
</s>
<s>
It	O
is	O
reordered	O
by	O
the	O
compiler	B-Language
,	O
the	O
processor	B-General_Concept
and	O
the	O
memory	B-General_Concept
subsystem	I-General_Concept
to	O
achieve	O
maximum	O
performance	O
.	O
</s>
<s>
The	O
Java	B-Language
programming	I-Language
language	I-Language
does	O
not	O
guarantee	O
linearizability	B-General_Concept
,	O
or	O
even	O
sequential	B-General_Concept
consistency	I-General_Concept
,	O
when	O
reading	O
or	O
writing	O
fields	O
of	O
shared	O
objects	O
,	O
and	O
this	O
is	O
to	O
allow	O
for	O
compiler	B-Application
optimizations	I-Application
(	O
such	O
as	O
register	O
allocation	O
,	O
common	O
subexpression	O
elimination	O
,	O
and	O
redundant	O
read	O
elimination	O
)	O
all	O
of	O
which	O
work	O
by	O
reordering	O
memory	O
reads	O
—	O
writes	O
.	O
</s>
<s>
Threads	B-Operating_System
communicate	O
primarily	O
by	O
sharing	O
access	O
to	O
fields	O
and	O
the	O
objects	O
that	O
reference	O
fields	O
refer	O
to	O
.	O
</s>
<s>
This	O
form	O
of	O
communication	O
is	O
extremely	O
efficient	O
,	O
but	O
makes	O
two	O
kinds	O
of	O
errors	O
possible	O
:	O
thread	B-Operating_System
interference	O
and	O
memory	O
consistency	O
errors	O
.	O
</s>
<s>
Reorderings	O
can	O
come	O
into	O
play	O
in	O
incorrectly	O
synchronized	O
multithreaded	O
programs	O
,	O
where	O
one	O
thread	B-Operating_System
is	O
able	O
to	O
observe	O
the	O
effects	O
of	O
other	O
threads	B-Operating_System
,	O
and	O
may	O
be	O
able	O
to	O
detect	O
that	O
variable	O
accesses	O
become	O
visible	O
to	O
other	O
threads	B-Operating_System
in	O
a	O
different	O
order	O
than	O
executed	O
or	O
specified	O
in	O
the	O
program	O
.	O
</s>
<s>
Most	O
of	O
the	O
time	O
,	O
one	O
thread	B-Operating_System
does	O
n't	O
care	O
what	O
the	O
other	O
is	O
doing	O
.	O
</s>
<s>
To	O
synchronize	O
threads	B-Operating_System
,	O
Java	B-Language
uses	O
monitors	O
,	O
which	O
are	O
a	O
high-level	O
mechanism	O
for	O
allowing	O
only	O
one	O
thread	B-Operating_System
at	O
a	O
time	O
to	O
execute	O
a	O
region	O
of	O
code	O
protected	O
by	O
the	O
monitor	O
.	O
</s>
<s>
The	O
behavior	O
of	O
monitors	O
is	O
explained	O
in	O
terms	O
of	O
locks	B-Operating_System
;	O
there	O
is	O
a	O
lock	B-Operating_System
associated	O
with	O
each	O
object	O
.	O
</s>
<s>
The	O
most	O
well-understood	O
is	O
mutual	B-Operating_System
exclusion	I-Operating_System
—	O
only	O
one	O
thread	B-Operating_System
can	O
hold	O
a	O
monitor	O
at	O
once	O
,	O
so	O
synchronizing	O
on	O
a	O
monitor	O
means	O
that	O
once	O
one	O
thread	B-Operating_System
enters	O
a	O
synchronized	O
block	O
protected	O
by	O
a	O
monitor	O
,	O
no	O
other	O
thread	B-Operating_System
can	O
enter	O
a	O
block	O
protected	O
by	O
that	O
monitor	O
until	O
the	O
first	O
thread	B-Operating_System
exits	O
the	O
synchronized	O
block	O
.	O
</s>
<s>
But	O
there	O
is	O
more	O
to	O
synchronization	O
than	O
mutual	B-Operating_System
exclusion	I-Operating_System
.	O
</s>
<s>
Synchronization	O
ensures	O
that	O
memory	O
writes	O
by	O
a	O
thread	B-Operating_System
before	O
or	O
during	O
a	O
synchronized	O
block	O
are	O
made	O
visible	O
in	O
a	O
predictable	O
manner	O
to	O
other	O
threads	B-Operating_System
which	O
synchronize	O
on	O
the	O
same	O
monitor	O
.	O
</s>
<s>
After	O
we	O
exit	O
a	O
synchronized	O
block	O
,	O
we	O
release	O
the	O
monitor	O
,	O
which	O
has	O
the	O
effect	O
of	O
flushing	O
the	O
cache	O
to	O
main	O
memory	O
,	O
so	O
that	O
writes	O
made	O
by	O
this	O
thread	B-Operating_System
can	O
be	O
visible	O
to	O
other	O
threads	B-Operating_System
.	O
</s>
<s>
Before	O
we	O
can	O
enter	O
a	O
synchronized	O
block	O
,	O
we	O
acquire	O
the	O
monitor	O
,	O
which	O
has	O
the	O
effect	O
of	O
invalidating	O
the	O
local	O
processor	B-General_Concept
cache	O
so	O
that	O
variables	O
will	O
be	O
reloaded	O
from	O
main	O
memory	O
.	O
</s>
<s>
Reads	O
—	O
writes	O
to	O
fields	O
are	O
linearizable	B-General_Concept
if	O
either	O
the	O
field	O
is	O
volatile	B-Operating_System
,	O
or	O
the	O
field	O
is	O
protected	O
by	O
a	O
unique	O
lock	B-Operating_System
which	O
is	O
acquired	O
by	O
all	O
readers	O
and	O
writers	O
.	O
</s>
<s>
A	O
thread	B-Operating_System
can	O
achieve	O
mutual	B-Operating_System
exclusion	I-Operating_System
either	O
by	O
entering	O
a	O
synchronized	O
block	O
or	O
method	O
,	O
which	O
acquires	O
an	O
implicit	O
lock	B-Operating_System
,	O
or	O
by	O
acquiring	O
an	O
explicit	O
lock	B-Operating_System
(	O
such	O
as	O
the	O
ReentrantLock	O
from	O
the	O
java.util.concurrent.locks	O
package	B-Language
)	O
.	O
</s>
<s>
If	O
all	O
accesses	O
to	O
a	O
particular	O
field	O
are	O
protected	O
by	O
the	O
same	O
lock	B-Operating_System
,	O
then	O
reads	O
—	O
writes	O
to	O
that	O
field	O
are	O
linearizable	B-General_Concept
(	O
atomic	O
)	O
.	O
</s>
<s>
When	O
applied	O
to	O
a	O
field	O
,	O
the	O
Java	B-Language
volatile	B-Operating_System
guarantees	O
that	O
:	O
</s>
<s>
(	O
In	O
all	O
versions	O
of	O
Java	B-Language
)	O
There	O
is	O
a	O
global	O
ordering	O
on	O
the	O
reads	O
and	O
writes	O
to	O
a	O
volatile	B-Operating_System
variable	I-Operating_System
.	O
</s>
<s>
This	O
implies	O
that	O
every	O
thread	B-Operating_System
accessing	O
a	O
volatile	B-Operating_System
field	O
will	O
read	O
its	O
current	O
value	O
before	O
continuing	O
,	O
instead	O
of	O
(	O
potentially	O
)	O
using	O
a	O
cached	O
value	O
.	O
</s>
<s>
(	O
However	O
,	O
there	O
is	O
no	O
guarantee	O
about	O
the	O
relative	O
ordering	O
of	O
volatile	B-Operating_System
reads	O
and	O
writes	O
with	O
regular	O
reads	O
and	O
writes	O
,	O
meaning	O
that	O
it	O
's	O
generally	O
not	O
a	O
useful	O
threading	O
construct	O
.	O
)	O
</s>
<s>
(	O
In	O
Java	B-Language
5	O
or	O
later	O
)	O
Volatile	B-Operating_System
reads	O
and	O
writes	O
establish	O
a	O
happens-before	B-Operating_System
relationship	I-Operating_System
,	O
much	O
like	O
acquiring	O
and	O
releasing	O
a	O
mutex	B-Operating_System
.	O
</s>
<s>
Volatile	B-Operating_System
fields	O
are	O
linearizable	B-General_Concept
.	O
</s>
<s>
Reading	O
a	O
volatile	B-Operating_System
field	O
is	O
like	O
acquiring	O
a	O
lock	B-Operating_System
:	O
the	O
working	O
memory	O
is	O
invalidated	O
and	O
the	O
volatile	B-Operating_System
field	O
's	O
current	O
value	O
is	O
reread	O
from	O
memory	O
.	O
</s>
<s>
Writing	O
a	O
volatile	B-Operating_System
field	O
is	O
like	O
releasing	O
a	O
lock	B-Operating_System
:	O
the	O
volatile	B-Operating_System
field	O
is	O
immediately	O
written	O
back	O
to	O
memory	O
.	O
</s>
<s>
If	O
the	O
constructor	O
follows	O
certain	O
simple	O
rules	O
,	O
then	O
the	O
correct	O
value	O
of	O
any	O
final	O
fields	O
will	O
be	O
visible	O
to	O
other	O
threads	B-Operating_System
without	O
synchronization	O
.	O
</s>
<s>
Doug	O
Lea	O
,	O
who	O
also	O
participated	O
in	O
the	O
Java	B-Language
collections	I-Language
framework	I-Language
implementation	O
,	O
developed	O
a	O
concurrency	B-Operating_System
package	B-Language
,	O
comprising	O
several	O
concurrency	B-Operating_System
primitives	O
and	O
a	O
large	O
battery	O
of	O
collection-related	O
classes	O
.	O
</s>
<s>
This	O
work	O
was	O
continued	O
and	O
updated	O
as	O
part	O
of	O
JSR	B-Language
166	I-Language
which	O
was	O
chaired	O
by	O
Doug	O
Lea	O
.	O
</s>
<s>
JDK	O
5.0	O
incorporated	O
many	O
additions	O
and	O
clarifications	O
to	O
the	O
Java	B-Language
concurrency	I-Language
model	O
.	O
</s>
<s>
The	O
concurrency	B-Operating_System
APIs	O
developed	O
by	O
JSR	B-Language
166	I-Language
were	O
also	O
included	O
as	O
part	O
of	O
the	O
JDK	O
for	O
the	O
first	O
time	O
.	O
</s>
<s>
JSR	B-General_Concept
133	I-General_Concept
provided	O
support	O
for	O
well-defined	O
atomic	B-General_Concept
operations	I-General_Concept
in	O
a	O
multithreaded/multiprocessor	O
environment	O
.	O
</s>
<s>
Both	O
the	O
Java	B-Language
SE	O
6	O
and	O
Java	B-Language
SE	O
7	O
releases	O
introduced	O
updated	O
versions	O
of	O
the	O
JSR	B-Language
166	I-Language
APIs	O
as	O
well	O
as	O
several	O
new	O
additional	O
APIs	O
.	O
</s>
