forked from vijayontheweb/Concurrency
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPersonSet.java
More file actions
30 lines (25 loc) · 744 Bytes
/
Copy pathPersonSet.java
File metadata and controls
30 lines (25 loc) · 744 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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package concurrency.composingobjects;
import java.util.HashSet;
import java.util.Set;
/**
* Encapsulating data within an object confines access to the data to the
* object's methods making it easier to ensure that the data is always accessed
* with the appropriate lock held
*
* This example illustrates confinement to ensure thread safety.
*/
class Person{
}
public class PersonSet {
private final Set<Person> mySet = new HashSet<Person>();
public synchronized void addPerson(Person p){
mySet.add(p);
}
public synchronized boolean containsPerson(Person p){
return mySet.contains(p);
}
}