-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-/*\r
- *\r
- * @author Toni Kalajainen\r
- */\r
-package org.simantics.utils.threads;\r
-\r
-import java.util.LinkedList;\r
-import java.util.concurrent.Semaphore;\r
-\r
-import org.simantics.utils.threads.internal.Pair;\r
-\r
-\r
-/**\r
- * Thread manager\r
- * \r
- * TODO Replace with ScheduledThreadPoolExecutor\r
- */\r
-public class WorkerThread extends Thread implements IThreadWorkQueue {\r
-\r
- LinkedList<Pair<Runnable, Semaphore>> list = new LinkedList<Pair<Runnable, Semaphore>>();\r
-\r
- boolean stop = false;\r
-\r
- Semaphore finishPermit = new Semaphore(0);\r
-\r
- public WorkerThread() {\r
- super();\r
- }\r
- \r
- public WorkerThread(String name) {\r
- super(name);\r
- }\r
- \r
- public void stopDispatchingEvents(boolean blockUntilCompleted) {\r
- synchronized (this) {\r
- stop = true;\r
- notify();\r
- }\r
- if (blockUntilCompleted) {\r
- try {\r
- finishPermit.acquire();\r
- } catch (InterruptedException e) {\r
- }\r
- }\r
- }\r
-\r
- @Override\r
- public void run() {\r
- try {\r
- while (!stop) {\r
- while (true) {\r
- Pair<Runnable, Semaphore> p = null;\r
- synchronized (this) {\r
- if(list.isEmpty())\r
- break;\r
- p = list.pop();\r
- } \r
- Runnable r = p.first;\r
- try {\r
- r.run();\r
- } catch (RuntimeException rte) {\r
- rte.printStackTrace();\r
- }\r
- if (p.second != null)\r
- p.second.release(1);\r
- }\r
- synchronized (this) {\r
- if (!stop)\r
- try {\r
- wait(10000L);\r
- } catch (InterruptedException e) {\r
- }\r
- }\r
- }\r
- } finally {\r
- finishPermit.release();\r
- }\r
- }\r
-\r
- @Override\r
- public boolean syncExec(Runnable runnable) {\r
- Semaphore s = new Semaphore(0);\r
- synchronized (this) {\r
- if (stop)\r
- return false;\r
- Pair<Runnable, Semaphore> p = new Pair<Runnable, Semaphore>(runnable, s);\r
- list.addFirst(p);\r
- notify();\r
- }\r
- return true;\r
- }\r
-\r
- @Override\r
- public synchronized Thread asyncExec(Runnable runnable) {\r
- if (stop)\r
- return null;\r
- Pair<Runnable, Semaphore> p = new Pair<Runnable, Semaphore>(runnable, new Semaphore(0));\r
- list.addLast(p);\r
- notify();\r
- return this;\r
- }\r
-\r
- @Override\r
- public boolean currentThreadAccess() {\r
- return Thread.currentThread() == this;\r
- }\r
-\r
- @Override\r
- public Thread getThread() {\r
- return this;\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * 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<Pair<Runnable, Semaphore>> list = new LinkedList<Pair<Runnable, Semaphore>>();
+
+ 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<Runnable, Semaphore> 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<Runnable, Semaphore> p = new Pair<Runnable, Semaphore>(runnable, s);
+ list.addFirst(p);
+ notify();
+ }
+ return true;
+ }
+
+ @Override
+ public synchronized Thread asyncExec(Runnable runnable) {
+ if (stop)
+ return null;
+ Pair<Runnable, Semaphore> p = new Pair<Runnable, Semaphore>(runnable, new Semaphore(0));
+ list.addLast(p);
+ notify();
+ return this;
+ }
+
+ @Override
+ public boolean currentThreadAccess() {
+ return Thread.currentThread() == this;
+ }
+
+ @Override
+ public Thread getThread() {
+ return this;
+ }
+
+}