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
;
22 * <b><u>TmfRequestExecutor</u></b>
24 * A simple, straightforward request executor.
26 public class TmfRequestExecutor
implements Executor
{
28 private final ExecutorService fExecutor
;
29 private final String fExecutorName
;
30 private final Queue
<Runnable
> fRequestQueue
= new LinkedBlockingQueue
<Runnable
>();
31 private Runnable fCurrentRequest
;
33 // ------------------------------------------------------------------------
35 // ------------------------------------------------------------------------
37 public TmfRequestExecutor() {
38 this(Executors
.newSingleThreadExecutor());
41 public TmfRequestExecutor(ExecutorService executor
) {
43 String canonicalName
= fExecutor
.getClass().getCanonicalName();
44 fExecutorName
= canonicalName
.substring(canonicalName
.lastIndexOf('.') + 1);
45 // if (Tracer.COMPONENTS) Tracer.trace(fExecutor + " created");
49 * @return the number of pending requests
51 public int getNbPendingRequests() {
52 return fRequestQueue
.size();
56 * @return the shutdown state (i.e. if it is accepting new requests)
58 public boolean isShutdown() {
59 return fExecutor
.isShutdown();
63 * @return the termination state
65 public boolean isTerminated() {
66 return fExecutor
.isTerminated();
74 // if (Tracer.COMPONENTS) Tracer.trace(fExecutor + " terminated");
77 // ------------------------------------------------------------------------
79 // ------------------------------------------------------------------------
82 * @see java.util.concurrent.Executor#execute(java.lang.Runnable)
84 public synchronized void execute(final Runnable request
) {
85 fRequestQueue
.offer(new Runnable() {
94 if (fCurrentRequest
== null) {
100 * Executes the next pending request, if applicable.
102 protected synchronized void scheduleNext() {
103 if ((fCurrentRequest
= fRequestQueue
.poll()) != null) {
104 fExecutor
.execute(fCurrentRequest
);
108 // ------------------------------------------------------------------------
110 // ------------------------------------------------------------------------
113 public String
toString() {
114 return "[TmfRequestExecutor(" + fExecutorName
+ ")]";