为什么第一个Thread.activeCount()在我的代码中返回1而另一个返回2?

时间:2014-04-17 18:14:22

标签: java

这是我的申请:

public class NamedThread extends Thread {
    /* This will store name of the thread */

    String name;

    @Override
    public void run() {
//Will store the number of threads
        int count = 0;
        while (count <= 3) {
//Display the number of threads
            System.out.println(Thread.activeCount());
//Display the name of the currently running thread
            name = Thread.currentThread().getName();
            count++;
            System.out.println(name);
            if (name.equals("Thread1")) {
                System.out.println("Marimba");
            } else {
                System.out.println("Jini");
            }
        }
    }

    public static void main(String args[]) {
        NamedThread objNamedThread = new NamedThread();
        objNamedThread.setName("Thread1");
//Display the status of the thread, whether alive or not
        System.out.println(Thread.currentThread().isAlive());
        System.out.println(objNamedThread.isAlive());
        /*invokes the start method which in turn will call
         * run and begin thread execution
         */
        objNamedThread.start();
        System.out.println(Thread.currentThread().isAlive());
        System.out.println(objNamedThread.isAlive());
    }
}

输出是:

true
false
true
true
1
Thread1
Marimba
2
Thread1
Marimba
2
Thread1
Marimba
2
Thread1
Marimba

我的申请有什么问题?为什么?谢谢!

3 个答案:

答案 0 :(得分:0)

public static void main(String[] args) {
    System.out.println(Thread.activeCount());

    //doSomething()
}

上面的代码输出2

GC占用一个线程,所以你得到2而不是1

此功能专为调试而设计

如果你在调试模式下运行,你会得到1

答案 1 :(得分:0)

InteliJ生成另一个线程,称为Monitor Ctrl-Break线程。

以下代码(摘自https://www.tutorialspoint.com/java/lang/thread_activecount.htm):

public static void main(String[] args) {

            Thread t = Thread.currentThread();
            t.setName("Admin Thread");

            // set thread priority to 1
            t.setPriority(1);

            // prints the current thread
            System.out.println("Thread = " + t);

            int count = Thread.activeCount();
            System.out.println("currently active threads = " + count);

            Thread th[] = new Thread[count];
            // returns the number of threads put into the array
            Thread.enumerate(th);

            // prints active threads
        for (int i = 0; i < count; i++) {
            System.out.println(i + ": " + th[i]);
        }
    }

将在inteliJ中生成以下输出:

Thread = Thread[Admin Thread,1,main]
currently active threads = 2
0: Thread[Admin Thread,1,main]
1: Thread[Monitor Ctrl-Break,5,main]

您可以在调试中运行它,并注意未创建此线程。因此,仅在正常运行期间添加该线程。

答案 2 :(得分:0)

如果我们打印所有线程,则可以观察到默认线程组是 System ,并且在系统内部,有不同的线程和组,例如:

Group[system:class java.lang.ThreadGroup]
 Reference
  Finalizer
  Signal Dispatcher
  Group[main:class java.lang.ThreadGroup]
     main
     Monitor
  Group[InnocuousThreadGroup:class java.lang.ThreadGroup]
    Common-Cleaner

在Java代码中,默认线程组为main。当我们打印活动线程数时,它将给出答案2,因为主线程组有两个线程main和monitor。