Java Concurrent Programming Consensus
Java Concurrent Programming Consensus
Summary
Consensus Queue<> Atomic( n, n(n+1)/2) Approximate agreement Trinary register Team consensus
2
Consensus
Consistent: all threads decide the same value Valid: the common decision value is some thread's input
4 (4)
Consensus Numbers
An object X has consensus number n
If it can be used to solve n-thread consensus
Take any number of instances of X together with atomic read/write registers and implement n-thread consensus
Queue<>
Suppose we augment the FIFO Queue<> class with a peek() method that returns but does not remove the first element in the queue. Show that the augmented queue has infinite consensus number.
6
Queue<> implementation
public class QueueConsensus extends ConsensusProtocol { private Queue queue; public decide(object value) { propose(value); this.queue.enq(threadID); return proposed[this.queue.peek()]; }
This is correct because if X is the first index to be enqueued, later enq() calls will not change X's position, and X stored its value in proposed[] before calling enq().
7
Atomic (n,n(n+1)/2)
Atomic (n,n(n+1)/2)-register assignment for n>1 has consensus number at least n.
Writes to 1 and 2
9
Atomic (n,n(n+1)/2)
n fields r0rn-1 n(n-1)/2 fields rij , where i > j. Each thread i atomically assigns its input value to n fields (with its index).
10
Atomic (n,n(n+1)/2)
Read rij . If the value is null, then neither assignment has occurred. Otherwise, read ri and rj . If ri value is null, then j precedes i, and similarly for rj . If neither ri nor rj is null, reread rij . If its value is equal to the value read from ri, then j precedes i, else vice-versa.
11
b<d<<c,a
12
b<a<d<<c
13
Approximate agreement
The two-thread approximate agreement class for a given : Call decide(xi)=yi (xi is a real number). yi in [min(xa, xb), max(xa, xb)] |ya - yb| < for > 0 What is the consensus number?
14
yi (yi+yj)/2 //average
15
Trinary register
Holds values [,0,1], provides compareAndSet() and get(). Solve n-consensus for K-bits values:
n trinary registers k trinary registers
16
yj.cas(,0)
Team consensus
A Team consensus object provides the same propose() and decide() methods as consensus. A team consensus object solves consensus as long as no more than two distinct values are ever proposed. (If more than two are proposed, the results are undefined.) Show how to solve wait-free n-thread consensus, with up to n distinct input values, from a supply of team consensus objects.
19
4
7 5
5 6
4 4
3 4
1
1 2
7
7 8
20
21