Commit | Line | Data |
---|---|---|
8c8bf09f ASL |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009, 2010 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.request; | |
14 | ||
15 | import java.util.Queue; | |
16 | import java.util.concurrent.Executor; | |
17 | import java.util.concurrent.ExecutorService; | |
18 | import java.util.concurrent.Executors; | |
19 | import java.util.concurrent.LinkedBlockingQueue; | |
20 | ||
21 | import org.eclipse.linuxtools.tmf.Tracer; | |
22 | ||
23 | /** | |
24 | * <b><u>TmfRequestExecutor</u></b> | |
25 | * | |
26 | * A simple, straightforward request executor. | |
27 | */ | |
28 | public class TmfRequestExecutor implements Executor { | |
29 | ||
30 | private final ExecutorService fExecutor; | |
31 | private final String fExecutorName; | |
32 | private final Queue<Runnable> fRequestQueue = new LinkedBlockingQueue<Runnable>(); | |
33 | private Runnable fCurrentRequest; | |
34 | ||
35 | // ------------------------------------------------------------------------ | |
36 | // Constructors | |
37 | // ------------------------------------------------------------------------ | |
38 | ||
39 | public TmfRequestExecutor() { | |
40 | this(Executors.newSingleThreadExecutor()); | |
41 | } | |
42 | ||
43 | public TmfRequestExecutor(ExecutorService executor) { | |
44 | fExecutor = executor; | |
45 | String canonicalName = fExecutor.getClass().getCanonicalName(); | |
46 | fExecutorName = canonicalName.substring(canonicalName.lastIndexOf('.') + 1); | |
47 | Tracer.trace(fExecutor + " created"); | |
48 | } | |
49 | ||
50 | /** | |
51 | * @return the number of pending requests | |
52 | */ | |
53 | public int getNbPendingRequests() { | |
54 | return fRequestQueue.size(); | |
55 | } | |
56 | ||
57 | /** | |
58 | * @return the shutdown state (i.e. if it is accepting new requests) | |
59 | */ | |
60 | public boolean isShutdown() { | |
61 | return fExecutor.isShutdown(); | |
62 | } | |
63 | ||
64 | /** | |
65 | * @return the termination state | |
66 | */ | |
67 | public boolean isTerminated() { | |
68 | return fExecutor.isTerminated(); | |
69 | } | |
70 | ||
71 | /** | |
72 | * Stops the executor | |
73 | */ | |
74 | public void stop() { | |
75 | fExecutor.shutdown(); | |
76 | Tracer.trace(fExecutor + " terminated"); | |
77 | } | |
78 | ||
79 | // ------------------------------------------------------------------------ | |
80 | // Operations | |
81 | // ------------------------------------------------------------------------ | |
82 | ||
83 | /* (non-Javadoc) | |
84 | * @see java.util.concurrent.Executor#execute(java.lang.Runnable) | |
85 | */ | |
86 | public synchronized void execute(final Runnable request) { | |
87 | Tracer.trace("Queueing request " + request); | |
88 | fRequestQueue.offer(new Runnable() { | |
89 | public void run() { | |
90 | try { | |
91 | Tracer.trace("Processing request " + request); | |
92 | request.run(); | |
93 | Tracer.trace("Finishing request " + request); | |
94 | } finally { | |
95 | scheduleNext(); | |
96 | } | |
97 | } | |
98 | }); | |
99 | if (fCurrentRequest == null) { | |
100 | scheduleNext(); | |
101 | } | |
102 | } | |
103 | ||
104 | /** | |
105 | * Executes the next pending request, if applicable. | |
106 | */ | |
107 | protected synchronized void scheduleNext() { | |
108 | if ((fCurrentRequest = fRequestQueue.poll()) != null) { | |
109 | fExecutor.execute(fCurrentRequest); | |
110 | } | |
111 | } | |
112 | ||
113 | // ------------------------------------------------------------------------ | |
114 | // Object | |
115 | // ------------------------------------------------------------------------ | |
116 | ||
117 | @Override | |
118 | public String toString() { | |
119 | return "[TmfRequestExecutor(" + fExecutorName + ")]"; | |
120 | } | |
121 | ||
122 | } |