<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>
Together	O
with	O
the	O
description	O
of	O
single-threaded	B-Operating_System
execution	O
of	O
code	O
,	O
the	O
memory	B-General_Concept
model	I-General_Concept
provides	O
the	O
semantics	B-Application
of	O
the	O
Java	B-Language
programming	I-Language
language	I-Language
.	O
</s>
<s>
The	O
original	O
Java	B-General_Concept
memory	I-General_Concept
model	I-General_Concept
developed	O
in	O
1995	O
,	O
was	O
widely	O
perceived	O
as	O
broken	O
,	O
preventing	O
many	O
runtime	O
optimizations	O
and	O
not	O
providing	O
strong	O
enough	O
guarantees	O
for	O
code	O
safety	O
.	O
</s>
<s>
It	O
was	O
updated	O
through	O
the	O
Java	B-Language
Community	I-Language
Process	I-Language
,	O
as	O
Java	B-Language
Specification	I-Language
Request	I-Language
133	O
(	O
JSR-133	O
)	O
,	O
which	O
took	O
effect	O
back	O
in	O
2004	O
,	O
for	O
Tiger	O
(	O
Java	B-Language
5.0	O
)	O
.	O
</s>
<s>
The	O
Java	B-Language
programming	I-Language
language	I-Language
and	O
platform	O
provide	O
thread	B-Operating_System
capabilities	O
.	O
</s>
<s>
Synchronization	O
between	O
threads	B-Operating_System
is	O
notoriously	O
difficult	O
for	O
developers	O
;	O
this	O
difficulty	O
is	O
compounded	O
because	O
Java	B-Language
applications	O
can	O
run	O
on	O
a	O
wide	O
range	O
of	O
processors	O
and	O
operating	B-General_Concept
systems	I-General_Concept
.	O
</s>
<s>
To	O
be	O
able	O
to	O
draw	O
conclusions	O
about	O
a	O
program	O
's	O
behavior	O
,	O
Java	B-Language
's	O
designers	O
decided	O
they	O
had	O
to	O
clearly	O
define	O
possible	O
behaviors	O
of	O
all	O
Java	B-Language
programs	O
.	O
</s>
<s>
On	O
multiprocessor	B-Operating_System
architectures	O
,	O
individual	O
processors	O
may	O
have	O
their	O
own	O
local	O
caches	O
that	O
are	O
out	O
of	O
sync	O
with	O
main	O
memory	O
.	O
</s>
<s>
It	O
is	O
generally	O
undesirable	O
to	O
require	O
threads	B-Operating_System
to	O
remain	O
perfectly	O
in	O
sync	O
with	O
one	O
another	O
because	O
this	O
would	O
be	O
too	O
costly	O
from	O
a	O
performance	O
point	O
of	O
view	O
.	O
</s>
<s>
This	O
means	O
that	O
at	O
any	O
given	O
time	O
,	O
different	O
threads	B-Operating_System
may	O
see	O
different	O
values	O
for	O
the	O
same	O
shared	O
data	O
.	O
</s>
<s>
In	O
a	O
single-threaded	B-Operating_System
environment	O
,	O
it	O
is	O
easy	O
to	O
reason	O
about	O
code	O
execution	O
.	O
</s>
<s>
The	O
typical	O
approach	O
requires	O
the	O
system	O
to	O
implement	O
as-if-serial	O
semantics	B-Application
for	O
individual	O
threads	B-Operating_System
in	O
isolation	O
.	O
</s>
<s>
When	O
an	O
individual	O
thread	B-Operating_System
executes	O
,	O
it	O
will	O
appear	O
as	O
if	O
all	O
of	O
the	O
actions	O
taken	O
by	O
that	O
thread	B-Operating_System
occur	O
in	O
the	O
order	O
they	O
appear	O
in	O
the	O
program	O
,	O
even	O
if	O
the	O
actions	O
themselves	O
occur	O
out	O
of	O
order	O
.	O
</s>
<s>
If	O
one	O
thread	B-Operating_System
executes	O
its	O
instructions	O
out	O
of	O
order	O
,	O
then	O
another	O
thread	B-Operating_System
might	O
see	O
the	O
fact	O
that	O
those	O
instructions	O
were	O
executed	O
out	O
of	O
order	O
,	O
even	O
if	O
that	O
did	O
not	O
affect	O
the	O
semantics	B-Application
of	O
the	O
first	O
thread	B-Operating_System
.	O
</s>
<s>
For	O
example	O
,	O
consider	O
two	O
threads	B-Operating_System
with	O
the	O
following	O
instructions	O
,	O
executing	O
concurrently	O
,	O
where	O
the	O
variables	O
x	O
and	O
y	O
are	O
both	O
initialized	O
to	O
0	O
:	O
</s>
<s>
Thread	B-Operating_System
1	O
Thread	B-Operating_System
2	O
x	O
=	O
1	O
;	O
int	O
r1	O
=	O
y	O
;	O
y	O
=	O
2	O
;	O
int	O
r2	O
=	O
x	O
;	O
</s>
<s>
The	O
Java	B-General_Concept
Memory	I-General_Concept
Model	I-General_Concept
(	O
JMM	O
)	O
defines	O
the	O
allowable	O
behavior	O
of	O
multithreaded	O
programs	O
,	O
and	O
therefore	O
describes	O
when	O
such	O
reorderings	O
are	O
possible	O
.	O
</s>
<s>
It	O
places	O
execution-time	O
constraints	O
on	O
the	O
relationship	O
between	O
threads	B-Operating_System
and	O
main	O
memory	O
in	O
order	O
to	O
achieve	O
consistent	O
and	O
reliable	O
Java	B-Language
applications	O
.	O
</s>
<s>
For	O
the	O
execution	O
of	O
a	O
single	B-Operating_System
thread	I-Operating_System
,	O
the	O
rules	O
are	O
simple	O
.	O
</s>
<s>
The	O
Java	B-Language
Language	I-Language
Specification	I-Language
requires	O
a	O
Java	B-Language
virtual	I-Language
machine	I-Language
to	O
observe	O
within-thread	O
as-if-serial	O
semantics	B-Application
.	O
</s>
<s>
The	O
runtime	O
(	O
which	O
,	O
in	O
this	O
case	O
,	O
usually	O
refers	O
to	O
the	O
dynamic	O
compiler	O
,	O
the	O
processor	O
and	O
the	O
memory	O
subsystem	O
)	O
is	O
free	O
to	O
introduce	O
any	O
useful	O
execution	O
optimizations	O
as	O
long	O
as	O
the	O
result	O
of	O
the	O
thread	B-Operating_System
in	O
isolation	O
is	O
guaranteed	O
to	O
be	O
exactly	O
the	O
same	O
as	O
it	O
would	O
have	O
been	O
had	O
all	O
the	O
statements	O
been	O
executed	O
in	O
the	O
order	O
the	O
statements	O
occurred	O
in	O
the	O
program	O
(	O
also	O
called	O
program	O
order	O
)	O
.	O
</s>
<s>
The	O
major	O
caveat	O
of	O
this	O
is	O
that	O
as-if-serial	O
semantics	B-Application
do	O
not	O
prevent	O
different	O
threads	B-Operating_System
from	O
having	O
different	O
views	O
of	O
the	O
data	O
.	O
</s>
<s>
The	O
memory	B-General_Concept
model	I-General_Concept
provides	O
clear	O
guidance	O
about	O
what	O
values	O
are	O
allowed	O
to	O
be	O
returned	O
when	O
the	O
data	O
is	O
read	O
.	O
</s>
<s>
The	O
basic	O
rules	O
imply	O
that	O
individual	O
actions	O
can	O
be	O
reordered	O
,	O
as	O
long	O
as	O
the	O
as-if-serial	O
semantics	B-Application
of	O
the	O
thread	B-Operating_System
are	O
not	O
violated	O
,	O
and	O
actions	O
that	O
imply	O
communication	O
between	O
threads	B-Operating_System
,	O
such	O
as	O
the	O
acquisition	O
or	O
release	O
of	O
a	O
lock	B-Operating_System
,	O
ensure	O
that	O
actions	O
that	O
happen	O
prior	O
to	O
them	O
are	O
seen	O
by	O
other	O
threads	B-Operating_System
that	O
see	O
their	O
effects	O
.	O
</s>
<s>
For	O
example	O
,	O
everything	O
that	O
happens	O
before	O
the	O
release	O
of	O
a	O
lock	B-Operating_System
will	O
be	O
seen	O
to	O
be	O
ordered	O
before	O
and	O
visible	O
to	O
everything	O
that	O
happens	O
after	O
a	O
subsequent	O
acquisition	O
of	O
that	O
same	O
lock	B-Operating_System
.	O
</s>
<s>
Mathematically	O
,	O
there	O
is	O
a	O
partial	O
order	O
called	O
the	O
happens-before	B-Operating_System
order	O
over	O
all	O
actions	O
performed	O
by	O
the	O
program	O
.	O
</s>
<s>
The	O
happens-before	B-Operating_System
order	O
subsumes	O
the	O
program	O
order	O
;	O
if	O
one	O
action	O
occurs	O
before	O
another	O
in	O
the	O
program	O
order	O
,	O
it	O
will	O
occur	O
before	O
the	O
other	O
in	O
the	O
happens-before	B-Operating_System
order	O
.	O
</s>
<s>
In	O
addition	O
,	O
releases	O
and	O
subsequent	O
acquisitions	O
of	O
locks	O
form	O
edges	O
in	O
the	O
happens-before	B-Operating_System
graph	O
.	O
</s>
<s>
A	O
read	O
is	O
allowed	O
to	O
return	O
the	O
value	O
of	O
a	O
write	O
if	O
that	O
write	O
is	O
the	O
last	O
write	O
to	O
that	O
variable	O
before	O
the	O
read	O
along	O
some	O
path	O
in	O
the	O
happens-before	B-Operating_System
order	O
,	O
or	O
if	O
the	O
write	O
is	O
not	O
ordered	O
with	O
respect	O
to	O
that	O
read	O
in	O
the	O
happens-before	B-Operating_System
order	O
.	O
</s>
<s>
The	O
Java	B-General_Concept
memory	I-General_Concept
model	I-General_Concept
was	O
the	O
first	O
attempt	O
to	O
provide	O
a	O
comprehensive	O
memory	B-General_Concept
model	I-General_Concept
for	O
a	O
popular	O
programming	O
language	O
.	O
</s>
<s>
It	O
was	O
justified	O
by	O
the	O
increasing	O
prevalence	O
of	O
concurrent	O
and	O
parallel	O
systems	O
,	O
and	O
the	O
need	O
to	O
provide	O
tools	O
and	O
technologies	O
with	O
clear	O
semantics	B-Application
for	O
such	O
systems	O
.	O
</s>
<s>
Since	O
then	O
,	O
the	O
need	O
for	O
a	O
memory	B-General_Concept
model	I-General_Concept
has	O
been	O
more	O
widely	O
accepted	O
,	O
with	O
similar	O
semantics	B-Application
being	O
provided	O
for	O
languages	O
such	O
as	O
C++	B-Language
.	O
</s>
