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