Difference between revisions of "Atomic Stack Assignment"
Jump to navigation
Jump to search
Line 1: | Line 1: | ||
=Motivation= | =Motivation= | ||
− | We will build | + | We will build a thread-safe implementation of a Stack using atomics. |
=Background= | =Background= | ||
− | |||
− | |||
− | |||
==Atomics== | ==Atomics== | ||
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicReference.html AtomicReference<V>] | [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicReference.html AtomicReference<V>] | ||
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicReference.html#get-- get()] | : [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicReference.html#get-- get()] | ||
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicReference.html#compareAndSet-V-V- compareAndSet(expect, update)] | : [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicReference.html#compareAndSet-V-V- compareAndSet(expect, update)] | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
=Code To Implement= | =Code To Implement= | ||
− | |||
− | |||
− | |||
− | + | ==AtomicStack== | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
While ConcurrentStack could simply rely on synchronized to provide thread-safety, AtomicStack must change its data structure. To be {{ThreadSafeLink}}, AtomicStack must correctly use [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicReference.html AtomicReference<V>]. | While ConcurrentStack could simply rely on synchronized to provide thread-safety, AtomicStack must change its data structure. To be {{ThreadSafeLink}}, AtomicStack must correctly use [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicReference.html AtomicReference<V>]. | ||
Do NOT hold any [https://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html intrinsic locks (via synchronized)]. It would be wasteful (and missing the point) to pay the lock overhead when the AtomicReference will get the job done. | Do NOT hold any [https://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html intrinsic locks (via synchronized)]. It would be wasteful (and missing the point) to pay the lock overhead when the AtomicReference will get the job done. | ||
− | {{CodeToImplement|AtomicStack|constructor | + | {{CodeToImplement|AtomicStack|constructor<br/>push<br/>peek<br/>pop|stack.atomic.exercise}} |
====constructor and instance variables==== | ====constructor and instance variables==== | ||
− | + | Be sure to initialize whatever state you need to implement a mutable thread-safe Stack using atomics. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | Be sure to | ||
− | |||
− | |||
− | |||
− | |||
====push==== | ====push==== | ||
====peek==== | ====peek==== | ||
====pop==== | ====pop==== | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
=Testing= | =Testing= | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
{{TestSuite|__AtomicStackTestSuite|stack.atomic.exercise}} | {{TestSuite|__AtomicStackTestSuite|stack.atomic.exercise}} | ||
− | |||
− | |||
− | |||
− | |||
− | |||
=Pledge, Acknowledgments, Citations= | =Pledge, Acknowledgments, Citations= | ||
{{Pledge|atomic-stack}} | {{Pledge|atomic-stack}} |
Revision as of 21:27, 1 February 2023
Contents
Motivation
We will build a thread-safe implementation of a Stack using atomics.
Background
Atomics
Code To Implement
AtomicStack
While ConcurrentStack could simply rely on synchronized to provide thread-safety, AtomicStack must change its data structure. To be @ThreadSafe, AtomicStack must correctly use AtomicReference<V>.
Do NOT hold any intrinsic locks (via synchronized). It would be wasteful (and missing the point) to pay the lock overhead when the AtomicReference will get the job done.
class: | AtomicStack.java | |
methods: | constructor push peek pop |
|
package: | stack.atomic.exercise | |
source folder: | student/src/main/java |
constructor and instance variables
Be sure to initialize whatever state you need to implement a mutable thread-safe Stack using atomics.
push
peek
pop
Testing
class: | __AtomicStackTestSuite.java | |
package: | stack.atomic.exercise | |
source folder: | testing/src/test/java |
Pledge, Acknowledgments, Citations
file: | atomic-stack-pledge-acknowledgments-citations.txt |
More info about the Honor Pledge