forked from vijayontheweb/Concurrency
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListHelper.java
More file actions
34 lines (30 loc) · 929 Bytes
/
Copy pathListHelper.java
File metadata and controls
34 lines (30 loc) · 929 Bytes
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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package concurrency.composingobjects;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Implementing put if absent with client side locking
*
* To make this approach work, we have to use the same lock that the List uses
* by using client side locking or external locking. Client side locking
* entails guarding client code that uses some object X with the lock X uses to
* guard its own state. In order to use client side locking, you must know what
* lock X uses.
*
* @author vijay
* ThreadSafe
*/
public class ListHelper<E> {
public List<E> list = Collections.synchronizedList(new ArrayList<E>());
public void putIfAbsent(E e) {
synchronized (list) {
if (list.contains(e)) {
list.add(e);
}
}
}
}