1 /*******************************************************************************
2 * Copyright (c) 2013 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 * Semantum Oy - initial API and implementation
12 *******************************************************************************/
13 package org.simantics.simulator.variable.impl;
15 import java.util.ArrayDeque;
16 import java.util.concurrent.CountDownLatch;
17 import java.util.concurrent.Semaphore;
18 import java.util.concurrent.TimeUnit;
20 import org.simantics.simulator.variable.Realm;
22 public class SimpleRealm implements Realm {
24 private ArrayDeque<Runnable> tasks = new ArrayDeque<Runnable>();
25 private Semaphore taskCount = new Semaphore(0);
27 public void executeTasks() {
28 while(taskCount.tryAcquire()) {
30 synchronized (tasks) {
37 public void waitTasks(long waitDuration) throws InterruptedException {
38 taskCount.tryAcquire(waitDuration, TimeUnit.MILLISECONDS);
42 public void syncExec(final Runnable runnable) throws InterruptedException {
43 final CountDownLatch latch = new CountDownLatch(1);
44 asyncExec(new Runnable() {
55 public void asyncExec(Runnable runnable) {
56 synchronized (tasks) {