2010-10-26 Francois Chouinard <fchouinard@gmail.com> Contribution for Bug309042
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf / src / org / eclipse / linuxtools / tmf / request / TmfRequestExecutor.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.request;
14
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;
20
21 import org.eclipse.linuxtools.tmf.Tracer;
22 import org.eclipse.linuxtools.tmf.component.TmfThread;
23 import org.eclipse.linuxtools.tmf.request.ITmfDataRequest.ExecutionType;
24
25 /**
26 * <b><u>TmfRequestExecutor</u></b>
27 *
28 * A simple, straightforward request executor.
29 */
30 public class TmfRequestExecutor implements Executor {
31
32 private final ExecutorService fExecutor;
33 private final String fExecutorName;
34 private final PriorityBlockingQueue<TmfThread> fRequestQueue = new PriorityBlockingQueue<TmfThread>(100, new Comparator<TmfThread>() {
35 @Override
36 public int compare(TmfThread o1, TmfThread o2) {
37 if (o1.getExecType() == o2.getExecType())
38 return 0;
39 if (o1.getExecType() == ExecutionType.BACKGROUND)
40 return 1;
41 return -1;
42 }
43 });
44 private Runnable fCurrentRequest;
45
46 // ------------------------------------------------------------------------
47 // Constructors
48 // ------------------------------------------------------------------------
49
50 public TmfRequestExecutor() {
51 this(Executors.newSingleThreadExecutor());
52 }
53
54 public TmfRequestExecutor(ExecutorService executor) {
55 fExecutor = executor;
56 String canonicalName = fExecutor.getClass().getCanonicalName();
57 fExecutorName = canonicalName.substring(canonicalName.lastIndexOf('.') + 1);
58 if (Tracer.isComponentTraced()) Tracer.trace(fExecutor + " created");
59 }
60
61 /**
62 * @return the number of pending requests
63 */
64 public int getNbPendingRequests() {
65 return fRequestQueue.size();
66 }
67
68 /**
69 * @return the shutdown state (i.e. if it is accepting new requests)
70 */
71 public synchronized boolean isShutdown() {
72 return fExecutor.isShutdown();
73 }
74
75 /**
76 * @return the termination state
77 */
78 public boolean isTerminated() {
79 return fExecutor.isTerminated();
80 }
81
82 /**
83 * Stops the executor
84 */
85 public void stop() {
86 fExecutor.shutdown();
87 if (Tracer.isComponentTraced()) Tracer.trace(fExecutor + " terminated");
88 }
89
90 // ------------------------------------------------------------------------
91 // Operations
92 // ------------------------------------------------------------------------
93
94 /* (non-Javadoc)
95 * @see java.util.concurrent.Executor#execute(java.lang.Runnable)
96 */
97 @Override
98 public synchronized void execute(final Runnable requestThread) {
99 fRequestQueue.offer(new TmfThread(((TmfThread) requestThread).getExecType()) {
100 @Override
101 public void run() {
102 try {
103 requestThread.run();
104 if (Tracer.isRequestTraced()) Tracer.trace("[REQ] Request finished");
105 } finally {
106 scheduleNext();
107 }
108 }
109 });
110 if (fCurrentRequest == null) {
111 scheduleNext();
112 }
113 }
114
115 /**
116 * Executes the next pending request, if applicable.
117 */
118 protected synchronized void scheduleNext() {
119 if ((fCurrentRequest = fRequestQueue.poll()) != null) {
120 if (!isShutdown())
121 fExecutor.execute(fCurrentRequest);
122 }
123 }
124
125 // ------------------------------------------------------------------------
126 // Object
127 // ------------------------------------------------------------------------
128
129 @Override
130 public String toString() {
131 return "[TmfRequestExecutor(" + fExecutorName + ")]";
132 }
133
134 }
This page took 0.033359 seconds and 5 git commands to generate.