欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java ThreadGroup 类的使用示例,javathreadgroup,此例子用来演示 Thre

来源: javaer 分享于  点击 106 次 点评:46

java ThreadGroup 类的使用示例,javathreadgroup,此例子用来演示 Thre


此例子用来演示 ThreadGroup 的一些作用

ThreadGroupDemo.java

/**  * Simple demo of ThreadGroup usage. */public class ThreadGroupDemo {  class MyThreadGroup extends ThreadGroup {    public void uncaughtException(Thread t, Throwable ex) {      System.err.println("I caught " + ex);    }    public MyThreadGroup(String name) {      super(name);    }  }  public static void main(String[] args) {    new ThreadGroupDemo().work();  }  protected void work() {    ThreadGroup g = new MyThreadGroup("bulk threads");    Runnable r = new Runnable() {      public void run() {        System.out.println(Thread.currentThread().getName() + " started");        for (int i=0; i<5; i++) {          System.out.println(Thread.currentThread().getName() + ": " + i);          try {            Thread.sleep(1776);          } catch (InterruptedException ex) {            System.out.println("Huh?");          }        }      }    };    // Create and start all the Threads    for (int i = 0; i< 10; i++) {      new Thread(g, r).start();    }    // List them.    Thread[] list = new Thread[g.activeCount()];    g.enumerate(list);    for (int i=0; i<list.length; i++) {      if (list[i] == null)        continue;      Thread t = list[i];      System.out.println(i + ": " + t);    }  }}
相关栏目:

用户点评