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

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

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

Thread.getState介绍

[英]Returns the state of this thread. This method is designed for use in monitoring of the system state, not for synchronization control.
[中]返回此线程的状态。此方法设计用于监视系统状态,而不是用于同步控制。

代码示例

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

@Override
public State state() {
  return t.getState();
}

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

@Override
public State state() {
  return t.getState();
}

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

boolean isWaiting(Thread thread) {
 switch (thread.getState()) {
  case BLOCKED:
  case WAITING:
  case TIMED_WAITING:
   return true;
  default:
   return false;
 }
}

代码示例来源:origin: Alluxio/alluxio

/**
 * @param thread a thread
 * @return a human-readable representation of the thread's stack trace
 */
public static String formatStackTrace(Thread thread) {
 Throwable t = new Throwable(String.format("Stack trace for thread %s (State: %s):",
   thread.getName(), thread.getState()));
 t.setStackTrace(thread.getStackTrace());
 StringWriter sw = new StringWriter();
 t.printStackTrace(new PrintWriter(sw));
 return sw.toString();
}

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

public Thread.State state()
{
  return thread.getState();
}

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

static void awaitWaiting(Thread t) {
 while (true) {
  Thread.State state = t.getState();
  switch (state) {
   case RUNNABLE:
   case BLOCKED:
    Thread.yield();
    break;
   case WAITING:
    return;
   default:
    throw new AssertionError("unexpected state: " + state);
  }
 }
}

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

/** Wait for the given thread to reach the {@link State#TIMED_WAITING} thread state. */
void awaitTimedWaiting(Thread thread) {
 while (true) {
  switch (thread.getState()) {
   case BLOCKED:
   case NEW:
   case RUNNABLE:
   case WAITING:
    Thread.yield();
    break;
   case TIMED_WAITING:
    return;
   case TERMINATED:
   default:
    throw new AssertionError();
  }
 }
}

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

private static void awaitBlockedOn(Thread t, Object blocker) throws InterruptedException {
 while (!isThreadBlockedOn(t, blocker)) {
  if (t.getState() == Thread.State.TERMINATED) {
   throw new RuntimeException("Thread " + t + " exited unexpectedly");
  }
  Thread.sleep(1);
 }
}

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

@Override
public boolean test( Thread thread )
{
  State threadState = thread.getState();
  seenStates.add( threadState );
  return possibleStates.contains( threadState );
}

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

private static boolean isThreadBlockedOn(Thread t, Object blocker) {
 return t.getState() == Thread.State.WAITING && LockSupport.getBlocker(t) == blocker;
}

代码示例来源:origin: CarGuo/GSYVideoPlayer

private synchronized void readSourceAsync() throws ProxyCacheException {
  boolean readingInProgress = sourceReaderThread != null && sourceReaderThread.getState() != Thread.State.TERMINATED;
  if (!stopped && !cache.isCompleted() && !readingInProgress) {
    sourceReaderThread = new Thread(new SourceReaderRunnable(), "Source reader for " + source);
    sourceReaderThread.start();
  }
}

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

@Override
public String toString()
{
  Thread thread = this.thread;
  return format( "%s[%s,state=%s]", getClass().getSimpleName(), name,
          thread == null ? "dead" : thread.getState() );
}

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

void joinSuccessfully(long timeoutMillis) {
 Uninterruptibles.joinUninterruptibly(thread, timeoutMillis, MILLISECONDS);
 completed.assertCompletionExpected();
 assertEquals(Thread.State.TERMINATED, thread.getState());
}

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

void joinSuccessfully() {
 Uninterruptibles.joinUninterruptibly(thread);
 completed.assertCompletionExpected();
 assertEquals(Thread.State.TERMINATED, thread.getState());
}

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

@Override
public boolean test( Thread thread )
{
  ReflectionUtil.verifyMethodExists( owner, method );
  if ( thread.getState() == Thread.State.WAITING || thread.getState() == Thread.State.TIMED_WAITING )
  {
    for ( StackTraceElement element : thread.getStackTrace() )
    {
      if ( element.getClassName().equals( owner.getName() ) && element.getMethodName().equals( method ) )
      {
        return true;
      }
    }
  }
  return false;
}

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

/**
 * Waits for the specified time (in milliseconds) for the thread to terminate (using {@link
 * Thread#join(long)}), else interrupts the thread (in the hope that it may terminate later) and
 * fails.
 */
void awaitTermination(Thread t, long timeoutMillis) {
 try {
  t.join(timeoutMillis);
 } catch (InterruptedException ie) {
  threadUnexpectedException(ie);
 } finally {
  if (t.getState() != Thread.State.TERMINATED) {
   t.interrupt();
   fail("Test timed out");
  }
 }
}

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

/**
 * Spin-waits up to the specified number of milliseconds for the given thread to enter a wait
 * state: BLOCKED, WAITING, or TIMED_WAITING.
 */
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) {
 long startTime = System.nanoTime();
 for (; ; ) {
  Thread.State s = thread.getState();
  if (s == Thread.State.BLOCKED || s == Thread.State.WAITING || s == Thread.State.TIMED_WAITING)
   return;
  else if (s == Thread.State.TERMINATED) fail("Unexpected thread termination");
  else if (millisElapsedSince(startTime) > timeoutMillis) {
   threadAssertTrue(thread.isAlive());
   return;
  }
  Thread.yield();
 }
}

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

void joinUnsuccessfully(long timeoutMillis) {
  Uninterruptibles.joinUninterruptibly(thread, timeoutMillis, MILLISECONDS);
  completed.assertCompletionNotExpected(timeoutMillis);
  assertFalse(Thread.State.TERMINATED.equals(thread.getState()));
 }
}

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

@Test
public void testCloseWorkerPool() throws Exception {
 String poolName = "vert.x-" + TestUtils.randomAlphaString(10);
 AtomicReference<Thread> thread = new AtomicReference<>();
 WorkerExecutor worker1 = vertx.createSharedWorkerExecutor(poolName);
 WorkerExecutor worker2 = vertx.createSharedWorkerExecutor(poolName);
 worker1.executeBlocking(fut -> {
  thread.set(Thread.currentThread());
 }, ar -> {
 });
 assertWaitUntil(() -> thread.get() != null);
 worker1.close();
 assertNotSame(thread.get().getState(), Thread.State.TERMINATED);
 worker2.close();
 assertWaitUntil(() -> thread.get().getState() == Thread.State.TERMINATED);
}

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

public void testThreadFactory() throws InterruptedException {
 final String THREAD_NAME = "ludicrous speed";
 final int THREAD_PRIORITY = 1;
 final boolean THREAD_DAEMON = false;
 ThreadFactory backingThreadFactory =
   new ThreadFactory() {
    @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;
    }
   };
 Thread thread =
   builder.setThreadFactory(backingThreadFactory).build().newThread(monitoredRunnable);
 assertEquals(THREAD_NAME, thread.getName());
 assertEquals(THREAD_PRIORITY, thread.getPriority());
 assertEquals(THREAD_DAEMON, thread.isDaemon());
 assertSame(UNCAUGHT_EXCEPTION_HANDLER, thread.getUncaughtExceptionHandler());
 assertSame(Thread.State.NEW, thread.getState());
 assertFalse(completed);
 thread.start();
 thread.join();
 assertTrue(completed);
}

相关文章