blob: 7177c337fdd8b01357912c16ed288c45a1f1acdb (
plain) (
blame)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.core;
import com.yahoo.jdisc.ResourceReference;
import com.yahoo.jdisc.application.DeactivatedContainer;
/**
* @author Simon Thoresen Hult
*/
public class ContainerTermination implements DeactivatedContainer, Runnable {
private final Object lock = new Object();
private final Object appContext;
private final ResourceReference containerReference;
private Runnable task;
private boolean done;
private boolean closed;
public ContainerTermination(Object appContext, ResourceReference containerReference) {
this.appContext = appContext;
this.containerReference = containerReference;
}
@Override
public Object appContext() {
return appContext;
}
@Override
public void notifyTermination(Runnable task) {
boolean done;
synchronized (lock) {
if (this.task != null) {
throw new IllegalStateException();
}
this.task = task;
done = this.done;
}
if (done) {
task.run();
}
}
@Override
public void close() {
synchronized (lock) {
if (closed) return;
closed = true;
containerReference.close();
}
}
@Override
public void run() {
Runnable task;
synchronized (lock) {
done = true;
task = this.task;
}
if (task != null) {
task.run();
}
}
}
|