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
13 package org.simantics.utils.threads.ua;
\r
15 import java.util.ArrayDeque;
\r
16 import java.util.Queue;
\r
17 import java.util.concurrent.Executor;
\r
20 * The executor serializes the submission of tasks to a second executor,
\r
21 * illustrating a composite executor.
\r
23 * (This code is copied from the example in {@link Executor})
\r
25 public class SerialExecutor implements Executor {
\r
26 final Queue<Runnable> tasks = new ArrayDeque<Runnable>();
\r
27 final Executor executor;
\r
30 public SerialExecutor(Executor executor) {
\r
31 this.executor = executor;
\r
34 public synchronized void execute(final Runnable r) {
\r
35 tasks.offer(new Runnable() {
\r
44 if (active == null) {
\r
49 protected synchronized void scheduleNext() {
\r
50 if ((active = tasks.poll()) != null) {
\r
51 executor.execute(active);
\r