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