Difference between revisions of "Concurrent Stack Assignment"

From CSE231 Wiki
Jump to navigation Jump to search
(Created page with "=Motivation= We will build a thread-safe implementation of a Stack using synchronized methods. =Background= ==Implicit Locks== [https://docs.oracle.com/javase/tutorial/essent...")
 
Line 10: Line 10:
 
To be {{ThreadSafeLink}}, one must hold [https://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html intrinsic lock (via synchronized)] on the ConcurrentStack instance for each of the methods which read and/or write to mutable data.
 
To be {{ThreadSafeLink}}, one must hold [https://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html intrinsic lock (via synchronized)] on the ConcurrentStack instance for each of the methods which read and/or write to mutable data.
  
{{CodeToImplement|ConcurrentStack|constructor<br/>nodeConstructor<br/>push<br/>peek<br/>pop|stack.concurrent.exercise}}
+
{{CodeToImplement|ConcurrentStack|constructor<br/>push<br/>peek<br/>pop|stack.concurrent.exercise}}
 
===constructor and instance variables===
 
===constructor and instance variables===
 
The constructor
 
The constructor

Revision as of 21:23, 1 February 2023

Motivation

We will build a thread-safe implementation of a Stack using synchronized methods.

Background

Implicit Locks

synchronized methods

Code To Implement

ConcurrentStack

To be @ThreadSafe, one must hold intrinsic lock (via synchronized) on the ConcurrentStack instance for each of the methods which read and/or write to mutable data.

class: ConcurrentStack.java Java.png
methods: constructor
push
peek
pop
package: stack.concurrent.exercise
source folder: student/src/main/java

constructor and instance variables

The constructor

public ConcurrentStack(BiFunction<E, Optional<Node<E>>, Node<E>> nodeCreator)

is passed a nodeCreator. The excessive over use of abstraction is in place to help the testing alert you to potential bugs in your code earlier.

Be sure to hang onto the nodeCreator in a final instance variable along with whatever other state you need to implement a mutable thread-safe Stack.

push

peek

pop

Testing

class: __ConcurrentStackTestSuite.java Junit.png
package: stack.concurrent.exercise
source folder: testing/src/test/java

Pledge, Acknowledgments, Citations

file: concurrent-stack-pledge-acknowledgments-citations.txt

More info about the Honor Pledge