Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bdf16a52ed9aa162181d65b8722730b1c8ac1ccd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*******************************************************************************
 * Copyright (c) 2006, 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jface.internal.databinding.provisional.swt;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;

import org.eclipse.swt.widgets.Display;

/**
 * NON-API - Helper class to manage a queue of runnables to be posted to the UI
 * thread in a way that they are only run once.
 * 
 * @since 1.1
 * 
 */
public class WorkQueue {

	private boolean updateScheduled = false;

	private LinkedList pendingWork = new LinkedList();

	private Display d;

	private Set pendingWorkSet = new HashSet();

	private Runnable updateJob = new Runnable() {
		public void run() {
			doUpdate();
			updateScheduled = false;
		}
	};

	/**
	 * @param targetDisplay
	 */
	public WorkQueue(Display targetDisplay) {
		d = targetDisplay;
	}

	private void doUpdate() {
		for (;;) {
			Runnable next;
			synchronized (pendingWork) {
				if (pendingWork.isEmpty()) {
					break;
				}
				next = (Runnable) pendingWork.removeFirst();
				pendingWorkSet.remove(next);
			}

			next.run();
		}
	}

	/**
	 * Schedules some work to happen in the UI thread as soon as possible. If
	 * possible, the work will happen before the next control redraws. The given
	 * runnable will only be run once. Has no effect if this runnable has
	 * already been queued for execution.
	 * 
	 * @param work
	 *            runnable to execute
	 */
	public void runOnce(Runnable work) {
		synchronized (pendingWork) {
			if (pendingWorkSet.contains(work)) {
				return;
			}

			pendingWorkSet.add(work);

			asyncExec(work);
		}
	}

	/**
	 * Schedules some work to happen in the UI thread as soon as possible. If
	 * possible, the work will happen before the next control redraws. Unlike
	 * runOnce, calling asyncExec twice with the same runnable will cause that
	 * runnable to run twice.
	 * 
	 * @param work
	 *            runnable to execute
	 */
	public void asyncExec(Runnable work) {
		synchronized (pendingWork) {
			pendingWork.add(work);
			if (!updateScheduled) {
				updateScheduled = true;
				d.timerExec(0, updateJob);
			}
		}
	}

	/**
	 * Cancels a previously-scheduled runnable. Has no effect if the given
	 * runnable was not previously scheduled or has already executed.
	 * 
	 * @param toCancel
	 *            runnable to cancel
	 */
	public void cancelExec(Runnable toCancel) {
		synchronized (pendingWork) {
			pendingWork.remove(toCancel);
			pendingWorkSet.remove(toCancel);
		}
	}

	/**
	 * Cancels all pending work.
	 */
	public void cancelAll() {
		synchronized (pendingWork) {
			pendingWork.clear();
			pendingWorkSet.clear();
		}
	}
}

Back to the top