[Bug292967] Second part of request coalescing + unit tests + minor fixes.
[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.Executors;
18 import java.util.concurrent.LinkedBlockingQueue;
19
20 /**
21 * <b><u>TmfRequestExecutor</u></b>
22 *
23 * Implement me. Please.
24 */
25 public class TmfRequestExecutor implements Executor {
26
27 private final Executor fExecutor;
28 private final Queue<Runnable> fRequests = new LinkedBlockingQueue<Runnable>();
29 private Runnable fRequest;
30
31 public TmfRequestExecutor(Executor executor) {
32 fExecutor = executor;
33 }
34
35 public TmfRequestExecutor() {
36 this(Executors.newSingleThreadExecutor());
37 }
38
39 public void execute(final Runnable request) {
40 fRequests.offer(new Runnable() {
41 public void run() {
42 try {
43 request.run();
44 } finally {
45 scheduleNext();
46 }
47 }
48 });
49 if (fRequest == null) {
50 scheduleNext();
51 }
52 }
53
54 protected synchronized void scheduleNext() {
55 if ((fRequest = fRequests.poll()) != null) {
56 fExecutor.execute(fRequest);
57 }
58 }
59
60 public synchronized void queueRequest(Runnable request) {
61 fRequests.add(request);
62 scheduleNext();
63 }
64
65 }
This page took 0.032436 seconds and 5 git commands to generate.