Stack Assignment
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
| Video: HackerRank: Stacks and Queues |
|---|
interface Stack
public interface Stack<E> {
void push(E value);
E peek();
E pop();
}
Example
empty
Stack<String> stack = new NotThreadSafeStack<>();
In this state, stack.peek() will return null() and stack.pop() will also return null.
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 "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");
String e = stack.pop();
In this state, e will hold "E" and stack.peek() will return "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 next |
|
| package: | stack.node.exercise | |
| source folder: | student-pre-aspect/src/main/java |
constructor and instance variables
The constructor
public Node(E value, Node<E> next)
is passed a value and a next. Hang on to this data in instance variables. As instances of Node are to be immutable, the instance variables should be marked as final which prevents them from being re-assigned.
value
return the value.
next
return the next node.
NotThreadSafeStack
@NotThreadSafe
public class NotThreadSafeStack<E> implements Stack<E>
| class: | NotThreadSafeStack.java | |
| methods: | push peek pop |
|
| package: | stack.notthreadsafe.exercise | |
| source folder: | student/src/main/java |
Note: The constructor has been intentionally omitted. Simply initialize the instance variable you need when you declare it.
instance variables
What state do you need to keep track of to support a mutable non-thread-safe Stack?
push
public void push(E value)
peek
public E peek()
If the current instance is empty, null should be returned. Otherwise, it should return the value on top of the stack. The current instance should NOT be mutated.
pop
public E pop()
If the current instance is empty, null should be returned. Otherwise, it should remove the top of the stack and return it. NOTE: if not empty, the current instance should be mutated.
Where should the top of the stack now be pointing?
Distasters To Investigate
NOTE: you only need to investigate these disaster clients. You need not fix them. Our NotThreadSafeStack is designed to be used in a single sequential thread. Not surprisingly, it fails when it is mutated in parallel.
ParallelPushStackDisasterClient
| class: | ParallelPushStackDisasterClient.java | |
| methods: | main | |
| package: | stack.notthreadsafe.disaster | |
| source folder: | src/main/java |
ParallelPushAndPopStackDisasterClient
| class: | ParallelPushAndPopStackDisasterClient.java | |
| 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: | stack-pledge-acknowledgments-citations.txt |
More info about the Honor Pledge