17
17
package org .springframework .boot ;
18
18
19
19
import java .util .Collections ;
20
- import java .util .IdentityHashMap ;
21
20
import java .util .LinkedHashSet ;
22
21
import java .util .Set ;
23
22
import java .util .WeakHashMap ;
@@ -104,16 +103,16 @@ void deregisterFailedApplicationContext(ConfigurableApplicationContext applicati
104
103
public void run () {
105
104
Set <ConfigurableApplicationContext > contexts ;
106
105
Set <ConfigurableApplicationContext > closedContexts ;
107
- Set <Runnable > actions ;
106
+ Set <Handler > handlers ;
108
107
synchronized (SpringApplicationShutdownHook .class ) {
109
108
this .inProgress = true ;
110
109
contexts = new LinkedHashSet <>(this .contexts );
111
110
closedContexts = new LinkedHashSet <>(this .closedContexts );
112
- actions = new LinkedHashSet <>(this .handlers .getActions ());
111
+ handlers = new LinkedHashSet <>(this .handlers .getActions ());
113
112
}
114
113
contexts .forEach (this ::closeAndWait );
115
114
closedContexts .forEach (this ::closeAndWait );
116
- actions .forEach (Runnable ::run );
115
+ handlers .forEach (Handler ::run );
117
116
}
118
117
119
118
boolean isApplicationContextRegistered (ConfigurableApplicationContext context ) {
@@ -171,15 +170,15 @@ private void assertNotInProgress() {
171
170
*/
172
171
private final class Handlers implements SpringApplicationShutdownHandlers , Runnable {
173
172
174
- private final Set <Runnable > actions = Collections . newSetFromMap ( new IdentityHashMap <>() );
173
+ private final Set <Handler > actions = new LinkedHashSet <>();
175
174
176
175
@ Override
177
176
public void add (Runnable action ) {
178
177
Assert .notNull (action , "Action must not be null" );
179
178
addRuntimeShutdownHookIfNecessary ();
180
179
synchronized (SpringApplicationShutdownHook .class ) {
181
180
assertNotInProgress ();
182
- this .actions .add (action );
181
+ this .actions .add (new Handler ( action ) );
183
182
}
184
183
}
185
184
@@ -188,11 +187,11 @@ public void remove(Runnable action) {
188
187
Assert .notNull (action , "Action must not be null" );
189
188
synchronized (SpringApplicationShutdownHook .class ) {
190
189
assertNotInProgress ();
191
- this .actions .remove (action );
190
+ this .actions .remove (new Handler ( action ) );
192
191
}
193
192
}
194
193
195
- Set <Runnable > getActions () {
194
+ Set <Handler > getActions () {
196
195
return this .actions ;
197
196
}
198
197
@@ -204,6 +203,36 @@ public void run() {
204
203
205
204
}
206
205
206
+ /**
207
+ * A single handler that uses object identity for {@link #equals(Object)} and
208
+ * {@link #hashCode()}.
209
+ *
210
+ * @param runnable the handler runner
211
+ */
212
+ record Handler (Runnable runnable ) {
213
+
214
+ @ Override
215
+ public int hashCode () {
216
+ return System .identityHashCode (this .runnable );
217
+ }
218
+
219
+ @ Override
220
+ public boolean equals (Object obj ) {
221
+ if (this == obj ) {
222
+ return true ;
223
+ }
224
+ if (obj == null || getClass () != obj .getClass ()) {
225
+ return false ;
226
+ }
227
+ return this .runnable == ((Handler ) obj ).runnable ;
228
+ }
229
+
230
+ void run () {
231
+ this .runnable .run ();
232
+ }
233
+
234
+ }
235
+
207
236
/**
208
237
* {@link ApplicationListener} to track closed contexts.
209
238
*/
0 commit comments