May 31
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf / src / org / eclipse / linuxtools / tmf / request / TmfRequestExecutor.java
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 /**
22 * <b><u>TmfRequestExecutor</u></b>
23 *
24 * A simple, straightforward request executor.
25 */
26 public class TmfRequestExecutor implements Executor {
27
28 private final ExecutorService fExecutor;
29 private final String fExecutorName;
30 private final Queue<Runnable> fRequestQueue = new LinkedBlockingQueue<Runnable>();
31 private Runnable fCurrentRequest;
32
33 // ------------------------------------------------------------------------
34 // Constructors
35 // ------------------------------------------------------------------------
36
37 public TmfRequestExecutor() {
38 this(Executors.newSingleThreadExecutor());
39 }
40
41 public TmfRequestExecutor(ExecutorService executor) {
42 fExecutor = executor;
43 String canonicalName = fExecutor.getClass().getCanonicalName();
44 fExecutorName = canonicalName.substring(canonicalName.lastIndexOf('.') + 1);
45 // if (Tracer.COMPONENTS) Tracer.trace(fExecutor + " created");
46 }
47
48 /**
49 * @return the number of pending requests
50 */
51 public int getNbPendingRequests() {
52 return fRequestQueue.size();
53 }
54
55 /**
56 * @return the shutdown state (i.e. if it is accepting new requests)
57 */
58 public boolean isShutdown() {
59 return fExecutor.isShutdown();
60 }
61
62 /**
63 * @return the termination state
64 */
65 public boolean isTerminated() {
66 return fExecutor.isTerminated();
67 }
68
69 /**
70 * Stops the executor
71 */
72 public void stop() {
73 fExecutor.shutdown();
74 // if (Tracer.COMPONENTS) Tracer.trace(fExecutor + " terminated");
75 }
76
77 // ------------------------------------------------------------------------
78 // Operations
79 // ------------------------------------------------------------------------
80
81 /* (non-Javadoc)
82 * @see java.util.concurrent.Executor#execute(java.lang.Runnable)
83 */
84 public synchronized void execute(final Runnable request) {
85 fRequestQueue.offer(new Runnable() {
86 public void run() {
87 try {
88 request.run();
89 } finally {
90 scheduleNext();
91 }
92 }
93 });
94 if (fCurrentRequest == null) {
95 scheduleNext();
96 }
97 }
98
99 /**
100 * Executes the next pending request, if applicable.
101 */
102 protected synchronized void scheduleNext() {
103 if ((fCurrentRequest = fRequestQueue.poll()) != null) {
104 fExecutor.execute(fCurrentRequest);
105 }
106 }
107
108 // ------------------------------------------------------------------------
109 // Object
110 // ------------------------------------------------------------------------
111
112 @Override
113 public String toString() {
114 return "[TmfRequestExecutor(" + fExecutorName + ")]";
115 }
116
117 }
This page took 0.033473 seconds and 5 git commands to generate.