0

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() {

                    }
}
Hakan Kiyar
  • 1,199
  • 6
  • 16
  • 26
  • 2
    It's the thread [underpant gnomes](http://upload.wikimedia.org/wikipedia/en/d/dd/Gnomes_plan.png) use to generate profit. :) Joking aside, you shouldn't worry about it, the JVM is free to create as many threads for its own use as it wants. Instead of trying to second-guess what they could be, you can list them all out with [`jvisualvm`](http://docs.oracle.com/javase/7/docs/technotes/tools/share/jvisualvm.html) – biziclop Oct 31 '14 at 11:37
  • 1
    Either use a debugger or some answers here http://stackoverflow.com/questions/1323408/get-a-list-of-all-threads-currently-running-in-java to see the thread names to indicative their purposes. – Tom Oct 31 '14 at 11:39

1 Answers1

2

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

Community
  • 1
  • 1