-
Notifications
You must be signed in to change notification settings - Fork 0
/
RequestQueue.java
69 lines (56 loc) · 1.55 KB
/
RequestQueue.java
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
65
66
67
68
69
public RequestQueue implements Runnable {
private static RequestQueue me;
private LinkedList<Request> mRequests;
private RequestQueue() {
mRequests = new LinkedList<>();
new Thread(this, "Request Queue").start();
}
public static synchronized RequestQueue sharedInstance() {
if (me == null) {
me = new RequestQueue();
}
return me;
}
public void synchronized addRequest(Request request) {
mRequests.add(request);
this.notify();
}
public void synchronized addSwingNativeRequest(Request request) {
mRequests.add(new SwingNativeRequestDecorator(request));
this.notify();
}
public void synchronized addNativeRequest(Request request) {
mRequests.add(new NativeRequestDecorator(request));
this.notify();
}
@Override
public void run() {
while (true) {
Request request = null;
synchronized (this) {
if (mRequests.size() > 0) {
// Remove item at front of the requests queue
request = mRequests.remove(0);
} else {
// Wait for more items to be added to the queue
request = null;
try {
this.wait();
} catch (InterruptedException e) {
// Unexpectedly restarted
e.printStackTrace();
}
}
}
if (request != null) {
// Process the request
try {
request.run();
} catch (Exception e) {
// Catch exception to avoid stopping thread
e.printStackTrace();
}
}
}
}
}