GenericThread.java

net.minecraft.server.rcon.thread.GenericThread

信息

  • 全限定名:net.minecraft.server.rcon.thread.GenericThread
  • 类型:public abstract class
  • 包:net.minecraft.server.rcon.thread
  • 源码路径:src/main/java/net/minecraft/server/rcon/thread/GenericThread.java
  • 起始行号:L9
  • 实现:Runnable
  • 职责:

    TODO

字段/常量

  • LOGGER

    • 类型: Logger
    • 修饰符: private static final
    • 源码定位: L10
    • 说明:

      TODO

  • UNIQUE_THREAD_ID

    • 类型: AtomicInteger
    • 修饰符: private static final
    • 源码定位: L11
    • 说明:

      TODO

  • MAX_STOP_WAIT

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L12
    • 说明:

      TODO

  • running

    • 类型: boolean
    • 修饰符: protected volatile
    • 源码定位: L13
    • 说明:

      TODO

  • name

    • 类型: String
    • 修饰符: protected final
    • 源码定位: L14
    • 说明:

      TODO

  • thread

    • 类型: Thread
    • 修饰符: protected
    • 源码定位: L15
    • 说明:

      TODO

内部类/嵌套类型

构造器

protected GenericThread(String name) @ L17

  • 构造器名:GenericThread
  • 源码定位:L17
  • 修饰符:protected

参数:

  • name: String

说明:

TODO

方法

下面的方法块按源码顺序生成。

public synchronized boolean start() @ L21

  • 方法名:start
  • 源码定位:L21
  • 返回类型:boolean
  • 修饰符:public synchronized

参数:

说明:

TODO

public synchronized void stop() @ L34

  • 方法名:stop
  • 源码定位:L34
  • 返回类型:void
  • 修饰符:public synchronized

参数:

说明:

TODO

public boolean isRunning() @ L57

  • 方法名:isRunning
  • 源码定位:L57
  • 返回类型:boolean
  • 修饰符:public

参数:

说明:

TODO

代码

public abstract class GenericThread implements Runnable {
    private static final Logger LOGGER = LogUtils.getLogger();
    private static final AtomicInteger UNIQUE_THREAD_ID = new AtomicInteger(0);
    private static final int MAX_STOP_WAIT = 5;
    protected volatile boolean running;
    protected final String name;
    protected @Nullable Thread thread;
 
    protected GenericThread(String name) {
        this.name = name;
    }
 
    public synchronized boolean start() {
        if (this.running) {
            return true;
        } else {
            this.running = true;
            this.thread = new Thread(this, this.name + " #" + UNIQUE_THREAD_ID.incrementAndGet());
            this.thread.setUncaughtExceptionHandler(new DefaultUncaughtExceptionHandlerWithName(LOGGER));
            this.thread.start();
            LOGGER.info("Thread {} started", this.name);
            return true;
        }
    }
 
    public synchronized void stop() {
        this.running = false;
        if (null != this.thread) {
            int waited = 0;
 
            while (this.thread.isAlive()) {
                try {
                    this.thread.join(1000L);
                    if (++waited >= 5) {
                        LOGGER.warn("Waited {} seconds attempting force stop!", waited);
                    } else if (this.thread.isAlive()) {
                        LOGGER.warn("Thread {} ({}) failed to exit after {} second(s)", this, this.thread.getState(), waited, new Exception("Stack:"));
                        this.thread.interrupt();
                    }
                } catch (InterruptedException var3) {
                }
            }
 
            LOGGER.info("Thread {} stopped", this.name);
            this.thread = null;
        }
    }
 
    public boolean isRunning() {
        return this.running;
    }
}

引用的其他类