forked from vijayontheweb/Concurrency
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBetterVector.java
More file actions
31 lines (28 loc) · 1.04 KB
/
BetterVector.java
File metadata and controls
31 lines (28 loc) · 1.04 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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package concurrency.composingobjects;
import java.util.Vector;
/**
* Extending vector to have a put-if-absent method
*
* Extension is more fragile than adding code directly to a class, because the
* implementation of the synchronization policy is now distributed over
* multiple, separately maintained source files. If the underlying class
* were to change its synchronization policy by choosing a different lock
* to guard its state variables, the subclass would subtly and silently
* break, because it no longer used the right lock to control
* concurrent access to the base class state. (The synchronization policy
* of Vector is fixed by its specification, so BetterVector would not suffer
* from this problem.)
*
* @author vijay
*/
public class BetterVector<E> extends Vector<E>{
public synchronized void putIfAbsent(E e){
if(!contains(e)){
add(e);
}
}
}