Commit | Line | Data |
---|---|---|
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 |
13 | package org.eclipse.linuxtools.tmf.request; |
14 | ||
9b635e61 | 15 | import java.util.Comparator; |
8c8bf09f | 16 | import java.util.concurrent.Executor; |
54d55ced | 17 | import java.util.concurrent.ExecutorService; |
8c8bf09f | 18 | import java.util.concurrent.Executors; |
9b635e61 FC |
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; | |
8c8bf09f | 24 | |
951d134a FC |
25 | /** |
26 | * <b><u>TmfRequestExecutor</u></b> | |
27 | * | |
2fb2eb37 | 28 | * A simple, straightforward request executor. |
951d134a | 29 | */ |
8c8bf09f ASL |
30 | public 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; | |
38 | if (o1.getExecType() == ExecutionType.SHORT) | |
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 | */ | |
70 | public boolean isShutdown() { | |
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 FC |
117 | if ((fCurrentRequest = fRequestQueue.poll()) != null) { |
118 | fExecutor.execute(fCurrentRequest); | |
8c8bf09f ASL |
119 | } |
120 | } | |
121 | ||
5c00c0b7 FC |
122 | // ------------------------------------------------------------------------ |
123 | // Object | |
124 | // ------------------------------------------------------------------------ | |
125 | ||
126 | @Override | |
127 | public String toString() { | |
7a88aecf | 128 | return "[TmfRequestExecutor(" + fExecutorName + ")]"; |
8c8bf09f ASL |
129 | } |
130 | ||
131 | } |