美文网首页
NamedRunnable:给Runnable起名字

NamedRunnable:给Runnable起名字

作者: linheimx | 来源:发表于2016-09-24 11:00 被阅读154次

    runnable 是运行在线程中的。也就是给线程起名字。

    **
     * Runnable implementation which always sets its thread name.
     */
    public abstract class NamedRunnable implements Runnable {
      protected final String name;
    
      public NamedRunnable(String format, Object... args) {
        this.name = Util.format(format, args);
      }
    
      @Override public final void run() {
        String oldName = Thread.currentThread().getName();
        Thread.currentThread().setName(name);
        try {
          execute();
        } finally {
          Thread.currentThread().setName(oldName);
        }
      }
    
      protected abstract void execute();
    }
    

    相关文章

      网友评论

          本文标题:NamedRunnable:给Runnable起名字

          本文链接:https://www.haomeiwen.com/subject/ikjxyttx.html