友情提示:如果本网页打开太慢或显示不完整,请尝试鼠标右键“刷新”本网页!阅读过程发现任何错误请告诉我们,谢谢!! 报告错误
一世书城 返回本书目录 我的书架 我的书签 TXT全本下载 进入书吧 加入书签

Java编程思想第4版[中文版](PDF格式)-第169章

按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!




下面这个例子展示了位于一个“叶子组”内的线程能修改它所在线程组树的所有线程的优先级,同时还能为 

这个“树”内的所有线程都调用一个方法。  

  

①:《The Java Programming Language》第 179 页。该书由 Arnold 和 Jams Gosling 编著,Addison…Wesley 

于 1996 年出版  

  

//: TestAccess。java  

// How threads can access other threads  

// in a parent thread group  

  

public class TestAccess {  

  public static void main(String'' args) {  

    ThreadGroup   

      x = new ThreadGroup(〃x〃);  

      y = new ThreadGroup(x; 〃y〃);  

      z = new ThreadGroup(y; 〃z〃);  

    Thread  

      one = new TestThread1(x; 〃one〃);  

      two = new TestThread2(z; 〃two〃);  

  }  

}  

  

class TestThread1 extends Thread {  

  private int i;  

  TestThread1(ThreadGroup g; String name) {  

    super(g; name);  

  }  

  void f() {  

    i++; // modify this thread  

    System。out。println(getName() + 〃 f()〃);  

  }  

}  

  



                                                                                     525 


…………………………………………………………Page 527……………………………………………………………

class TestThread2 extends TestThread1 {  

  TestThread2(ThreadGroup g; String name) {  

    super(g; name);  

    start();  

  }  

  public void run() {  

    ThreadGroup g =  

      getThreadGroup()。getParent()。getParent();  

    g。list();  

    Thread'' gAll = new Thread'g。act iveCount()';  

    g。enumerate(gAll);  

    for(int i = 0; i 《 gAll。length; i++) {  

      gAll'i'。setPriority(Thread。MIN_PRIORITY);  

      ((TestThread1)gAll'i')。f();  

    }  

    g。list();  

  }  

} ///:~  

  

在main()中,我们创建了几个ThreadGroup (线程组),每个都位于不同的“叶”上:x 没有参数,只有它 

的名字(一个String),所以会自动进入“system”(系统)线程组;y 位于x 下方,而 z位于y 下方。注 

意初始化是按照文字顺序进行的,所以代码合法。  

有两个线程创建之后进入了不同的线程组。其中,TestThread1 没有一个 run()方法,但有一个f(),用于通 

知线程以及打印出一些东西,以便我们知道它已被调用。而TestThread2 属于TestThread1 的一个子类,它 

的run()非常详尽,要做许多事情。首先,它获得当前线程所在的线程组,然后利用getParent()在继承树中 

向上移动两级(这样做是有道理的,因为我想把TestThread2 在分级结构中向下移动两级)。随后,我们调 

用方法 activeCount() ,查询这个线程组以及所有子线程组内有多少个线程,从而创建由指向Thread 的句柄 

构成的一个数组。enumerate()方法将指向所有这些线程的句柄置入数组 gAll 里。然后在整个数组里遍历, 

为每个线程都调用 f()方法,同时修改优先级。这样一来,位于一个“叶子”线程组里的线程就修改了位于 

父线程组的线程。  

调试方法 list()打印出与一个线程组有关的所有信息,把它们作为标准输出。在我们对线程组的行为进行调 

查的时候,这样做是相当有好处的。下面是程序的输出:  

  

java。lang。ThreadGroup'name=x;maxpri=10'  

    Thread'one;5;x'  

    java。lang。ThreadGroup'name=y;maxpri=10'  

        java。lang。ThreadGroup'name=z;maxpri=10'  

            Thread'two;5;z'  

one f()  

two f()  

java。lang。ThreadGroup'name=x;maxpri=10'  

    Thread'one;1;x'  

    java。lang。ThreadGroup'name=y;maxpri=10'  

        java。lang。ThreadGroup'name=z;maxpri=10'  

            Thread'two;1;z'  

  

list()不仅打印出 ThreadGroup 或者Thread 的类名,也打印出了线程组的名字以及它的最高优先级。对于线 

程,则打印出它们的名字,并接上线程优先级以及所属的线程组。注意 list()会对线程和线程组进行缩排处 

理,指出它们是未缩排的线程组的“子”。  

大家可看到 f()是由TestThread2 的run()方法调用的,所以很明显,组内的所有线程都是相当脆弱的。然 

而,我们只能访问那些从自己的system 线程组树分支出来的线程,而且或许这就是所谓“安全”的意思。我 

们不能访问其他任何人的系统线程树。  



                                                                                         526 


…………………………………………………………Page 528……………………………………………………………

  

1。 线程组的控制  

抛开安全问题不谈,线程组最有用的一个地方就是控制:只需用单个命令即可完成对整个线程组的操作。下 

面这个例子演示了这一点,并对线程组内优先级的限制进行了说明。括号内的注释数字便于大家比较输出结 

果:  

  

//: ThreadGroup1。java  

// How thread groups control priorities  

// of the threads inside them。  

  

public class ThreadGroup1 {  

  public static void main(String'' args) {  

    // Get the system thread & print its Info:  

    ThreadGroup sys =   

      Thread。currentThread()。getThreadGroup();  

    sys。list(); // (1)  

    // Reduce the system thread group priority:  

    sys。setMaxPriority(Thread。MAX_PRIORITY 1);  

    // Increase the main thread priority:  

    Thread curr = Thread。currentThread();  

    curr。setPriority(curr。getPriority() + 1);  

    sys。list(); // (2)  

    // Attempt to set a new group to the max:  

    ThreadGroup g1 = new ThreadGroup(〃g1〃);  

    g1。setMaxPriority(Thread。MAX_PRIORITY);  

    // Attempt to set a new thread to the max:  

    Thread t = new Thread(g1; 〃A〃);  

    t。setPriority(Thread。MAX_PRIORITY);  

    g1。list(); // (3)  

    // Reduce g1's max priority; then attempt  

    // to increase it:  

    g1。setMaxPriority(Thread。MAX_PRIORITY 2);  

    g1。setMaxPriority(Thread。MAX_PRIORITY);  

    g1。list(); // (4)  

    // Attempt to set a new thread to the max:  

    t = new Thread(g1; 〃B〃);  

    t。setPriority(Thread。MAX_PRIORITY);  

    g1。list(); // (5)  

    // Lower the max priority below the default  

    // thread priority:  

    g1。setMaxPriority(Thread。MIN_PRIORITY + 2);  

    // Look at a new thread's priority before  

    // and after changing it:  

    t = new Thread(g1; 〃C〃);  

    g1。list(); // (6)  

    t。setPriority(t。getPriority() …1);  

    g1。list(); // (7)  

    // Make g2 a child Threadgroup of g1 and  

    // try to increase its priority:  

    ThreadGroup g2 = new ThreadGroup(g1; 〃g2〃);  

    g2。list(); // (8)  

    g2。setMaxPriority(Thread。MAX_PRIORITY);  



                                                                                          527 


…………………………………………………………Page 529……………………………………………………………

    g2。list(); // (9)  

    // Add a bunch of new threads to g2:  

    for (int i = 0; i 《 5; i++)  

      new Thread(g2; Integer。toString(i));  

    // Show information about all threadgroups  

    // and threads:  

    sys。list(); // (10)  

    System。out。println(〃Starting all threads:〃);  

    Thread'' all = new Thread'sys。activeCount()';  

    sys。enumerate(all);  

    for(int i = 0; i 《 all。length; i++)  

      if(!all'i'。isAlive())  

        all'i'。start();  

    // Suspends & Stops all threads in   

    // this group and its subgroups:  

    System。out。println(〃All threads started〃);  

    sys。suspend(); // Deprecated in Java 1。2  

    // Never gets here。。。  

    System。out。println(〃All threads suspended〃);  

    sys。stop(); // Deprecated in Java 1。2  

    System。out。println(〃All threads stopped〃);  

  }  

} ///:~  

  

下面的输出结果已进行了适当的编辑,以便用一页能够装下(java。lang。已被删去),而且添加了适当的数 

字,与前面程序列表中括号里的数字对应:  

  

(1) ThreadGroup'name=system;maxpri=10'  

      Thread'main;5;system'  

(2) ThreadGroup'name=system;maxpri=9'  

      Thread'main;6;system'  

(3) ThreadGroup'name=g1;maxpri=9'  

      Thread'A;9;g1'  

(4) ThreadGroup'name=g1;maxpri=8'  

      Thread'A;9;g 1'  

(5) ThreadGroup'name=g1;maxpri=8'  

      Thread'A;9;g1'  

      Thread'B;8;g1'  

(6) ThreadGroup'name=g1;maxpri=3'  

      Thread'A;9;g1'  

      Thread'B;8;g1'  

      Thread'C;6;g1'  

(7) ThreadGroup'name=g1;maxpri=3'  

      Thread'A;9;g1'  

      Thread'B;8;g1'  

      Thread'C;3;g1'  

(8) ThreadGroup'name=g2;maxpri=3'  

(9) ThreadGroup'name=g2;maxpri=3'  

(10)ThreadGroup'name=system;maxpri=9'  

      Thread'main;6;system'  

      ThreadGroup'name=g1;maxpri=3'  

        Thread'A;9;g1'  



                                                                                           528 


…………………………………………………………Page 530……………………………………………………………

        Thread'B;8;g1'  

返回目录 上一页 下一页 回到顶部 0 0
未阅读完?加入书签已便下次继续阅读!
温馨提示: 温看小说的同时发表评论,说出自己的看法和其它小伙伴们分享也不错哦!发表书评还可以获得积分和经验奖励,认真写原创书评 被采纳为精评可以获得大量金币、积分和经验奖励哦!