X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.utils.thread%2Fsrc%2Forg%2Fsimantics%2Futils%2Fthreads%2FWorkerThread.java;h=1b27c2de1f50b1ff9dfbf3b0b880572912eda54c;hb=1dfeb7d5c49b1391cd9d877e1eddab18995cb151;hp=a48187bf24ae9d9acb070363ff451cf6fba91724;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.utils.thread/src/org/simantics/utils/threads/WorkerThread.java b/bundles/org.simantics.utils.thread/src/org/simantics/utils/threads/WorkerThread.java index a48187bf2..1b27c2de1 100644 --- a/bundles/org.simantics.utils.thread/src/org/simantics/utils/threads/WorkerThread.java +++ b/bundles/org.simantics.utils.thread/src/org/simantics/utils/threads/WorkerThread.java @@ -1,124 +1,124 @@ -/******************************************************************************* - * Copyright (c) 2007, 2010 Association for Decentralized Information Management - * in Industry THTH ry. - * 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: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -/* - * - * @author Toni Kalajainen - */ -package org.simantics.utils.threads; - -import java.util.LinkedList; -import java.util.concurrent.Semaphore; - -import org.simantics.utils.threads.internal.Pair; - - -/** - * Thread manager - * - * TODO Replace with ScheduledThreadPoolExecutor - */ -public class WorkerThread extends Thread implements IThreadWorkQueue { - - LinkedList> list = new LinkedList>(); - - boolean stop = false; - - Semaphore finishPermit = new Semaphore(0); - - public WorkerThread() { - super(); - } - - public WorkerThread(String name) { - super(name); - } - - public void stopDispatchingEvents(boolean blockUntilCompleted) { - synchronized (this) { - stop = true; - notify(); - } - if (blockUntilCompleted) { - try { - finishPermit.acquire(); - } catch (InterruptedException e) { - } - } - } - - @Override - public void run() { - try { - while (!stop) { - while (true) { - Pair p = null; - synchronized (this) { - if(list.isEmpty()) - break; - p = list.pop(); - } - Runnable r = p.first; - try { - r.run(); - } catch (RuntimeException rte) { - rte.printStackTrace(); - } - if (p.second != null) - p.second.release(1); - } - synchronized (this) { - if (!stop) - try { - wait(10000L); - } catch (InterruptedException e) { - } - } - } - } finally { - finishPermit.release(); - } - } - - @Override - public boolean syncExec(Runnable runnable) { - Semaphore s = new Semaphore(0); - synchronized (this) { - if (stop) - return false; - Pair p = new Pair(runnable, s); - list.addFirst(p); - notify(); - } - return true; - } - - @Override - public synchronized Thread asyncExec(Runnable runnable) { - if (stop) - return null; - Pair p = new Pair(runnable, new Semaphore(0)); - list.addLast(p); - notify(); - return this; - } - - @Override - public boolean currentThreadAccess() { - return Thread.currentThread() == this; - } - - @Override - public Thread getThread() { - return this; - } - -} +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * 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: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +/* + * + * @author Toni Kalajainen + */ +package org.simantics.utils.threads; + +import java.util.LinkedList; +import java.util.concurrent.Semaphore; + +import org.simantics.utils.threads.internal.Pair; + + +/** + * Thread manager + * + * TODO Replace with ScheduledThreadPoolExecutor + */ +public class WorkerThread extends Thread implements IThreadWorkQueue { + + LinkedList> list = new LinkedList>(); + + boolean stop = false; + + Semaphore finishPermit = new Semaphore(0); + + public WorkerThread() { + super(); + } + + public WorkerThread(String name) { + super(name); + } + + public void stopDispatchingEvents(boolean blockUntilCompleted) { + synchronized (this) { + stop = true; + notify(); + } + if (blockUntilCompleted) { + try { + finishPermit.acquire(); + } catch (InterruptedException e) { + } + } + } + + @Override + public void run() { + try { + while (!stop) { + while (true) { + Pair p = null; + synchronized (this) { + if(list.isEmpty()) + break; + p = list.pop(); + } + Runnable r = p.first; + try { + r.run(); + } catch (RuntimeException rte) { + rte.printStackTrace(); + } + if (p.second != null) + p.second.release(1); + } + synchronized (this) { + if (!stop) + try { + wait(10000L); + } catch (InterruptedException e) { + } + } + } + } finally { + finishPermit.release(); + } + } + + @Override + public boolean syncExec(Runnable runnable) { + Semaphore s = new Semaphore(0); + synchronized (this) { + if (stop) + return false; + Pair p = new Pair(runnable, s); + list.addFirst(p); + notify(); + } + return true; + } + + @Override + public synchronized Thread asyncExec(Runnable runnable) { + if (stop) + return null; + Pair p = new Pair(runnable, new Semaphore(0)); + list.addLast(p); + notify(); + return this; + } + + @Override + public boolean currentThreadAccess() { + return Thread.currentThread() == this; + } + + @Override + public Thread getThread() { + return this; + } + +}