1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
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
10 * Alexandre Montplaisir - Initial API and implementation
11 * Patrick Tasse - Update queue handling
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.ui
;
16 import java
.util
.LinkedHashMap
;
19 import org
.eclipse
.swt
.widgets
.Display
;
22 * This handler offers "coalescing" of UI updates.
24 * When displaying live experiments containing a high number of traces, every
25 * trace will want to regularly update views with their new available data. This
26 * can cause a high number of threads calling {@link Display#asyncExec}
27 * repeatedly, which can really impede UI responsiveness.
29 * Instead of calling {@link Display#asyncExec} directly, threads that want to
30 * queue updates to the UI can instead call
31 * {@link TmfUiRefreshHandler#queueUpdate}. If the handler is not currently
32 * executing another update it will be scheduled immediately. Otherwise the
33 * update will be queued.
35 * The handler will only execute one update at a time. While it is busy, new
36 * requests received from a source that is already in the queue will replace the
37 * previous one (as we assume the latest UI update request is the most
38 * up-to-date and interesting one), preserving the previous request order. New
39 * requests received from other sources will be added to the end of the queue
40 * (keeping only the latest request from each source).
42 * Once the current update is completed, the oldest request in the queue will be
43 * sent to the UI thread via one single call to {@link Display#syncExec}.
45 * @author Alexandre Montplaisir
48 public class TmfUiRefreshHandler
{
50 /** Singleton instance */
51 private static TmfUiRefreshHandler fInstance
= null;
53 private final Map
<Object
, Runnable
> fUpdates
= new LinkedHashMap
<>();
54 private Thread fCurrentTask
;
58 * Internal constructor
60 private TmfUiRefreshHandler() {
65 * Get the handler's instance
67 * @return The singleton instance
69 public static synchronized TmfUiRefreshHandler
getInstance() {
70 if (fInstance
== null) {
71 fInstance
= new TmfUiRefreshHandler();
77 * Cancel all current requests and dispose the handler.
79 public synchronized void dispose() {
85 * Queue a UI update. Threads that want to benefit from "UI coalescing"
86 * should send their {@link Runnable} to this method, instead of
87 * {@link Display#asyncExec(Runnable)}.
90 * The source sending the request. Typically callers should use
91 * "this". When multiple requests are queued before being
92 * executed, only the latest request per source is actually sent.
94 * The {@link Runnable} to execute in the UI thread.
96 public synchronized void queueUpdate(Object source
, Runnable task
) {
97 fUpdates
.put(source
, task
);
98 if (fCurrentTask
== null) {
99 fCurrentTask
= new RunAllUpdates();
100 fCurrentTask
.start();
105 * Task to empty the update queue, and send each task to the UI thread.
107 private class RunAllUpdates
extends Thread
{
111 Runnable nextTask
= null;
112 synchronized (TmfUiRefreshHandler
.this) {
113 if (!fUpdates
.isEmpty()) {
114 Object firstKey
= fUpdates
.keySet().iterator().next();
115 nextTask
= fUpdates
.get(firstKey
);
116 fUpdates
.remove(firstKey
);
118 if (nextTask
== null) {
120 * No updates remaining in the queue, put fCurrentTask
121 * back to null so that a new task can be scheduled.
127 Display
.getDefault().syncExec(nextTask
);