1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 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 *******************************************************************************/
14 * @author Toni Kalajainen
16 package org.simantics.utils.threads;
18 import java.util.concurrent.Callable;
19 import java.util.concurrent.ScheduledFuture;
20 import java.util.concurrent.ScheduledThreadPoolExecutor;
21 import java.util.concurrent.TimeUnit;
24 public class ExecutorWorker {
26 private static ExecutorWorker instance;
28 static class DelayedExecution implements Comparable<DelayedExecution> {
29 Executable executable;
32 public int compareTo(DelayedExecution o) {
33 if (o.executionTime<executionTime) return -1;
34 if (o.executionTime>executionTime) return 1;
39 public static ExecutorWorker getInstance()
42 instance = new ExecutorWorker();
49 ScheduledThreadPoolExecutor pool = new ScheduledThreadPoolExecutor(1);
51 public synchronized ScheduledFuture<Object> timerExec(final Executable executable, int delay)
53 Callable<Object> c = new Callable<Object>() {
55 public Object call() throws Exception {
56 // FIXME: executable.runnable gets called twice!
57 ThreadUtils.asyncExec(executable.threadAccess, executable.runnable);
58 //executable.runnable.run();
62 return pool.schedule(c, delay, TimeUnit.MILLISECONDS);