正如标题所暗示的,我不知道我的程序中怎么会有三个线程?
我的建议是:
(1) 主线程
(2) EDT(因为 JButton)
(3) ????
这是我的代码(非常简单):
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() {
}
}
最佳答案
线程的名称总是有帮助的。您可以通过名称列出所有线程:
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());
}
}
}
对我来说,它列出了:
- 终结器
- 信号调度员
- 主要
- 引用处理程序
编辑:threadSet行取自此处:Get a List of all Threads currently running in Java
关于java - 为什么我的Java程序中突然出现3个线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26673750/