Would you like me to add this class, so that existing configurations utilising a TaskManager can also be used? This might be useful for retaining backward compatibility with existing configurations?

Regards,

Peter.


/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sun.jini.thread;

import com.sun.jini.thread.TaskManager.Task;
import java.util.List;
import java.util.concurrent.AbstractExecutorService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;

/**
 *
 * @author peter
 */
public class TaskManagerWrapper extends AbstractExecutorService implements ExecutorService {

    private final TaskManager tm;
    private final PosionPill pill;
    private volatile boolean isShutdown;

    public TaskManagerWrapper(TaskManager manager){
        tm = manager;
        isShutdown = false;
        pill = new PosionPill(manager);
    }

    @Override
    public void shutdown() {
        isShutdown = true;
        tm.add(pill);
    }

    @Override
    public List<Runnable> shutdownNow() {
        isShutdown = true;
        tm.terminate();
        return tm.getPending();
    }

    @Override
    public boolean isShutdown() {
        return isShutdown;
    }

    @Override
    public boolean isTerminated() {
        return isShutdown;
    }

    @Override
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
        long start = System.currentTimeMillis();
        long duration = unit.toMillis(timeout);
        synchronized (pill){
            while (!pill.terminated){
                wait(duration);
                if (pill.terminated) return true;
                long elapsed = System.currentTimeMillis() - start;
                if (elapsed >= duration) return false;
                duration = duration - elapsed;
            }
        }
        return true; // pill was terminated.
    }

    @Override
    public void execute(Runnable command) {
if (isShutdown) throw new RejectedExecutionException("TaskManager terminated");
    }

    private static class PosionPill implements Task {

        private final TaskManager tm;
        boolean terminated;

        PosionPill(TaskManager tm){
            this.tm = tm;
            terminated = false;
        }

        @Override
        public boolean runAfter(List tasks, int size) {
if (!tasks.isEmpty()) return true; // Make sure we always run last.
            return false;
        }

        @Override
        public void run() {
            tm.terminate(); // Bye.
            synchronized (this){
                terminated = true;
                notifyAll();
            }
        }

    }

}

Reply via email to