1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
14 * @author Toni Kalajainen
\r
16 package org.simantics.utils.threads;
\r
18 import java.util.LinkedList;
\r
19 import java.util.concurrent.Semaphore;
\r
21 import org.simantics.utils.threads.internal.Pair;
\r
27 * TODO Replace with ScheduledThreadPoolExecutor
\r
29 public class WorkerThread extends Thread implements IThreadWorkQueue {
\r
31 LinkedList<Pair<Runnable, Semaphore>> list = new LinkedList<Pair<Runnable, Semaphore>>();
\r
33 boolean stop = false;
\r
35 Semaphore finishPermit = new Semaphore(0);
\r
37 public WorkerThread() {
\r
41 public WorkerThread(String name) {
\r
45 public void stopDispatchingEvents(boolean blockUntilCompleted) {
\r
46 synchronized (this) {
\r
50 if (blockUntilCompleted) {
\r
52 finishPermit.acquire();
\r
53 } catch (InterruptedException e) {
\r
63 Pair<Runnable, Semaphore> p = null;
\r
64 synchronized (this) {
\r
69 Runnable r = p.first;
\r
72 } catch (RuntimeException rte) {
\r
73 rte.printStackTrace();
\r
75 if (p.second != null)
\r
76 p.second.release(1);
\r
78 synchronized (this) {
\r
82 } catch (InterruptedException e) {
\r
87 finishPermit.release();
\r
92 public boolean syncExec(Runnable runnable) {
\r
93 Semaphore s = new Semaphore(0);
\r
94 synchronized (this) {
\r
97 Pair<Runnable, Semaphore> p = new Pair<Runnable, Semaphore>(runnable, s);
\r
105 public synchronized Thread asyncExec(Runnable runnable) {
\r
108 Pair<Runnable, Semaphore> p = new Pair<Runnable, Semaphore>(runnable, new Semaphore(0));
\r
115 public boolean currentThreadAccess() {
\r
116 return Thread.currentThread() == this;
\r
120 public Thread getThread() {
\r