File tree Expand file tree Collapse file tree 2 files changed +21
-4
lines changed
inject/src/main/java/io/avaje/inject Expand file tree Collapse file tree 2 files changed +21
-4
lines changed Original file line number Diff line number Diff line change 17
17
import java .util .Map ;
18
18
import java .util .ServiceLoader ;
19
19
import java .util .Set ;
20
+ import java .util .concurrent .locks .ReentrantLock ;
20
21
import java .util .function .Consumer ;
21
22
22
23
/**
@@ -163,6 +164,7 @@ public void run() {
163
164
*/
164
165
private static class ShutdownAwareBeanContext implements BeanContext {
165
166
167
+ private final ReentrantLock lock = new ReentrantLock ();
166
168
private final BeanContext context ;
167
169
private final Hook hook ;
168
170
private boolean shutdown ;
@@ -240,21 +242,27 @@ public void start() {
240
242
241
243
@ Override
242
244
public void close () {
243
- synchronized (this ) {
245
+ lock .lock ();
246
+ try {
244
247
if (!shutdown ) {
245
248
Runtime .getRuntime ().removeShutdownHook (hook );
246
249
}
247
250
context .close ();
251
+ } finally {
252
+ lock .unlock ();
248
253
}
249
254
}
250
255
251
256
/**
252
257
* Close via shutdown hook.
253
258
*/
254
259
void shutdown () {
255
- synchronized (this ) {
260
+ lock .lock ();
261
+ try {
256
262
shutdown = true ;
257
263
close ();
264
+ } finally {
265
+ lock .unlock ();
258
266
}
259
267
}
260
268
}
Original file line number Diff line number Diff line change 11
11
import java .util .Collections ;
12
12
import java .util .List ;
13
13
import java .util .Map ;
14
+ import java .util .concurrent .locks .ReentrantLock ;
14
15
15
16
class DBeanContext implements BeanContext {
16
17
17
18
private static final Logger log = LoggerFactory .getLogger (DBeanContext .class );
18
19
20
+ private final ReentrantLock lock = new ReentrantLock ();
21
+
19
22
private final String name ;
20
23
21
24
private final String [] provides ;
@@ -138,7 +141,8 @@ public List<Object> getBeansWithAnnotation(Class<?> annotation) {
138
141
139
142
@ Override
140
143
public void start () {
141
- synchronized (this ) {
144
+ lock .lock ();
145
+ try {
142
146
if (name != null ) {
143
147
log .debug ("firing postConstruct on beans in context:{}" , name );
144
148
}
@@ -148,12 +152,15 @@ public void start() {
148
152
for (BeanContext childContext : children .values ()) {
149
153
childContext .start ();
150
154
}
155
+ } finally {
156
+ lock .unlock ();
151
157
}
152
158
}
153
159
154
160
@ Override
155
161
public void close () {
156
- synchronized (this ) {
162
+ lock .lock ();
163
+ try {
157
164
if (!closed ) {
158
165
// we only allow one call to preDestroy
159
166
closed = true ;
@@ -167,6 +174,8 @@ public void close() {
167
174
childContext .close ();
168
175
}
169
176
}
177
+ } finally {
178
+ lock .unlock ();
170
179
}
171
180
}
172
181
You can’t perform that action at this time.
0 commit comments