java.lang.Thread.setName()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(6.1k)|赞(0)|评价(0)|浏览(224)

本文整理了Java中java.lang.Thread.setName()方法的一些代码示例,展示了Thread.setName()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Thread.setName()方法的具体详情如下:
包路径:java.lang.Thread
类名称:Thread
方法名:setName

Thread.setName介绍

[英]Changes the name of this thread to be equal to the argument name.

First the checkAccess method of this thread is called with no arguments. This may result in throwing a SecurityException.
[中]将此线程的名称更改为等于参数name
首先,调用此线程的checkAccess方法时不带任何参数。这可能会导致抛出SecurityException

代码示例

代码示例来源:origin: apache/incubator-dubbo

public Notifier(String service) {
  super.setDaemon(true);
  super.setName("DubboRedisSubscribe");
  this.service = service;
}

代码示例来源:origin: apache/incubator-dubbo

public Notifier(String service) {
  super.setDaemon(true);
  super.setName("DubboRedisSubscribe");
  this.service = service;
}

代码示例来源:origin: square/okhttp

@Override public final void run() {
 String oldName = Thread.currentThread().getName();
 Thread.currentThread().setName(name);
 try {
  execute();
 } finally {
  Thread.currentThread().setName(oldName);
 }
}

代码示例来源:origin: jenkinsci/jenkins

@Override public Thread newThread(Runnable r) {
  Thread t = delegate.newThread(r);
  t.setName(name + " [#" + threadNum.incrementAndGet() + "]");
  return t;
}

代码示例来源:origin: log4j/log4j

/**
 Create a thread and start the monitor. */
public
ServerMonitor(int _port, Vector _oosList) {
 port = _port;
 oosList = _oosList;
 keepRunning = true;
 monitorThread = new Thread(this);
 monitorThread.setDaemon(true);
 monitorThread.setName("SocketHubAppender-Monitor-" + port);
 monitorThread.start();
}

代码示例来源:origin: google/guava

@Override
 public Thread newThread(Runnable runnable) {
  Thread thread = backingThreadFactory.newThread(runnable);
  if (nameFormat != null) {
   thread.setName(format(nameFormat, count.getAndIncrement()));
  }
  if (daemon != null) {
   thread.setDaemon(daemon);
  }
  if (priority != null) {
   thread.setPriority(priority);
  }
  if (uncaughtExceptionHandler != null) {
   thread.setUncaughtExceptionHandler(uncaughtExceptionHandler);
  }
  return thread;
 }
};

代码示例来源:origin: square/okhttp

private void drainQueue() throws Exception {
 for (HttpUrl url; (url = queue.take()) != null; ) {
  if (!fetchedUrls.add(url)) {
   continue;
  }
  Thread currentThread = Thread.currentThread();
  String originalName = currentThread.getName();
  currentThread.setName("Crawler " + url.toString());
  try {
   fetch(url);
  } catch (IOException e) {
   System.out.printf("XXX: %s %s%n", url, e);
  } finally {
   currentThread.setName(originalName);
  }
 }
}

代码示例来源:origin: jenkinsci/jenkins

@Override
  public void run() {
    Thread t = Thread.currentThread();
    String name = t.getName();
    ClassLoader cl = t.getContextClassLoader();
    try {
      r.run();
    } finally {
      t.setName(name);
      t.setContextClassLoader(cl);
    }
  }
};

代码示例来源:origin: jenkinsci/jenkins

@Override
  public V call() throws Exception {
    Thread t = Thread.currentThread();
    String name = t.getName();
    ClassLoader cl = t.getContextClassLoader();
    try {
      return r.call();
    } finally {
      t.setName(name);
      t.setContextClassLoader(cl);
    }
  }
};

代码示例来源:origin: prestodb/presto

private static <T> Future<T> executeInNewThread(String threadName, Callable<T> callable)
  {
    FutureTask<T> task = new FutureTask<>(callable);
    Thread thread = new Thread(task);
    thread.setName(threadName);
    thread.setDaemon(true);
    thread.start();
    return task;
  }
}

代码示例来源:origin: google/guava

/** Tries to set name of the given {@link Thread}, returns true if successful. */
 @GwtIncompatible // threads
 private static boolean trySetName(final String threadName, Thread currentThread) {
  // In AppEngine, this will always fail. Should we test for that explicitly using
  // MoreExecutors.isAppEngine? More generally, is there a way to see if we have the modifyThread
  // permission without catching an exception?
  try {
   currentThread.setName(threadName);
   return true;
  } catch (SecurityException e) {
   return false;
  }
 }
}

代码示例来源:origin: prestodb/presto

@Override public final void run() {
 String oldName = Thread.currentThread().getName();
 Thread.currentThread().setName(name);
 try {
  execute();
 } finally {
  Thread.currentThread().setName(oldName);
 }
}

代码示例来源:origin: google/guava

/**
 * Creates a thread using {@link #platformThreadFactory}, and sets its name to {@code name} unless
 * changing the name is forbidden by the security manager.
 */
@GwtIncompatible // concurrency
static Thread newThread(String name, Runnable runnable) {
 checkNotNull(name);
 checkNotNull(runnable);
 Thread result = platformThreadFactory().newThread(runnable);
 try {
  result.setName(name);
 } catch (SecurityException e) {
  // OK if we can't set the name in this environment.
 }
 return result;
}

代码示例来源:origin: square/picasso

static void updateThreadName(Request data) {
 String name = data.getName();
 StringBuilder builder = NAME_BUILDER.get();
 builder.ensureCapacity(Utils.THREAD_PREFIX.length() + name.length());
 builder.replace(Utils.THREAD_PREFIX.length(), builder.length(), name);
 Thread.currentThread().setName(builder.toString());
}

代码示例来源:origin: skylot/jadx

public static void check(final IUpdateCallback callback) {
  Runnable run = () -> {
    try {
      Release release = checkForNewRelease();
      if (release != null) {
        callback.onUpdate(release);
      }
    } catch (Exception e) {
      LOG.debug("Jadx update error", e);
    }
  };
  Thread thread = new Thread(run);
  thread.setName("Jadx update thread");
  thread.setPriority(Thread.MIN_PRIORITY);
  thread.start();
}

代码示例来源:origin: crossoverJie/JCSprout

public static void main(String[] args) {
  TwoThread twoThread = new TwoThread();
  Thread t1 = new Thread(new OuNum(twoThread));
  t1.setName("t1");
  Thread t2 = new Thread(new JiNum(twoThread));
  t2.setName("t2");
  t1.start();
  t2.start();
}

代码示例来源:origin: google/guava

@Override
 public Thread newThread(Runnable r) {
  Thread thread = new Thread(r);
  thread.setName(THREAD_NAME);
  thread.setPriority(THREAD_PRIORITY);
  thread.setDaemon(THREAD_DAEMON);
  thread.setUncaughtExceptionHandler(UNCAUGHT_EXCEPTION_HANDLER);
  return thread;
 }
};

代码示例来源:origin: LMAX-Exchange/disruptor

@Override
  public void onShutdown()
  {
    Thread.currentThread().setName(oldName);
  }
}

代码示例来源:origin: LMAX-Exchange/disruptor

@Override
public void onStart()
{
  final Thread currentThread = Thread.currentThread();
  oldName = currentThread.getName();
  currentThread.setName(name);
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void theTest() throws Exception {
 AtomicInteger seq = new AtomicInteger();
 Thread threadA = new Thread(() -> threadA(seq));
 threadA.setName("Thread-A");
 Thread threadB = new Thread(() -> threadB(seq));
 threadB.setName("Thread-B");
 threadA.start();
 threadB.start();
 threadA.join(20 * 1000);
 threadB.join(20 * 1000);
}

相关文章