相关疑难解决方法(0)

获取当前在Java中运行的所有线程的列表

有没有什么办法可以获得当前JVM中所有正在运行的Thread的列表(包括我的类未启动的Threads)?

是否也可以在列表中获取所有Thread的Thread和Class对象?

我希望能够通过代码完成此操作.

java multithreading jvm

221
推荐指数
10
解决办法
19万
查看次数

如何从线程中捕获异常

我有Java主类,在类中,我启动一个新线程,在主,它等待直到线程死亡.在某些时刻,我从线程中抛出一个运行时异常,但是我无法捕获从主类中的线程抛出的异常.

这是代码:

public class Test extends Thread
{
  public static void main(String[] args) throws InterruptedException
  {
    Test t = new Test();

    try
    {
      t.start();
      t.join();
    }
    catch(RuntimeException e)
    {
      System.out.println("** RuntimeException from main");
    }

    System.out.println("Main stoped");
  }

  @Override
  public void run()
  {
    try
    {
      while(true)
      {
        System.out.println("** Started");

        sleep(2000);

        throw new RuntimeException("exception from thread");
      }
    }
    catch (RuntimeException e)
    {
      System.out.println("** RuntimeException from thread");

      throw e;
    } 
    catch (InterruptedException e)
    {

    }
  }
}
Run Code Online (Sandbox Code Playgroud)

谁知道为什么?

java multithreading

153
推荐指数
7
解决办法
16万
查看次数

标签 统计

java ×2

multithreading ×2

jvm ×1