--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+\r
+package org.simantics.utils.threads.ua;\r
+\r
+import java.util.ArrayDeque;\r
+import java.util.Queue;\r
+import java.util.concurrent.Executor;\r
+\r
+/**\r
+ * The executor serializes the submission of tasks to a second executor,\r
+ * illustrating a composite executor.\r
+ * \r
+ * (This code is copied from the example in {@link Executor})\r
+ */\r
+public class SerialExecutor implements Executor {\r
+ final Queue<Runnable> tasks = new ArrayDeque<Runnable>();\r
+ final Executor executor;\r
+ Runnable active;\r
+ \r
+ public SerialExecutor(Executor executor) {\r
+ this.executor = executor;\r
+ }\r
+ \r
+ public synchronized void execute(final Runnable r) {\r
+ tasks.offer(new Runnable() {\r
+ public void run() {\r
+ try {\r
+ r.run();\r
+ } finally {\r
+ scheduleNext();\r
+ }\r
+ }\r
+ });\r
+ if (active == null) {\r
+ scheduleNext();\r
+ }\r
+ }\r
+ \r
+ protected synchronized void scheduleNext() {\r
+ if ((active = tasks.poll()) != null) {\r
+ executor.execute(active);\r
+ }\r
+ }\r
+ \r
+}\r