1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
14 * @author Toni Kalajainen
\r
16 package org.simantics.utils.threads;
\r
18 import java.util.concurrent.Callable;
\r
19 import java.util.concurrent.ScheduledFuture;
\r
20 import java.util.concurrent.ScheduledThreadPoolExecutor;
\r
21 import java.util.concurrent.TimeUnit;
\r
24 public class ExecutorWorker {
\r
26 private static ExecutorWorker instance;
\r
28 static class DelayedExecution implements Comparable<DelayedExecution> {
\r
29 Executable executable;
\r
32 public int compareTo(DelayedExecution o) {
\r
33 if (o.executionTime<executionTime) return -1;
\r
34 if (o.executionTime>executionTime) return 1;
\r
39 public static ExecutorWorker getInstance()
\r
41 if (instance == null)
\r
42 instance = new ExecutorWorker();
\r
49 ScheduledThreadPoolExecutor pool = new ScheduledThreadPoolExecutor(1);
\r
51 public synchronized ScheduledFuture<Object> timerExec(final Executable executable, int delay)
\r
53 Callable<Object> c = new Callable<Object>() {
\r
55 public Object call() throws Exception {
\r
56 // FIXME: executable.runnable gets called twice!
\r
57 ThreadUtils.asyncExec(executable.threadAccess, executable.runnable);
\r
58 //executable.runnable.run();
\r
62 return pool.schedule(c, delay, TimeUnit.MILLISECONDS);
\r