JAVA GUI 多线程 验证

来源:互联网 发布:在线监测设施数据造假 编辑:程序博客网 时间:2024/04/29 03:03
     线程在网络编程中的一个重要功能就是监听某个通信接口。因此想到在JAVA 的界面设计编程,即GUI设计中必定涉及到监听,比如对键盘和鼠标动作的监听。那么GUI程序运行时应该是多线程的。于是写出验证代码如下 。
  1. public class GUIThreadsDemo {
  2.     public GUIThreadsDemo() {
  3.         JFrame jf = new JFrame("GUIThreadsDemo");
  4.         jf.setSize(160,90);
  5.         jf.setVisible(true);
  6.     }
  7.     
  8.     public static void main(String[] args) {
  9.         GUIThreadsDemo g = new GUIThreadsDemo();
  10.         showThreads();
  11.     }
  12.     private static void showThreads() {
  13.         //当前活动的线程数目
  14.         int num = Thread.activeCount();
  15.         //根据当前的线程数目初始化一个线程数组
  16.         Thread[] tg = new Thread[num];
  17.         //将当前线程的线程组及其子组中的每一个活动线程复制到指定的数组<code>tg</code>中
  18.         Thread.enumerate(tg);
  19.         //打印信息
  20.         System.out.println("The number of the currentThread = " + num);
  21.         System.out.println("The Name of the currentThread : ");
  22.         for(int i = 0 ; i < tg.length; i++) {
  23.             System.out.println(tg[i].getName());
  24.         }
  25.     }
  26. }

程序运行结果如下:
  1. The number of the currentThread = 4
  2. The Name of the currentThread : 
  3. main
  4. AWT-Shutdown
  5. AWT-Windows
  6. AWT-EventQueue-0
其中 线程main是必须的
而AWT-Shutdown 是监听窗口关闭动作的  ; AWT-Windows 是系统接收动作信息用到 ; AWT-EventQueue-0 用于处理信息
其中的机制仍需要我出继续深入学习。努力。

结论:GUI程序必定涉及到多线程,在我的这个程序中,只是简单的启动了一个Frame窗口,是最简单的GUI程序了。