低
低调一贱男
Unregistered / Unconfirmed
GUEST, unregistred user!
//testaccess.java 线程群组(Thread group)
class testthread1 extends Thread{//线程
private int i;
testthread1(ThreadGroup g, String name){
super(g,name);
// System.out.println(this.getName());
}
void f(){
i++;
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();//取得上两级父线程组,即为x ThreadGroup
// System.out.println(this.getName());
// g.list();
Thread[] gall=new Thread[g.activeCount()];
//取得线程组内的线程数
g.enumerate(gall);
//会将指向"先前产生之线程"的所有references置于数组gall中
// System.out.println(gall.toString());
for(int i=0;
i<gall.length;
i++){
System.out.println(gall);
// gall.setPriority(Thread.MIN_PRIORITY);//将优先级设置为最低 //为什么有个空指针???????????
// ((testthread1)gall).f();
}
g.list();
}
}
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");
}
}
/*结果
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]
twof()
java.lang.NullPointerException
at testthread2.run(testaccess.java:29)
Press any key to continue...
*/
class testthread1 extends Thread{//线程
private int i;
testthread1(ThreadGroup g, String name){
super(g,name);
// System.out.println(this.getName());
}
void f(){
i++;
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();//取得上两级父线程组,即为x ThreadGroup
// System.out.println(this.getName());
// g.list();
Thread[] gall=new Thread[g.activeCount()];
//取得线程组内的线程数
g.enumerate(gall);
//会将指向"先前产生之线程"的所有references置于数组gall中
// System.out.println(gall.toString());
for(int i=0;
i<gall.length;
i++){
System.out.println(gall);
// gall.setPriority(Thread.MIN_PRIORITY);//将优先级设置为最低 //为什么有个空指针???????????
// ((testthread1)gall).f();
}
g.list();
}
}
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");
}
}
/*结果
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]
twof()
java.lang.NullPointerException
at testthread2.run(testaccess.java:29)
Press any key to continue...
*/