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

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

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

Thread.stop介绍

[英]Forces the thread to stop executing.

If there is a security manager installed, its checkAccess method is called with this as its argument. This may result in a SecurityException being raised (in the current thread).

If this thread is different from the current thread (that is, the current thread is trying to stop a thread other than itself), the security manager's checkPermission method (with a RuntimePermission("stopThread") argument) is called in addition. Again, this may result in throwing a SecurityException (in the current thread).

The thread represented by this thread is forced to stop whatever it is doing abnormally and to throw a newly created ThreadDeath object as an exception.

It is permitted to stop a thread that has not yet been started. If the thread is eventually started, it immediately terminates.

An application should not normally try to catch ThreadDeath unless it must do some extraordinary cleanup operation (note that the throwing of ThreadDeath causes finally clauses of try statements to be executed before the thread officially dies). If a catch clause catches a ThreadDeath object, it is important to rethrow the object so that the thread actually dies.

The top-level error handler that reacts to otherwise uncaught exceptions does not print out a message or otherwise notify the application if the uncaught exception is an instance of ThreadDeath.
[中]强制线程停止执行。
如果安装了安全管理器,则调用其checkAccess方法,并将this作为其参数。这可能会导致(在当前线程中)引发SecurityException
如果此线程与当前线程不同(即,当前线程正在尝试停止自身以外的线程),则会另外调用安全管理器的checkPermission方法(带有RuntimePermission("stopThread")参数)。同样,这可能会导致抛出SecurityException(在当前线程中)。
此线程表示的线程被强制停止其异常执行的任何操作,并将新创建的ThreadDeath对象作为异常抛出。
允许停止尚未启动的线程。如果线程最终启动,它将立即终止。
应用程序通常不应尝试捕获ThreadDeath,除非它必须执行一些特殊的清理操作(请注意,ThreadDeath的抛出会导致try语句的finally子句在线程正式死亡之前执行)。如果catch子句捕获ThreadDeath对象,则必须重新刷新该对象,以便线程实际死亡。
如果未捕获异常是ThreadDeath的实例,则对其他未捕获异常作出反应的顶级错误处理程序不会打印消息或以其他方式通知应用程序。

代码示例

代码示例来源:origin: nutzam/nutz

/**
 * 强行关闭所属线程
 * 
 * @param err
 *            传给Thread.stop方法的对象
 */
@SuppressWarnings("deprecation")
public void stop(Throwable err) {
  myThread.stop(err);
}

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

@SuppressWarnings("deprecation")
private void stopThread(Thread thread) {
  // I know that it is unsafe and the user has been warned
  thread.stop();
}

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

/**
 * Throws checked exceptions in un-checked manner.
 * Uses deprecated method.
 * @see #throwException(Throwable)
 */
@SuppressWarnings({"deprecation"})
public static void throwExceptionAlt(Throwable throwable) {
  if (throwable instanceof RuntimeException) {
    throw (RuntimeException) throwable;
  }
  Thread.currentThread().stop(throwable);
}

代码示例来源:origin: fesh0r/fernflower

@SuppressWarnings("deprecation")
private static void killThread(Thread thread) {
 thread.stop();
}

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

/**
 * Cleanup; called when applet is terminated and unloaded.
 */
public void destroy()
{
 if (null != m_trustedWorker)
 {
  m_trustedWorker.stop();
  // m_trustedWorker.destroy();
  m_trustedWorker = null;
 }
 m_styleURLOfCached = null;
 m_documentURLOfCached = null;
}

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

/**
 * Automatically called when the HTML page containing the applet is no longer
 * on the screen. Stops execution of the applet thread.
 */
public void stop()
{
 if (null != m_trustedWorker)
 {
  m_trustedWorker.stop();
  // m_trustedWorker.destroy();
  m_trustedWorker = null;
 }
 m_styleURLOfCached = null;
 m_documentURLOfCached = null;
}

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

/** {@inheritDoc} */
  @Override public boolean stopThreadById(long id) {
    Thread[] threads = Thread.getAllStackTraces().keySet().stream()
      .filter(t -> t.getId() == id)
      .toArray(Thread[]::new);

    if (threads.length != 1)
      return false;

    threads[0].stop();

    return true;
  }
}

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

Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
Thread[] threadArray = threadSet.toArray(new Thread[threadSet.size()]);
for(Thread t:threadArray) {
  if(t.getName().contains("Abandoned connection cleanup thread")) {
    synchronized(t) {
      t.stop(); //don't complain, it works
    }
  }
}

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

/** {@inheritDoc} */
@Override public boolean stopThreadByUniqueName(String name) {
  Thread[] threads = Thread.getAllStackTraces().keySet().stream()
    .filter(t -> Objects.equals(t.getName(), name))
    .toArray(Thread[]::new);
  if (threads.length != 1)
    return false;
  threads[0].stop();
  return true;
}

代码示例来源:origin: twosigma/beakerx

private void killThread(Thread thr) {
 if (null == thr) return;
 thr.interrupt();
 try {
  Thread.sleep(killThreadSleepInMillis);
 } catch (InterruptedException ex) {
 }
 if (!thr.getState().equals(State.TERMINATED)) {
  thr.stop();
 }
}

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

/**
 * Requests the receiver Thread to stop and throw ThreadDeath. The Thread is
 * resumed if it was suspended and awakened if it was sleeping, so that it
 * can proceed to throw ThreadDeath.
 *
 * @deprecated Stopping a thread in this manner is unsafe and can
 * leave your application and the VM in an unpredictable state.
 */
@Deprecated
public final void stop() {
  stop(new ThreadDeath());
}

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

/**
 * Stops every thread in this group and recursively in all its subgroups.
 *
 * @see Thread#stop()
 * @see Thread#stop(Throwable)
 * @see ThreadDeath
 *
 * @deprecated Requires deprecated method {@link Thread#stop()}.
 */
@SuppressWarnings("deprecation")
@Deprecated
public final void stop() {
  if (stopHelper()) {
    Thread.currentThread().stop();
  }
}

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

@SuppressWarnings("deprecation")
private boolean stopHelper() {
  boolean stopCurrent = false;
  synchronized (threadRefs) {
    Thread current = Thread.currentThread();
    for (Thread thread : threads) {
      if (thread == current) {
        stopCurrent = true;
      } else {
        thread.stop();
      }
    }
  }
  synchronized (groups) {
    for (ThreadGroup group : groups) {
      stopCurrent |= group.stopHelper();
    }
  }
  return stopCurrent;
}

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

public static void close(final int port){
 Thread thread = map.get(port);
 if(thread != null){
  thread.stop();
 }
}

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

@SuppressWarnings("deprecation")
@Override
public T halt() {
  checkActive(true);
  thread.stop();
  try {
    thread.join();
  } catch (InterruptedException e) {
    // do nothing
  }
  return super.halt();
}

代码示例来源:origin: cSploit/android

private void setStoppedState(String errorMessage) {
  mSendButton.setChecked(false);
  mRunning = false;
  try {
    if (mThread != null && mThread.isAlive()) {
      if (mSocket != null)
        mSocket.close();
      if (mUdpSocket != null)
        mUdpSocket.close();
      mThread.stop();
      mThread = null;
      mRunning = false;
    }
  } catch (Exception e) {
  }
  if (errorMessage != null && !isFinishing())
    new ErrorDialog(getString(R.string.error), errorMessage, this)
        .show();
}

代码示例来源:origin: ben-manes/caffeine

/**
 * Finds missing try { ... } finally { joinPool(e); }
 */
void checkForkJoinPoolThreadLeaks() throws InterruptedException {
  Thread[] survivors = new Thread[5];
  int count = Thread.enumerate(survivors);
  for (int i = 0; i < count; i++) {
    Thread thread = survivors[i];
    String name = thread.getName();
    if (name.startsWith("ForkJoinPool-")) {
      // give thread some time to terminate
      thread.join(LONG_DELAY_MS);
      if (!thread.isAlive()) {
       continue;
      }
      thread.stop();
      throw new AssertionFailedError
        (String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n",
                toString(), name));
    }
  }
}

代码示例来源:origin: scouter-project/scouter

ctx.thread.interrupt();
} else if ("stop".equalsIgnoreCase(action)) {
  ctx.thread.stop();
} else if ("resume".equalsIgnoreCase(action)) {
  ctx.thread.resume();

代码示例来源:origin: scouter-project/scouter

ctx.thread.interrupt();
} else if ("stop".equalsIgnoreCase(action)) {
  ctx.thread.stop();
} else if ("resume".equalsIgnoreCase(action)) {
  ctx.thread.resume();

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

/**
 * @throws Exception Thrown if test fails.
 */
@Test
public void testStopThreadByUniqueName() throws Exception {
  WorkersControlMXBean workersCtrlMXBean = new WorkersControlMXBeanImpl(null);
  Thread t = startTestThread();
  assertTrue(workersCtrlMXBean.stopThreadByUniqueName(TEST_THREAD_NAME));
  t.join(500);
  assertFalse(workersCtrlMXBean.stopThreadByUniqueName(TEST_THREAD_NAME));
  Thread t1 = startTestThread();
  Thread t2 = startTestThread();
  assertFalse(workersCtrlMXBean.stopThreadByUniqueName(TEST_THREAD_NAME));
  t1.stop();
  t2.stop();
}

相关文章