November 15, 2007 A Java Implementation of a Lock- Free Concurrent Priority Queue Bart Verzijlenberg
November 15, Agenda Algorithm Review Algorithm Review Implementation Implementation Java Package used Java Package used Testing Testing Conclusion Conclusion Questions Questions
November 15, Algorithm Review Priority Queue Priority Queue Lock-free Lock-free Relies on atomic Compare and Swap operations Relies on atomic Compare and Swap operations
November 15, Algorithm Review The algorithm uses the Skip-List data structure The algorithm uses the Skip-List data structure Extends the Skip-List for concurrent use Extends the Skip-List for concurrent use The Skip-List is sorted on the priority of the nodes The Skip-List is sorted on the priority of the nodes The algorithm is lock-free The algorithm is lock-free No blocking No blocking Prevent dead locks Prevent dead locks Always progress by at least one operation Always progress by at least one operation Risk of starvation Risk of starvation
November 15, Algorithm Review Skip-List Multi-layer linked list Multi-layer linked list Probabilistic alternative to balanced trees Probabilistic alternative to balanced trees H forward pointer for a node of height h H forward pointer for a node of height h Each pointer i points to the next node with a height of at least i Each pointer i points to the next node with a height of at least i Probabilistic time complexity log(N) for N nodes Probabilistic time complexity log(N) for N nodes
November 15, Inserting in a Skip-List Inserting 17 in the list
November 15, Implementation Two classes Two classes Node Node Represents an entry in the queue Represents an entry in the queue SkipQueue SkipQueue Contains the algorithm logic Contains the algorithm logic
November 15, Node Class
November 15, SkipQueue Class
November 15, Java Package Used AtomicMarkableReference AtomicMarkableReference In Java.util.concurrent.atomic In Java.util.concurrent.atomic Stores Stores A reference to an object A reference to an object A boolean flag A boolean flag Provides ability to Provides ability to Atomically set both the flag and reference Atomically set both the flag and reference Compare and Swap (CAS) the flag and reference Compare and Swap (CAS) the flag and reference
November 15, Testing Multi-threaded Multi-threaded 10 insert threads 10 insert threads Each inserting 100,000 objects with random integer keys Each inserting 100,000 objects with random integer keys 10 delete threads 10 delete threads Each deleting while the queue is not empty Each deleting while the queue is not empty If the queue is empty, sleep for a bit and try again If the queue is empty, sleep for a bit and try again
November 15, Testing Maintain a counter Maintain a counter Stores the number of nodes dequeued Stores the number of nodes dequeued When 1,000,000 nodes removed When 1,000,000 nodes removed Stop the delete threads Stop the delete threads Check that Check that the queue is empty the queue is empty removed exactly 1,000,000 nodes removed exactly 1,000,000 nodes
November 15, Testing 10 insert threads 10 insert threads Each inserting 100,000 random integer numbers Each inserting 100,000 random integer numbers When all numbers have been inserted When all numbers have been inserted Remove them one at a time. Remove them one at a time. Making sure the nodes come out sorted Making sure the nodes come out sorted
November 15, Removing Atomicity Do atomic references make a difference Do atomic references make a difference i.e. does concurrency come into play, or are we just lucky i.e. does concurrency come into play, or are we just lucky Replace each AtomicMarkableReference with a similar class Replace each AtomicMarkableReference with a similar class Same functions Same functions But they are not atomic But they are not atomic The queue becomes locked after a small number of concurrent deletes. The queue becomes locked after a small number of concurrent deletes.
November 15, Conclusion Further testing is needed to verify the correctness of the implementation Further testing is needed to verify the correctness of the implementation Tests so far are positive Tests so far are positive But cannot be certain there are no problems But cannot be certain there are no problems
November 15, 2007 Questions