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
.Comparator
;
16 import java
.util
.concurrent
.Executor
;
17 import java
.util
.concurrent
.ExecutorService
;
18 import java
.util
.concurrent
.Executors
;
19 import java
.util
.concurrent
.PriorityBlockingQueue
;
21 import org
.eclipse
.linuxtools
.tmf
.Tracer
;
22 import org
.eclipse
.linuxtools
.tmf
.component
.TmfThread
;
23 import org
.eclipse
.linuxtools
.tmf
.request
.ITmfDataRequest
.ExecutionType
;
26 * <b><u>TmfRequestExecutor</u></b>
28 * A simple, straightforward request executor.
30 public class TmfRequestExecutor
implements Executor
{
32 private final ExecutorService fExecutor
;
33 private final String fExecutorName
;
34 private final PriorityBlockingQueue
<TmfThread
> fRequestQueue
= new PriorityBlockingQueue
<TmfThread
>(100, new Comparator
<TmfThread
>() {
36 public int compare(TmfThread o1
, TmfThread o2
) {
37 if (o1
.getExecType() == o2
.getExecType())
39 if (o1
.getExecType() == ExecutionType
.BACKGROUND
)
44 private TmfThread fCurrentRequest
;
46 // ------------------------------------------------------------------------
48 // ------------------------------------------------------------------------
50 public TmfRequestExecutor() {
51 this(Executors
.newSingleThreadExecutor());
54 public TmfRequestExecutor(ExecutorService executor
) {
56 String canonicalName
= fExecutor
.getClass().getCanonicalName();
57 fExecutorName
= canonicalName
.substring(canonicalName
.lastIndexOf('.') + 1);
58 if (Tracer
.isComponentTraced()) Tracer
.trace(fExecutor
+ " created"); //$NON-NLS-1$
62 * @return the number of pending requests
64 public synchronized int getNbPendingRequests() {
65 return fRequestQueue
.size();
69 * @return the shutdown state (i.e. if it is accepting new requests)
71 public synchronized boolean isShutdown() {
72 return fExecutor
.isShutdown();
76 * @return the termination state
78 public synchronized boolean isTerminated() {
79 return fExecutor
.isTerminated();
85 public synchronized void stop() {
86 if (fCurrentRequest
!= null) {
87 fCurrentRequest
.cancel();
90 while ((fCurrentRequest
= fRequestQueue
.poll()) != null) {
91 fCurrentRequest
.cancel();
95 if (Tracer
.isComponentTraced()) Tracer
.trace(fExecutor
+ " terminated"); //$NON-NLS-1$
98 // ------------------------------------------------------------------------
100 // ------------------------------------------------------------------------
103 * @see java.util.concurrent.Executor#execute(java.lang.Runnable)
106 public synchronized void execute(final Runnable requestThread
) {
107 fRequestQueue
.offer(new TmfThread(((TmfThread
) requestThread
).getExecType()) {
117 public void cancel() {
118 ((TmfThread
) requestThread
).cancel();
121 if (fCurrentRequest
== null) {
127 * Executes the next pending request, if applicable.
129 protected synchronized void scheduleNext() {
130 if ((fCurrentRequest
= fRequestQueue
.poll()) != null) {
132 fExecutor
.execute(fCurrentRequest
);
136 // ------------------------------------------------------------------------
138 // ------------------------------------------------------------------------
141 @SuppressWarnings("nls")
142 public String
toString() {
143 return "[TmfRequestExecutor(" + fExecutorName
+ ")]";