]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.thread/src/org/simantics/utils/threads/WorkerThread.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils.thread / src / org / simantics / utils / threads / WorkerThread.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 /*
13  *
14  * @author Toni Kalajainen
15  */
16 package org.simantics.utils.threads;
17
18 import java.util.LinkedList;
19 import java.util.concurrent.Semaphore;
20
21 import org.simantics.utils.threads.internal.Pair;
22
23
24 /**
25  * Thread manager
26  * 
27  * TODO Replace with ScheduledThreadPoolExecutor
28  */
29 public class WorkerThread extends Thread implements IThreadWorkQueue {
30
31     LinkedList<Pair<Runnable, Semaphore>> list         = new LinkedList<Pair<Runnable, Semaphore>>();
32
33     boolean                               stop         = false;
34
35     Semaphore                             finishPermit = new Semaphore(0);
36
37     public WorkerThread() {
38         super();
39     }
40     
41     public WorkerThread(String name) {
42         super(name);
43     }
44     
45     public void stopDispatchingEvents(boolean blockUntilCompleted) {
46         synchronized (this) {
47             stop = true;
48             notify();
49         }
50         if (blockUntilCompleted) {
51             try {
52                 finishPermit.acquire();
53             } catch (InterruptedException e) {
54             }
55         }
56     }
57
58     @Override
59     public void run() {
60         try {
61             while (!stop) {
62                 while (true) {
63                     Pair<Runnable, Semaphore> p = null;
64                     synchronized (this) {
65                         if(list.isEmpty())
66                             break;
67                         p = list.pop();
68                     }                                         
69                     Runnable r = p.first;
70                     try {
71                         r.run();
72                     } catch (RuntimeException rte) {
73                         rte.printStackTrace();
74                     }
75                     if (p.second != null)
76                         p.second.release(1);
77                 }
78                 synchronized (this) {
79                     if (!stop)
80                         try {
81                             wait(10000L);
82                         } catch (InterruptedException e) {
83                         }
84                 }
85             }
86         } finally {
87             finishPermit.release();
88         }
89     }
90
91     @Override
92     public boolean syncExec(Runnable runnable) {
93         Semaphore s = new Semaphore(0);
94         synchronized (this) {
95             if (stop)
96                 return false;
97             Pair<Runnable, Semaphore> p = new Pair<Runnable, Semaphore>(runnable, s);
98             list.addFirst(p);
99             notify();
100         }
101         return true;
102     }
103
104     @Override
105     public synchronized Thread asyncExec(Runnable runnable) {
106         if (stop)
107             return null;
108         Pair<Runnable, Semaphore> p = new Pair<Runnable, Semaphore>(runnable, new Semaphore(0));
109         list.addLast(p);
110         notify();
111         return this;
112     }
113
114     @Override
115     public boolean currentThreadAccess() {
116         return Thread.currentThread() == this;
117     }
118
119     @Override
120     public Thread getThread() {
121         return this;
122     }
123
124 }