]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.thread/src/org/simantics/utils/threads/ExecutorWorker.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.thread / src / org / simantics / utils / threads / ExecutorWorker.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 /*\r
13  *\r
14  * @author Toni Kalajainen\r
15  */\r
16 package org.simantics.utils.threads;\r
17 \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
22 \r
23 \r
24 public class ExecutorWorker {\r
25         \r
26         private static ExecutorWorker instance;\r
27         \r
28         static class DelayedExecution implements Comparable<DelayedExecution> {\r
29                 Executable executable;\r
30                 long executionTime;\r
31                 @Override\r
32                 public int compareTo(DelayedExecution o) {\r
33                         if (o.executionTime<executionTime) return -1;\r
34                         if (o.executionTime>executionTime) return 1;\r
35                         return 0;\r
36                 }\r
37         }\r
38 \r
39         public static ExecutorWorker getInstance()\r
40         {\r
41                 if (instance == null)\r
42                         instance = new ExecutorWorker();\r
43                 return instance;\r
44         }\r
45         \r
46         ExecutorWorker() {              \r
47         }       \r
48 \r
49         ScheduledThreadPoolExecutor pool = new ScheduledThreadPoolExecutor(1);\r
50         \r
51         public synchronized ScheduledFuture<Object> timerExec(final Executable executable, int delay)\r
52         {\r
53                 Callable<Object> c = new Callable<Object>() {\r
54                         @Override\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
59                                 return null;\r
60                         }\r
61                 };              \r
62                 return pool.schedule(c, delay, TimeUnit.MILLISECONDS);\r
63         }\r
64 \r
65 }\r