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 | 34 | private final PriorityBlockingQueue<TmfThread> fRequestQueue = new PriorityBlockingQueue<TmfThread>(100, new Comparator<TmfThread>() { |
d4011df2 | 35 | @Override |
9b635e61 FC |
36 | public int compare(TmfThread o1, TmfThread o2) { |
37 | if (o1.getExecType() == o2.getExecType()) | |
38 | return 0; | |
f6b14ce2 | 39 | if (o1.getExecType() == ExecutionType.BACKGROUND) |
9b635e61 FC |
40 | return 1; |
41 | return -1; | |
42 | } | |
43 | }); | |
5c00c0b7 FC |
44 | private Runnable fCurrentRequest; |
45 | ||
46 | // ------------------------------------------------------------------------ | |
47 | // Constructors | |
48 | // ------------------------------------------------------------------------ | |
8c8bf09f | 49 | |
fc6ccf6f FC |
50 | public TmfRequestExecutor() { |
51 | this(Executors.newSingleThreadExecutor()); | |
8c8bf09f | 52 | } |
fc6ccf6f | 53 | |
5c00c0b7 FC |
54 | public TmfRequestExecutor(ExecutorService executor) { |
55 | fExecutor = executor; | |
7a88aecf FC |
56 | String canonicalName = fExecutor.getClass().getCanonicalName(); |
57 | fExecutorName = canonicalName.substring(canonicalName.lastIndexOf('.') + 1); | |
3b38ea61 | 58 | if (Tracer.isComponentTraced()) Tracer.trace(fExecutor + " created"); //$NON-NLS-1$ |
54d55ced FC |
59 | } |
60 | ||
5c00c0b7 FC |
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 | */ | |
db1ea19b | 71 | public synchronized boolean isShutdown() { |
5c00c0b7 FC |
72 | return fExecutor.isShutdown(); |
73 | } | |
74 | ||
75 | /** | |
76 | * @return the termination state | |
77 | */ | |
78 | public boolean isTerminated() { | |
79 | return fExecutor.isTerminated(); | |
80 | } | |
81 | ||
2fb2eb37 FC |
82 | /** |
83 | * Stops the executor | |
84 | */ | |
54d55ced FC |
85 | public void stop() { |
86 | fExecutor.shutdown(); | |
3b38ea61 | 87 | if (Tracer.isComponentTraced()) Tracer.trace(fExecutor + " terminated"); //$NON-NLS-1$ |
54d55ced FC |
88 | } |
89 | ||
5c00c0b7 FC |
90 | // ------------------------------------------------------------------------ |
91 | // Operations | |
92 | // ------------------------------------------------------------------------ | |
93 | ||
2fb2eb37 FC |
94 | /* (non-Javadoc) |
95 | * @see java.util.concurrent.Executor#execute(java.lang.Runnable) | |
96 | */ | |
d4011df2 | 97 | @Override |
9b635e61 FC |
98 | public synchronized void execute(final Runnable requestThread) { |
99 | fRequestQueue.offer(new TmfThread(((TmfThread) requestThread).getExecType()) { | |
100 | @Override | |
8c8bf09f ASL |
101 | public void run() { |
102 | try { | |
9b635e61 | 103 | requestThread.run(); |
8c8bf09f ASL |
104 | } finally { |
105 | scheduleNext(); | |
106 | } | |
107 | } | |
108 | }); | |
5c00c0b7 | 109 | if (fCurrentRequest == null) { |
8c8bf09f ASL |
110 | scheduleNext(); |
111 | } | |
112 | } | |
113 | ||
2fb2eb37 FC |
114 | /** |
115 | * Executes the next pending request, if applicable. | |
116 | */ | |
8c8bf09f | 117 | protected synchronized void scheduleNext() { |
5c00c0b7 | 118 | if ((fCurrentRequest = fRequestQueue.poll()) != null) { |
db1ea19b FC |
119 | if (!isShutdown()) |
120 | fExecutor.execute(fCurrentRequest); | |
8c8bf09f ASL |
121 | } |
122 | } | |
123 | ||
5c00c0b7 FC |
124 | // ------------------------------------------------------------------------ |
125 | // Object | |
126 | // ------------------------------------------------------------------------ | |
127 | ||
128 | @Override | |
3b38ea61 | 129 | @SuppressWarnings("nls") |
5c00c0b7 | 130 | public String toString() { |
7a88aecf | 131 | return "[TmfRequestExecutor(" + fExecutorName + ")]"; |
8c8bf09f ASL |
132 | } |
133 | ||
134 | } |