All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.sshd.common.util.threads.SshThreadPoolExecutor Maven / Gradle / Ivy

There is a newer version: 2.4.1.Final
Show newest version
/*
 * 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 org.apache.sshd.common.util.threads;

import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.sshd.common.future.CloseFuture;
import org.apache.sshd.common.future.SshFutureListener;
import org.apache.sshd.common.util.closeable.AbstractCloseable;

/**
 * @author Apache MINA SSHD Project
 */
public class SshThreadPoolExecutor extends ThreadPoolExecutor implements CloseableExecutorService {
    protected final DelegateCloseable closeable = new DelegateCloseable();

    protected class DelegateCloseable extends AbstractCloseable {
        protected DelegateCloseable() {
            super();
        }

        @Override
        protected CloseFuture doCloseGracefully() {
            shutdown();
            return closeFuture;
        }

        @Override
        protected void doCloseImmediately() {
            shutdownNow();
            super.doCloseImmediately();
        }

        protected void setClosed() {
            closeFuture.setClosed();
        }
    }

    public SshThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
                                 BlockingQueue workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    public SshThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
                                 BlockingQueue workQueue, ThreadFactory threadFactory) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
    }

    public SshThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
                                 BlockingQueue workQueue, RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
    }

    public SshThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
                                 BlockingQueue workQueue, ThreadFactory threadFactory,
                                 RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
    }

    @Override
    protected void terminated() {
        closeable.doCloseImmediately();
    }

    @Override
    public void shutdown() {
        super.shutdown();
    }

    @Override
    public List shutdownNow() {
        return super.shutdownNow();
    }

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

    @Override
    public boolean isTerminating() {
        return super.isTerminating();
    }

    @Override
    public boolean isTerminated() {
        return super.isTerminated();
    }

    @Override
    public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
        return super.awaitTermination(timeout, unit);
    }

    @Override
    public CloseFuture close(boolean immediately) {
        return closeable.close(immediately);
    }

    @Override
    public void addCloseFutureListener(SshFutureListener listener) {
        closeable.addCloseFutureListener(listener);
    }

    @Override
    public void removeCloseFutureListener(SshFutureListener listener) {
        closeable.removeCloseFutureListener(listener);
    }

    @Override
    public boolean isClosed() {
        return closeable.isClosed();
    }

    @Override
    public boolean isClosing() {
        return closeable.isClosing();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy