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);
48 * @return the number of pending requests
50 public int getNbPendingRequests() {
51 return fRequestQueue
.size();
55 * @return the shutdown state (i.e. if it is accepting new requests)
57 public boolean isShutdown() {
58 return fExecutor
.isShutdown();
62 * @return the termination state
64 public boolean isTerminated() {
65 return fExecutor
.isTerminated();
75 // ------------------------------------------------------------------------
77 // ------------------------------------------------------------------------
80 * @see java.util.concurrent.Executor#execute(java.lang.Runnable)
82 public synchronized void execute(final Runnable request
) {
83 fRequestQueue
.offer(new Runnable() {
92 if (fCurrentRequest
== null) {
98 * Executes the next pending request, if applicable.
100 protected synchronized void scheduleNext() {
101 if ((fCurrentRequest
= fRequestQueue
.poll()) != null) {
102 fExecutor
.execute(fCurrentRequest
);
106 // ------------------------------------------------------------------------
108 // ------------------------------------------------------------------------
111 public String
toString() {
112 return "[TmfRequestExecutor(" + fExecutorName
+ ")]";