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; | |
8c8bf09f ASL |
17 | import java.util.concurrent.Executors; |
18 | import java.util.concurrent.LinkedBlockingQueue; | |
19 | ||
951d134a FC |
20 | /** |
21 | * <b><u>TmfRequestExecutor</u></b> | |
22 | * | |
23 | * Implement me. Please. | |
24 | */ | |
8c8bf09f ASL |
25 | public class TmfRequestExecutor implements Executor { |
26 | ||
fc6ccf6f FC |
27 | private final Executor fExecutor; |
28 | private final Queue<Runnable> fRequests = new LinkedBlockingQueue<Runnable>(); | |
29 | private Runnable fRequest; | |
8c8bf09f | 30 | |
fc6ccf6f | 31 | public TmfRequestExecutor(Executor executor) { |
8c8bf09f | 32 | fExecutor = executor; |
8c8bf09f ASL |
33 | } |
34 | ||
fc6ccf6f FC |
35 | public TmfRequestExecutor() { |
36 | this(Executors.newSingleThreadExecutor()); | |
8c8bf09f | 37 | } |
fc6ccf6f FC |
38 | |
39 | public void execute(final Runnable request) { | |
40 | fRequests.offer(new Runnable() { | |
8c8bf09f ASL |
41 | public void run() { |
42 | try { | |
8c8bf09f | 43 | request.run(); |
8c8bf09f ASL |
44 | } finally { |
45 | scheduleNext(); | |
46 | } | |
47 | } | |
48 | }); | |
fc6ccf6f | 49 | if (fRequest == null) { |
8c8bf09f ASL |
50 | scheduleNext(); |
51 | } | |
52 | } | |
53 | ||
8c8bf09f | 54 | protected synchronized void scheduleNext() { |
fc6ccf6f FC |
55 | if ((fRequest = fRequests.poll()) != null) { |
56 | fExecutor.execute(fRequest); | |
8c8bf09f ASL |
57 | } |
58 | } | |
59 | ||
fc6ccf6f FC |
60 | public synchronized void queueRequest(Runnable request) { |
61 | fRequests.add(request); | |
62 | scheduleNext(); | |
8c8bf09f ASL |
63 | } |
64 | ||
65 | } |