15
15
*/
16
16
package rx .internal .util ;
17
17
18
- import java .util .ArrayList ;
19
- import java .util .Arrays ;
20
- import java .util .Collection ;
21
- import java .util .LinkedList ;
22
- import java .util .List ;
23
- import java .util .concurrent .locks .ReentrantLock ;
18
+ import java .util .*;
24
19
25
20
import rx .Subscription ;
26
21
import rx .exceptions .Exceptions ;
@@ -34,7 +29,6 @@ public final class SubscriptionList implements Subscription {
34
29
35
30
private LinkedList <Subscription > subscriptions ;
36
31
private volatile boolean unsubscribed ;
37
- private final ReentrantLock lock = new ReentrantLock ();
38
32
39
33
public SubscriptionList () {
40
34
}
@@ -66,8 +60,7 @@ public void add(final Subscription s) {
66
60
return ;
67
61
}
68
62
if (!unsubscribed ) {
69
- lock .lock ();
70
- try {
63
+ synchronized (this ) {
71
64
if (!unsubscribed ) {
72
65
LinkedList <Subscription > subs = subscriptions ;
73
66
if (subs == null ) {
@@ -77,8 +70,6 @@ public void add(final Subscription s) {
77
70
subs .add (s );
78
71
return ;
79
72
}
80
- } finally {
81
- lock .unlock ();
82
73
}
83
74
}
84
75
// call after leaving the synchronized block so we're not holding a lock while executing this
@@ -88,15 +79,12 @@ public void add(final Subscription s) {
88
79
public void remove (final Subscription s ) {
89
80
if (!unsubscribed ) {
90
81
boolean unsubscribe = false ;
91
- lock .lock ();
92
- try {
82
+ synchronized (this ) {
93
83
LinkedList <Subscription > subs = subscriptions ;
94
84
if (unsubscribed || subs == null ) {
95
85
return ;
96
86
}
97
87
unsubscribe = subs .remove (s );
98
- } finally {
99
- lock .unlock ();
100
88
}
101
89
if (unsubscribe ) {
102
90
// if we removed successfully we then need to call unsubscribe on it (outside of the lock)
@@ -113,16 +101,13 @@ public void remove(final Subscription s) {
113
101
public void unsubscribe () {
114
102
if (!unsubscribed ) {
115
103
List <Subscription > list ;
116
- lock .lock ();
117
- try {
104
+ synchronized (this ) {
118
105
if (unsubscribed ) {
119
106
return ;
120
107
}
121
108
unsubscribed = true ;
122
109
list = subscriptions ;
123
110
subscriptions = null ;
124
- } finally {
125
- lock .unlock ();
126
111
}
127
112
// we will only get here once
128
113
unsubscribeFromAll (list );
@@ -150,12 +135,9 @@ private static void unsubscribeFromAll(Collection<Subscription> subscriptions) {
150
135
public void clear () {
151
136
if (!unsubscribed ) {
152
137
List <Subscription > list ;
153
- lock .lock ();
154
- try {
138
+ synchronized (this ) {
155
139
list = subscriptions ;
156
140
subscriptions = null ;
157
- } finally {
158
- lock .unlock ();
159
141
}
160
142
unsubscribeFromAll (list );
161
143
}
@@ -166,11 +148,8 @@ public void clear() {
166
148
*/
167
149
public boolean hasSubscriptions () {
168
150
if (!unsubscribed ) {
169
- lock .lock ();
170
- try {
151
+ synchronized (this ) {
171
152
return !unsubscribed && subscriptions != null && !subscriptions .isEmpty ();
172
- } finally {
173
- lock .unlock ();
174
153
}
175
154
}
176
155
return false ;
0 commit comments