Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CS-206 Demos
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
LARA
CS-206 Demos
Commits
797a83b5
Commit
797a83b5
authored
1 year ago
by
Matt Bovel
Browse files
Options
Downloads
Patches
Plain Diff
Create atomicIncrement.worksheet.sc
parent
a53c3744
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#147872
passed
1 year ago
Stage: test
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/scala/ex05/atomicIncrement.worksheet.sc
+76
-0
76 additions, 0 deletions
src/main/scala/ex05/atomicIncrement.worksheet.sc
with
76 additions
and
0 deletions
src/main/scala/ex05/atomicIncrement.worksheet.sc
0 → 100644
+
76
−
0
View file @
797a83b5
import
java.util.concurrent.atomic.AtomicInteger
import
collection.parallel.CollectionConverters.
*
//
This
demo
should
help
understanding
the
`add`
method
implementation
in
//
src
/
main
/
scala
/
midterm22
/
Part8
.
scala
.
//
TL
;
DR
:
it
demonstrate
that
`+=`
is
not
atomic
.
var
id
:
Int
=
0
val
l
=
for
_
<-
(
0
until
10
).
par
yield
id
+=
1
id
l
l
.
distinct
.
size
//
Try
to
execute
the
worksheet
several
times
(
by
saving
again
and
again
).
We
//
might
get
a
number
of
distinct
elements
not
equal
to
10.
Why
?
//
//
First
,
note
that
`id += 1`
is
equivalent
to
`val tmp = id; id = tmp + 1`
.
//
I
.
e
.
the
read
and
write
are
two
separate
operations
;
the
`+=`
operation
is
//
not
*
atomic
*
.
//
//
Then
,
consider
the
following
execution
:
//
-
T1
:
reads
0
from
id
and
store
it
in
its
local
`tmp`
var
.
//
-
T2
:
does
the
same
:
reads
0
from
id
and
store
it
in
its
local
`tmp`
var
.
//
-
T1
:
computes
`tmp + 1 == 1`
,
stores
it
in
`id`
and
returns
`1`
//
-
T2
:
computes
`tmp + 1 == 1`
,
stores
it
in
`id`
and
returns
`1`
//
//
Now
we
have
two
times
the
ID
1
!
//
The
code
we
wrote
before
is
equivalent
to
:
var
id2
:
Int
=
0
val
l2
=
for
_
<-
(
0
until
10
).
par
yield
val
tmp
=
id2
id2
=
tmp
+
1
val
tmp2
=
id2
tmp2
l2
l2
.
distinct
.
size
//
*
//
How
to
make
it
correct
?
Use
an
atomic
integer
!
val
id3
=
AtomicInteger
()
val
l3
=
for
_
<-
(
0
until
10
).
par
yield
id3
.
incrementAndGet
()
l3
//
Now
we
are
sure
that
this
will
always
be
10.
l3
.
distinct
.
size
//
*
assert
(
l3
.
distinct
.
size
==
10
)
//
Or
,
using
only
what
has
been
seen
before
the
midterm
this
year
:
another
//
solution
is
to
use
synchronize
:
var
id4
:
Int
=
0
val
lock
=
Object
()
val
l4
=
for
_
<-
(
0
until
10
).
par
yield
lock
.
synchronized
{
id4
+=
1
id4
}
l4
//
Now
we
are
sure
that
this
will
always
be
10.
l4
.
distinct
.
size
//
*
assert
(
l4
.
distinct
.
size
==
10
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment