-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy pathNonBlockingDesign.java
49 lines (43 loc) · 1.4 KB
/
NonBlockingDesign.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package br.com.leonardoz.patterns.non_blocking;
import java.util.concurrent.atomic.AtomicReference;
/**
* Pattern: Non-blocking Design
*
* Motivations: Blocking algorithms are inefficiently under certain loads and are
* susceptible to failures like liveness. Algorithms are called non-blocking if
* failure or suspension of any thread cannot cause the suspension or failure of
* another thread; or if in each step it can make progress in some thread.
*
* Intent: Non-blocking algorithms actually can block briefly, by limiting the
* scope of atomic changes to a single variable while maintaining data
* consistency.
*
* Applicability: Performance dependent operations where blocking designs can be
* a problem.
*
*/
public class NonBlockingDesign {
private AtomicReference<Object> value = new AtomicReference<>(new Object());
public void modifyValue() {
Object updatedValue;
Object old;
do {
old = value.get(); // get value
updatedValue = new Object(); // new updated value
} while (!value.compareAndSet(old, updatedValue));
/*
* if the expected value (old) differs from what is actually present in the
* structure, this means that in the mean time some thread changed it. The loop
* will run until it reaches an acceptable state.
*/
}
public Object getValue() {
return value.get();
}
/*
* Modified value...
*/
public Object newObject() {
return new Object();
}
}