java 线程组

http://blog.csdn.net/xubo578/article/details/5472164

线程组
Java中每一个线程都归属于某个线程组管理的一员,例如在主函数main()主工作流程中产生一个线程,则产生的线程属于main这个线程组管理的一员。简单地说,线程组就是由线程组成的管理线程的类,这个类是java.lang.ThreadGroup类。
可以通过使用如下代码获取此线程所属线程组的名称。
Thread.currentThread().getThreadGroup().getName();

currentThread():取得当前线程。
getThreadGroup()
:取得当前线程所在的组。
getName()
:取得组的名称。
定义一个线程组,通过以下代码可以实现。
ThreadGroup group=newThreadGroup("group");

Thread thread=new Thread(group,"the firstthread of group");
ThreadGroup类中的某些方法,可以对线程组中的线程产生作用。例如,setMaxPriority()方法可以设定线程组中的所有线程拥有最大的优先权。

所有线程都隶属于一个线程组。那可以是一个默认线程组,亦可是一个创建线程时明确指定的组。在创建之初,线程被限制到一个组里,而且不能改变到一个不同的组。每个应用都至少有一个线程从属于系统线程组。若创建多个线程而不指定一个组,它们就会自动归属于系统线程组。
  线程组也必须从属于其他线程组。必须在构建器里指定新线程组从属于哪个线程组。若在创建一个线程组的时候没有指定它的归属,则同样会自动成为系统线程组的一名属下。因此,一个应用程序中的所有线程组最终都会将系统线程组作为自己的“父”。
  之所以要提出“线程组”的概念,很难从字面上找到原因。这多少为我们讨论的主题带来了一些混乱。一般地说,我们认为是由于“ 安全 ”或者“保密”方面的理由才使用线程组的。根据 Arnold Gosling 的说法:“线程组中的线程可以修改组内的其他线程,包括那些位于分层结构最深处的。一个线程不能修改位于自己所在组或者下属组之外的任何线程”(注释①)。然而,我们很难判断“修改”在这儿的具体含义是什么。下面这个例子展示了位于一个“叶子组”内的线程能修改它所在线程组树的所有线程的优先级,同时还能为这个“树”内的所有线程都调用一个方法。
  ①:《 The   Java   ProgrammingLanguage 》第 179 页。该书由 Arnold JamsGosling 编著, 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()");
}
}
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 = newThread[g.activeCount()];
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 线程组树分支出来的线程,而且或许这就是所谓“ 安全 ”的意思。我们不能访问其他任何人的系统线程树。
线程组的控制
  抛开安全问题不谈,线程组最有用的一个地方就是控制:只需用单个命令即可完成对整个线程组的操作。下面这个例子演示了这一点,并对线程组内优先级的限制进行了说明。括号内的注释数字便于大家比较输出结果:
//: 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 itsInfo:
ThreadGroup sys =  
Thread.currentThread().getThreadGroup();
sys.list(); // (1)
// Reduce the system thread grouppriority:
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 = newThreadGroup("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 thedefault
// 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);
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 allthreads:");
Thread[] all = newThread[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 threadsstarted");
sys.suspend(); // Deprecated in   Java   1.2
// Never gets here...
System.out.println("All threadssuspended");
sys.stop(); // Deprecated in Java 1.2
System.out.println("All threadsstopped");
}
} ///:~
  下面的输出结果已进行了适当的编辑,以便用一页能够装下( 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=
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值