This is sample code from my presentation at BJUG #18.
Contents:
- example1 is about the perils of atomicity and visibility or lack thereof
- example2 is about ThreadLocals and ThreadPools usage
- example3 is about the implementation of a non thread-safe stack
- example4 is about improving that stack for thread-safety and with proper encapsulation for
pop()
- example5 is an implementation of an immutable (persistent) Stack
- example6 builds on the previously implemented immutable Stack for implementing an immutable/persistent Queue and a corresponding mutable non-blocking Queue, contrasting the easiness with which this is achieved by means of persistent data-structures, in contrast with the classic Maged M. Michael and Michael L. Scott algorithm for non-blocking queues
- example7 builds on that non-blocking queue, providing the implementation for simple Actors, somewhat similar to what more potent libraries are doing (e.g. Akka)
- example8 contrasts the difference between reads and writes, by building a very simple in-memory cache, in which the lock used is a ReentrantReadWriteLock