[英]Why there are suddenly 3 Threads in my Java program?
如標題所示,我不知道程序中可能有三個線程?
我的建議是:
(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行是從這里獲取的 : 獲取當前在Java中運行的所有線程的列表
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.