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

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

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

Thread.getId介绍

[英]Returns the identifier of this Thread. The thread ID is a positive long number generated when this thread was created. The thread ID is unique and remains unchanged during its lifetime. When a thread is terminated, this thread ID may be reused.
[中]返回此线程的标识符。线程ID是创建此线程时生成的正长数值。线程ID是唯一的,在其生存期内保持不变。当线程终止时,可以重用该线程ID。

代码示例

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

@Override
public long id() {
  return t.getId();
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Override
  public boolean test(final Thread thread) {
    return thread != null && thread.getId() == threadId;
  }
}

代码示例来源:origin: stackoverflow.com

Thread t = Thread.currentThread();    
int id = t.getId();

代码示例来源:origin: stackoverflow.com

private class MyTask implements Runnable {
  public void run() {
    long threadId = Thread.currentThread().getId();
    logger.debug("Thread # " + threadId + " is doing this task");
  }
}

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

@Override
public void uncaughtException(Thread t, Throwable ex) {
  // if this was an OutOfMemoryError then all bets about logging are off - but in the absence of anything else...
  LOGGER.log(Level.SEVERE,
        "A thread (" + t.getName() + '/' + t.getId()
               + ") died unexpectedly due to an uncaught exception, this may leave your Jenkins in a bad way and is usually indicative of a bug in the code.",
        ex);
}

代码示例来源:origin: apache/rocketmq

public void removeFromWaitingThreadTable() {
    long currentThreadId = Thread.currentThread().getId();
    synchronized (this) {
      this.waitingThreadTable.remove(currentThreadId);
    }
  }
}

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

public int compare(Thread a, Thread b) {
    int result = compare(a.getId(), b.getId());
    if (result == 0)
      result = a.getName().compareToIgnoreCase(b.getName());
    return result;
  }
}

代码示例来源:origin: apache/kafka

/**
 * Acquire the light lock protecting this consumer from multi-threaded access. Instead of blocking
 * when the lock is not available, however, we just throw an exception (since multi-threaded usage is not
 * supported).
 * @throws ConcurrentModificationException if another thread already has the lock
 */
private void acquire() {
  long threadId = Thread.currentThread().getId();
  if (threadId != currentThread.get() && !currentThread.compareAndSet(NO_CURRENT_THREAD, threadId))
    throw new ConcurrentModificationException("KafkaConsumer is not safe for multi-threaded access");
  refcount.incrementAndGet();
}

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

private ThreadSorterBase() {
  ThreadGroup tg = Thread.currentThread().getThreadGroup();
  while (tg.getParent() != null) tg = tg.getParent();
  Thread[] threads = new Thread[tg.activeCount()*2];
  int threadsLen = tg.enumerate(threads, true);
  for (int i = 0; i < threadsLen; i++) {
    ThreadGroup group = threads[i].getThreadGroup();
    map.put(threads[i].getId(), group != null ? group.getName() : null);
  }
}

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

@Override
public RFuture<Void> unlockAsync() {
  long threadId = Thread.currentThread().getId();
  return unlockAsync(threadId);
}

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

@Override
public RFuture<Void> lockAsync(long leaseTime, TimeUnit unit) {
  final long currentThreadId = Thread.currentThread().getId();
  return lockAsync(leaseTime, unit, currentThreadId);
}

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

@Override
public RFuture<Void> unlockAsync() {
  long threadId = Thread.currentThread().getId();
  return unlockAsync(threadId);
}

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

@Override
public RFuture<Boolean> tryLockAsync(long waitTime, long leaseTime, TimeUnit unit) {
  long currentThreadId = Thread.currentThread().getId();
  return tryLockAsync(waitTime, leaseTime, unit, currentThreadId);
}

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

@Override
public RFuture<Void> lockAsync(long leaseTime, TimeUnit unit) {
  final long currentThreadId = Thread.currentThread().getId();
  return lockAsync(leaseTime, unit, currentThreadId);
}

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

@Override
public RFuture<Boolean> tryLockAsync(long waitTime, long leaseTime, TimeUnit unit) {
  long currentThreadId = Thread.currentThread().getId();
  return tryLockAsync(waitTime, leaseTime, unit, currentThreadId);
}

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

@Override
public boolean isHeldByCurrentThread() {
  return isHeldByThread(Thread.currentThread().getId());
}

代码示例来源:origin: ReactiveX/RxJava

@Override
  public void run() {
    if (running != null) {
      running.countDown();
    }
    for (int i = 0; i < numStringsToSend; i++) {
      observer.onNext(Thread.currentThread().getId() + "-" + i);
      if (latch != null) {
        latch.countDown();
      }
      produced.incrementAndGet();
    }
  }
}

代码示例来源:origin: ReactiveX/RxJava

@Override
  public void run() {
    if (running != null) {
      running.countDown();
    }
    for (int i = 0; i < numStringsToSend; i++) {
      subscriber.onNext(Thread.currentThread().getId() + "-" + i);
      if (latch != null) {
        latch.countDown();
      }
      produced.incrementAndGet();
    }
  }
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test(expected=IllegalArgumentException.class)
public void testThreadGroupsByIdFail() throws InterruptedException {
  ThreadUtils.findThreadById(Thread.currentThread().getId(), (String) null);
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testThreadsByIdWrongGroup() throws InterruptedException {
  final Thread t1 = new TestThread("thread1_XXOOLL__");
  final ThreadGroup tg = new ThreadGroup("tg__HHEE22");
  try {
    t1.start();
    assertNull(ThreadUtils.findThreadById(t1.getId(), tg));
  } finally {
    t1.interrupt();
    t1.join();
    tg.destroy();
  }
}

相关文章