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