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

io.deepsense.neptune.clientlibrary.threads.JobInternalThread Maven / Gradle / Ivy

There is a newer version: 1.6.1
Show newest version
/**
 * Copyright (c) 2016, CodiLime Inc.
 */

package io.deepsense.neptune.clientlibrary.threads;

import com.google.common.base.Preconditions;
import io.deepsense.neptune.clientlibrary.utils.ThreadBrowser;

public abstract class JobInternalThread extends Thread {

    private final ThreadBrowser threadBrowser;

    private volatile boolean shutdownRequested;

    public JobInternalThread(ThreadBrowser threadBrowser, String name, boolean isDaemon) {
        this.threadBrowser = Preconditions.checkNotNull(threadBrowser);
        this.setName("neptune-internal-" + Preconditions.checkNotNull(name));
        this.setDaemon(isDaemon);
    }

    @Override
    public void interrupt() {
        throw new RuntimeException("Neptune threads shouldn't use interrupt(). Use gracefulShutdown() instead.");
    }

    public void gracefulShutdown() {
        shutdownRequested = true;
    }

    public boolean isShutdownRequested() {
        return shutdownRequested;
    }

    public boolean areOtherThreadsAlive() {
        return threadBrowser.getActiveThreads().stream()
                .filter(thread -> thread != this
                        && !isInternalJavaThread(thread)
                        && thread.isAlive()
                        && !thread.isDaemon())
                .findAny()
                .isPresent();
    }

    private static boolean isInternalJavaThread(Thread thread) {
        return thread.getName().equals("DestroyJavaVM");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy