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

com.envisioniot.sub.client.internal.SubThread Maven / Gradle / Ivy

There is a newer version: 3.0.3
Show newest version
package com.envisioniot.sub.client.internal;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author by jie.jin on 2018/8/8.
 */
public class SubThread extends Thread {

    protected AtomicBoolean closed = new AtomicBoolean(false);

    private final Logger log = LoggerFactory.getLogger(getClass());

    public static SubThread daemon(final String name, Runnable runnable) {
        return new SubThread(name, runnable, true);
    }

    public static SubThread nonDaemon(final String name, Runnable runnable) {
        return new SubThread(name, runnable, false);
    }

    public SubThread(final String name, boolean daemon) {
        super(name);
        configureThread(name, daemon);
    }

    public SubThread(final String name, Runnable runnable, boolean daemon) {
        super(runnable, name);
        configureThread(name, daemon);
    }

    private void configureThread(final String name, boolean daemon) {
        setDaemon(daemon);
        setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread t, Throwable e) {
                log.error("Uncaught exception in thread '{}':", name, e);
            }
        });
    }

    protected void enable() {
        this.closed.set(false);
    }

    protected void disable() {
        this.closed.set(true);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy