| 1 | package de.uka.ipd.sdq.simulation.abstractsimengine.processes; |
| 2 | |
| 3 | import java.util.concurrent.Semaphore; |
| 4 | |
| 5 | public class SimProcessThreadingStrategy implements ISimProcessStrategy { |
| 6 | |
| 7 | private Thread myThread = null; |
| 8 | private Semaphore waitingSemaphore = new Semaphore(0); |
| 9 | private Semaphore waitingForSuspendSemaphore = new Semaphore(0); |
| 10 | |
| 11 | public void startProcess(Runnable myRunnable) { |
| 12 | this.myThread = new Thread(myRunnable); |
| 13 | this.myThread.start(); |
| 14 | waitingForSuspendSemaphore.acquireUninterruptibly(); |
| 15 | } |
| 16 | |
| 17 | public void resumeProcess() { |
| 18 | waitingSemaphore.release(); |
| 19 | waitingForSuspendSemaphore.acquireUninterruptibly(); |
| 20 | } |
| 21 | |
| 22 | public void finishProcess() { |
| 23 | // This process is done and will not suspend any more... |
| 24 | // Hence, release its wait for suspend semaphore held by the main control thread. |
| 25 | this.waitingForSuspendSemaphore.release(); |
| 26 | } |
| 27 | |
| 28 | public void suspendProcess() { |
| 29 | waitingForSuspendSemaphore.release(); |
| 30 | waitingSemaphore.acquireUninterruptibly(); |
| 31 | } |
| 32 | |
| 33 | } |