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
.internal
.tmf
.core
.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
.internal
.tmf
.core
.Tracer
;
22 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.component
.TmfThread
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
.ExecutionType
;
26 * A simple, straightforward request executor.
29 * @author Francois Chouinard
31 public class TmfRequestExecutor
implements Executor
{
33 private final ExecutorService fExecutor
;
34 private final String fExecutorName
;
35 private final PriorityBlockingQueue
<TmfThread
> fRequestQueue
= new PriorityBlockingQueue
<TmfThread
>(100, new Comparator
<TmfThread
>() {
37 public int compare(TmfThread o1
, TmfThread o2
) {
38 if (o1
.getExecType() == o2
.getExecType())
40 if (o1
.getExecType() == ExecutionType
.BACKGROUND
)
45 private TmfThread fCurrentRequest
;
47 // ------------------------------------------------------------------------
49 // ------------------------------------------------------------------------
51 public TmfRequestExecutor() {
52 this(Executors
.newSingleThreadExecutor());
55 public TmfRequestExecutor(ExecutorService executor
) {
57 String canonicalName
= fExecutor
.getClass().getCanonicalName();
58 fExecutorName
= canonicalName
.substring(canonicalName
.lastIndexOf('.') + 1);
59 if (Tracer
.isComponentTraced()) Tracer
.trace(fExecutor
+ " created"); //$NON-NLS-1$
63 * @return the number of pending requests
65 public synchronized int getNbPendingRequests() {
66 return fRequestQueue
.size();
70 * @return the shutdown state (i.e. if it is accepting new requests)
72 public synchronized boolean isShutdown() {
73 return fExecutor
.isShutdown();
77 * @return the termination state
79 public synchronized boolean isTerminated() {
80 return fExecutor
.isTerminated();
86 public synchronized void stop() {
87 if (fCurrentRequest
!= null) {
88 fCurrentRequest
.cancel();
91 while ((fCurrentRequest
= fRequestQueue
.poll()) != null) {
92 fCurrentRequest
.cancel();
96 if (Tracer
.isComponentTraced()) Tracer
.trace(fExecutor
+ " terminated"); //$NON-NLS-1$
99 // ------------------------------------------------------------------------
101 // ------------------------------------------------------------------------
104 * @see java.util.concurrent.Executor#execute(java.lang.Runnable)
107 public synchronized void execute(final Runnable requestThread
) {
108 fRequestQueue
.offer(new TmfThread(((TmfThread
) requestThread
).getExecType()) {
118 public void cancel() {
119 ((TmfThread
) requestThread
).cancel();
122 if (fCurrentRequest
== null) {
128 * Executes the next pending request, if applicable.
130 protected synchronized void scheduleNext() {
131 if ((fCurrentRequest
= fRequestQueue
.poll()) != null) {
133 fExecutor
.execute(fCurrentRequest
);
137 // ------------------------------------------------------------------------
139 // ------------------------------------------------------------------------
142 @SuppressWarnings("nls")
143 public String
toString() {
144 return "[TmfRequestExecutor(" + fExecutorName
+ ")]";