forked from vijayontheweb/Concurrency
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImprovedList.java
More file actions
41 lines (34 loc) · 1.31 KB
/
Copy pathImprovedList.java
File metadata and controls
41 lines (34 loc) · 1.31 KB
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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package concurrency.composingobjects;
import java.util.List;
import java.util.ListIterator;
/**
* Implementing put if absent with composition
*
* ImprovedList adds an additional level of locking using its own intrinsic lock.
* It does not care whether the underlying List is thread safe, because it
* provides its own consistent locking that provides thread safety even if
* the List is not thread safe or changes its locking implementation.While
* the extra layer of synchronization may add some small performance
* penalty,[7] the implementation in ImprovedList is less fragile than
* attempting to mimic the locking strategy of another object. In effect,
* we've used the Java monitor pattern to encapsulate an existing List, and this
* is guaranteed to provide thread safety so long as our class holds the only
* outstanding reference to the underlying List.
*
* @author vijay
*/
public class ImprovedList<T> implements List<T>{
private final List<T> list;
public ImprovedList(List<T> list){
this.list = list;
}
public synchronized void putIfAbsent(E e){
if(!list.contains(e)){
list.add(e);
}
}
}