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