简体   繁体   中英

Why there are suddenly 3 Threads in my Java program?

as the title implies I do not know how there could be three threads in my program?

My suggestion is:

(1) main-Thread

(2) EDT (because of JButton)

(3) ????

Here is my Code (it is very simple):

package newProject;

import javax.swing.JButton;

public class MyExample {

    public static void main(String[] args) {

        System.out.println(Thread.activeCount() + " " + Thread.currentThread());
        MyThread myExample = new MyThread();
        System.out.println(Thread.activeCount() + " " + Thread.currentThread());
    }

}

class MyThread {

    JButton button=new JButton();

                    public MyThread() {

                    }
}

The name of a thread is always helpful. You can list all threads by name via:

import java.util.*;

public class ListThreads {

     public static void main(String []args){
        Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
        for (Thread t : threadSet) {
            System.out.println (t.getName());
        }
     }
}

For me it lists:

  • Finalizer
  • Signal Dispatcher
  • main
  • Reference Handler

EDIT: The threadSet line was taken from here: Get a List of all Threads currently running in Java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM