Internalize some classes and fix a pile of warnings
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / internal / tmf / core / 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.internal.tmf.core.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.internal.tmf.core.Tracer;
22 import org.eclipse.linuxtools.internal.tmf.core.component.TmfThread;
23 import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest.ExecutionType;
24
25 /**
26 * A simple, straightforward request executor.
27 *
28 * @version 1.0
29 * @author Francois Chouinard
30 */
31 public class TmfRequestExecutor implements Executor {
32
33 private final ExecutorService fExecutor;
34 private final String fExecutorName;
35 private final PriorityBlockingQueue<TmfThread> fRequestQueue = new PriorityBlockingQueue<TmfThread>(100, new Comparator<TmfThread>() {
36 @Override
37 public int compare(TmfThread o1, TmfThread o2) {
38 if (o1.getExecType() == o2.getExecType())
39 return 0;
40 if (o1.getExecType() == ExecutionType.BACKGROUND)
41 return 1;
42 return -1;
43 }
44 });
45 private TmfThread fCurrentRequest;
46
47 // ------------------------------------------------------------------------
48 // Constructors
49 // ------------------------------------------------------------------------
50
51 public TmfRequestExecutor() {
52 this(Executors.newSingleThreadExecutor());
53 }
54
55 public TmfRequestExecutor(ExecutorService executor) {
56 fExecutor = 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$
60 }
61
62 /**
63 * @return the number of pending requests
64 */
65 public synchronized int getNbPendingRequests() {
66 return fRequestQueue.size();
67 }
68
69 /**
70 * @return the shutdown state (i.e. if it is accepting new requests)
71 */
72 public synchronized boolean isShutdown() {
73 return fExecutor.isShutdown();
74 }
75
76 /**
77 * @return the termination state
78 */
79 public synchronized boolean isTerminated() {
80 return fExecutor.isTerminated();
81 }
82
83 /**
84 * Stops the executor
85 */
86 public synchronized void stop() {
87 if (fCurrentRequest != null) {
88 fCurrentRequest.cancel();
89 }
90
91 while ((fCurrentRequest = fRequestQueue.poll()) != null) {
92 fCurrentRequest.cancel();
93 }
94
95 fExecutor.shutdown();
96 if (Tracer.isComponentTraced()) Tracer.trace(fExecutor + " terminated"); //$NON-NLS-1$
97 }
98
99 // ------------------------------------------------------------------------
100 // Operations
101 // ------------------------------------------------------------------------
102
103 /* (non-Javadoc)
104 * @see java.util.concurrent.Executor#execute(java.lang.Runnable)
105 */
106 @Override
107 public synchronized void execute(final Runnable requestThread) {
108 fRequestQueue.offer(new TmfThread(((TmfThread) requestThread).getExecType()) {
109 @Override
110 public void run() {
111 try {
112 requestThread.run();
113 } finally {
114 scheduleNext();
115 }
116 }
117 @Override
118 public void cancel() {
119 ((TmfThread) requestThread).cancel();
120 }
121 });
122 if (fCurrentRequest == null) {
123 scheduleNext();
124 }
125 }
126
127 /**
128 * Executes the next pending request, if applicable.
129 */
130 protected synchronized void scheduleNext() {
131 if ((fCurrentRequest = fRequestQueue.poll()) != null) {
132 if (!isShutdown())
133 fExecutor.execute(fCurrentRequest);
134 }
135 }
136
137 // ------------------------------------------------------------------------
138 // Object
139 // ------------------------------------------------------------------------
140
141 @Override
142 @SuppressWarnings("nls")
143 public String toString() {
144 return "[TmfRequestExecutor(" + fExecutorName + ")]";
145 }
146
147 }
This page took 0.036502 seconds and 5 git commands to generate.