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 | ||
15 | import java.util.Queue; | |
16 | import java.util.concurrent.Executor; | |
54d55ced | 17 | import java.util.concurrent.ExecutorService; |
8c8bf09f ASL |
18 | import java.util.concurrent.Executors; |
19 | import java.util.concurrent.LinkedBlockingQueue; | |
20 | ||
951d134a FC |
21 | /** |
22 | * <b><u>TmfRequestExecutor</u></b> | |
23 | * | |
2fb2eb37 | 24 | * A simple, straightforward request executor. |
951d134a | 25 | */ |
8c8bf09f ASL |
26 | public class TmfRequestExecutor implements Executor { |
27 | ||
54d55ced | 28 | private final ExecutorService fExecutor; |
7a88aecf | 29 | private final String fExecutorName; |
5c00c0b7 FC |
30 | private final Queue<Runnable> fRequestQueue = new LinkedBlockingQueue<Runnable>(); |
31 | private Runnable fCurrentRequest; | |
32 | ||
33 | // ------------------------------------------------------------------------ | |
34 | // Constructors | |
35 | // ------------------------------------------------------------------------ | |
8c8bf09f | 36 | |
fc6ccf6f FC |
37 | public TmfRequestExecutor() { |
38 | this(Executors.newSingleThreadExecutor()); | |
8c8bf09f | 39 | } |
fc6ccf6f | 40 | |
5c00c0b7 FC |
41 | public TmfRequestExecutor(ExecutorService executor) { |
42 | fExecutor = executor; | |
7a88aecf FC |
43 | String canonicalName = fExecutor.getClass().getCanonicalName(); |
44 | fExecutorName = canonicalName.substring(canonicalName.lastIndexOf('.') + 1); | |
550d787e | 45 | // if (Tracer.COMPONENTS) Tracer.trace(fExecutor + " created"); |
54d55ced FC |
46 | } |
47 | ||
5c00c0b7 FC |
48 | /** |
49 | * @return the number of pending requests | |
50 | */ | |
51 | public int getNbPendingRequests() { | |
52 | return fRequestQueue.size(); | |
53 | } | |
54 | ||
55 | /** | |
56 | * @return the shutdown state (i.e. if it is accepting new requests) | |
57 | */ | |
58 | public boolean isShutdown() { | |
59 | return fExecutor.isShutdown(); | |
60 | } | |
61 | ||
62 | /** | |
63 | * @return the termination state | |
64 | */ | |
65 | public boolean isTerminated() { | |
66 | return fExecutor.isTerminated(); | |
67 | } | |
68 | ||
2fb2eb37 FC |
69 | /** |
70 | * Stops the executor | |
71 | */ | |
54d55ced FC |
72 | public void stop() { |
73 | fExecutor.shutdown(); | |
550d787e | 74 | // if (Tracer.COMPONENTS) Tracer.trace(fExecutor + " terminated"); |
54d55ced FC |
75 | } |
76 | ||
5c00c0b7 FC |
77 | // ------------------------------------------------------------------------ |
78 | // Operations | |
79 | // ------------------------------------------------------------------------ | |
80 | ||
2fb2eb37 FC |
81 | /* (non-Javadoc) |
82 | * @see java.util.concurrent.Executor#execute(java.lang.Runnable) | |
83 | */ | |
84 | public synchronized void execute(final Runnable request) { | |
5c00c0b7 | 85 | fRequestQueue.offer(new Runnable() { |
8c8bf09f ASL |
86 | public void run() { |
87 | try { | |
8c8bf09f | 88 | request.run(); |
8c8bf09f ASL |
89 | } finally { |
90 | scheduleNext(); | |
91 | } | |
92 | } | |
93 | }); | |
5c00c0b7 | 94 | if (fCurrentRequest == null) { |
8c8bf09f ASL |
95 | scheduleNext(); |
96 | } | |
97 | } | |
98 | ||
2fb2eb37 FC |
99 | /** |
100 | * Executes the next pending request, if applicable. | |
101 | */ | |
8c8bf09f | 102 | protected synchronized void scheduleNext() { |
5c00c0b7 FC |
103 | if ((fCurrentRequest = fRequestQueue.poll()) != null) { |
104 | fExecutor.execute(fCurrentRequest); | |
8c8bf09f ASL |
105 | } |
106 | } | |
107 | ||
5c00c0b7 FC |
108 | // ------------------------------------------------------------------------ |
109 | // Object | |
110 | // ------------------------------------------------------------------------ | |
111 | ||
112 | @Override | |
113 | public String toString() { | |
7a88aecf | 114 | return "[TmfRequestExecutor(" + fExecutorName + ")]"; |
8c8bf09f ASL |
115 | } |
116 | ||
117 | } |