Why are threads being created for each component in a GUI program?
I was making a multithreaded gui program and noticed unexpected (at least I never expected this) behavior. Apparently a new thread is created for each new graphical component created. Here is some code and output to demonstrate:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
publicclass gui
{
publicstaticvoid main(String[] args)
{
System.out.println("thread count at beginning of main: " +Thread.activeCount());
JButton create_button =new JButton("Create!");
JButton about_button =new JButton("About...");
JButton quit_button =new JButton("Quit");
System.out.println("thread count after making 3 buttons: " +Thread.activeCount());
Frame frame =new JFrame();
System.out.println("thread count after constructing JFrame: " +Thread.activeCount());
frame.setSize(100,100);
frame.setVisible(true);
System.out.println("thread count after making JFrame visible: " +Thread.activeCount());
System.out.println("thread count at end of main: " +Thread.activeCount());
}
}
and the output from that code is this:
thread count at beginning of main: 1
thread count after making 3 buttons: 4
thread count after constructing JFrame: 4
thread count after making JFrame visible: 5
thread count at end of main: 5
So I was wondering if anyone could explain why new threads are created, or give me a link to a site where I could learn more about this phenomena. (Also this is my first attempt to distribute duke dollars)

