1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.request
;
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
;
21 import org
.eclipse
.linuxtools
.tmf
.Tracer
;
24 * <b><u>TmfRequestExecutor</u></b>
26 * A simple, straightforward request executor.
28 public class TmfRequestExecutor
implements Executor
{
30 private final ExecutorService fExecutor
;
31 private final String fExecutorName
;
32 private final Queue
<Runnable
> fRequestQueue
= new LinkedBlockingQueue
<Runnable
>();
33 private Runnable fCurrentRequest
;
35 // ------------------------------------------------------------------------
37 // ------------------------------------------------------------------------
39 public TmfRequestExecutor() {
40 this(Executors
.newSingleThreadExecutor());
43 public TmfRequestExecutor(ExecutorService executor
) {
45 String canonicalName
= fExecutor
.getClass().getCanonicalName();
46 fExecutorName
= canonicalName
.substring(canonicalName
.lastIndexOf('.') + 1);
47 Tracer
.trace(fExecutor
+ " created");
51 * @return the number of pending requests
53 public int getNbPendingRequests() {
54 return fRequestQueue
.size();
58 * @return the shutdown state (i.e. if it is accepting new requests)
60 public boolean isShutdown() {
61 return fExecutor
.isShutdown();
65 * @return the termination state
67 public boolean isTerminated() {
68 return fExecutor
.isTerminated();
76 Tracer
.trace(fExecutor
+ " terminated");
79 // ------------------------------------------------------------------------
81 // ------------------------------------------------------------------------
84 * @see java.util.concurrent.Executor#execute(java.lang.Runnable)
86 public synchronized void execute(final Runnable request
) {
87 Tracer
.trace("Queueing request " + request
);
88 fRequestQueue
.offer(new Runnable() {
91 Tracer
.trace("Processing request " + request
);
93 Tracer
.trace("Finishing request " + request
);
99 if (fCurrentRequest
== null) {
105 * Executes the next pending request, if applicable.
107 protected synchronized void scheduleNext() {
108 if ((fCurrentRequest
= fRequestQueue
.poll()) != null) {
109 fExecutor
.execute(fCurrentRequest
);
113 // ------------------------------------------------------------------------
115 // ------------------------------------------------------------------------
118 public String
toString() {
119 return "[TmfRequestExecutor(" + fExecutorName
+ ")]";