Difference between revisions of "Concurrent Stack Assignment"

From CSE231 Wiki
Jump to navigation Jump to search
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
=Previous Exercise=
 +
Recall that you have built a [[Stack_Assignment|NotThreadSafeStack]].
 +
 
=Motivation=
 
=Motivation=
We will build a thread-safe implementation of a Stack using synchronized methods.
+
We will modify our previous Stack solution, this time producing a thread-safe implementation of a Stack using [https://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html synchronized methods].
  
 
=Background=
 
=Background=
==Implicit Locks==
+
[https://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html Synchronized methods] are common way to enforce mutual exclusion on shared, mutable data.
[https://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html synchronized methods]
 
  
 
=Code To Implement=
 
=Code To Implement=
Line 10: Line 12:
 
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/>push<br/>peek<br/>pop|stack.concurrent.exercise}}
+
{{CodeToImplement|ConcurrentStack|push<br/>peek<br/>pop|stack.concurrent.exercise}}
===constructor and instance variables===
+
===instance variables===
The constructor
+
The data structure instance variables from the [[Stack_Assignment#instance_variables|NotThreadSafeStack]] will serve equally well here.
 
 
<nowiki>public ConcurrentStack(BiFunction<E, Optional<Node<E>>, Node<E>> nodeCreator)</nowiki>
 
 
 
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 <code>final</code> instance variable along with whatever other state you need to implement a mutable thread-safe Stack.
 
 
 
 
====push====
 
====push====
 +
similar to [[Stack_Assignment#push|NotThreadSafeStack's push]] method, with the added requirement of thread safety.
 
====peek====
 
====peek====
 +
similar to [[Stack_Assignment#peek|NotThreadSafeStack's peek]] method, with the added requirement of thread safety.
 
====pop====
 
====pop====
 +
similar to [[Stack_Assignment#pop|NotThreadSafeStack's pop]] method, with the added requirement of thread safety.
  
 
=Testing=
 
=Testing=

Latest revision as of 19:50, 7 April 2023

Previous Exercise

Recall that you have built a NotThreadSafeStack.

Motivation

We will modify our previous Stack solution, this time producing a thread-safe implementation of a Stack using synchronized methods.

Background

Synchronized methods are common way to enforce mutual exclusion on shared, mutable data.

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: push
peek
pop
package: stack.concurrent.exercise
source folder: student/src/main/java

instance variables

The data structure instance variables from the NotThreadSafeStack will serve equally well here.

push

similar to NotThreadSafeStack's push method, with the added requirement of thread safety.

peek

similar to NotThreadSafeStack's peek method, with the added requirement of thread safety.

pop

similar to NotThreadSafeStack's pop method, with the added requirement of thread safety.

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