Difference between revisions of "Stack Assignment"
(→Node) |
|||
Line 50: | Line 50: | ||
We will build an [https://www.javadoc.io/static/com.google.code.findbugs/jsr305/3.0.1/javax/annotation/concurrent/Immutable.html @Immutable] class Node. | We will build an [https://www.javadoc.io/static/com.google.code.findbugs/jsr305/3.0.1/javax/annotation/concurrent/Immutable.html @Immutable] class Node. | ||
− | {{CodeToImplement| | + | {{CodeToImplement|Node|value<br/>nextNode|stack.node.exercise|main}} |
====constructor and instance variables==== | ====constructor and instance variables==== | ||
Line 56: | Line 56: | ||
The constructor | The constructor | ||
− | <nowiki>public | + | <nowiki>public Node(E value, Optional<Node<E>> nextNode)</nowiki> |
is passed a value and a nextNode. Hang on to this data in instance variables. As instances of DefaultNode are to be immutable, the instance variables should be marked as <code>final</code>. | is passed a value and a nextNode. Hang on to this data in instance variables. As instances of DefaultNode are to be immutable, the instance variables should be marked as <code>final</code>. |
Revision as of 21:29, 1 February 2023
Contents
Motivation
We will build an initial not-thread-safe implementation of a Stack. This exercise will allow us to gain experience building a data structure and see how it performs when used in a parallel for which it was not designed. Later in the semester we will build two thread-safe implementations: one concurrent and one atomic. Your work on this exercise will pay dividends on these later exercises.
Background
interface Stack
public interface Stack<E> { void push(E value); Optional<E> peek(); Optional<E> pop(); }
Example
empty
Stack<String> stack = new NotThreadSafeStack<>();
In this state, stack.peek()
will return Optional.empty()
and stack.pop()
will also return Optional.empty()
.
push A, B, C, D, E
Stack<String> stack = new NotThreadSafeStack<>(); stack.push("A"); stack.push("B"); stack.push("C"); stack.push("D"); stack.push("E");
In this state, stack.peek()
will return Optional.of("E")
.
push A, B, C, D, E, pop
Stack<String> stack = new NotThreadSafeStack<>(); stack.push("A"); stack.push("B"); stack.push("C"); stack.push("D"); stack.push("E"); Optional<String> optOfE = stack.pop();
In this state, optOfE
will hold Optional.of("E")
and stack.peek()
will return Optional.of("D")
.
Code To Implement
Node
The mighty cons cell goes back to the early days of computing. People have been building amazing systems with this data structure since the 1950s.
We will build an @Immutable class Node.
class: | Node.java | |
methods: | value nextNode |
|
package: | stack.node.exercise | |
source folder: | main/src/main/java |
constructor and instance variables
The constructor
public Node(E value, Optional<Node<E>> nextNode)
is passed a value and a nextNode. Hang on to this data in instance variables. As instances of DefaultNode are to be immutable, the instance variables should be marked as final
.
value
return the value.
nextNode
return the next node.
Stack
public interface Stack<E> { void push(E value); Optional<E> peek(); Optional<E> pop(); }
NotThreadSafeStack
class: | NotThreadSafeStack.java | |
methods: | push peek pop |
|
package: | stack.notthreadsafe.exercise | |
source folder: | student/src/main/java |
instance variables
What state do you need to keep track of to support a mutable non-thread-safe Stack?
push
peek
pop
Distasters To Investigate
ParallelPushStackDisasterClient
class: | ParallelPushStackDisasterClient.java | DEMO: |
methods: | main | |
package: | stack.notthreadsafe.disaster | |
source folder: | src/main/java |
ParallelPushAndPopStackDisasterClient
class: | ParallelPushAndPopStackDisasterClient.java | DEMO: |
methods: | main | |
package: | stack.notthreadsafe.disaster | |
source folder: | src/main/java |
Testing
class: | _NotThreadSafeStackTestSuite.java | |
package: | stack.notthreadsafe.exercise | |
source folder: | testing/src/test/java |
Pledge, Acknowledgments, Citations
file: | atomic-stack-pledge-acknowledgments-citations.txt |
More info about the Honor Pledge