📄 testthreadgroup.java
字号:
public class TestThreadGroup
{
public static void main(String[] args)
{
//(1)显示系统线程组的信息
ThreadGroup systemGroup=Thread.currentThread().getThreadGroup();
System.out.println("(1)显示系统线程组的信息 : ");
systemGroup.list();
//(2)将系统线程组的最大优先级减一
//并且将系统主线程的优先级加一
systemGroup.setMaxPriority(Thread.MAX_PRIORITY);
Thread currentThread=Thread.currentThread();
currentThread.setPriority(currentThread.getPriority()+1);
System.out.println("(2)将系统线程组的最大优先级减一,"+"并且将系统主线程的优先级加一");
systemGroup.list();
//(3)创建一个线程组ThreadGroup1,并且将优先级设置最大
//在其中创建一个线程,也将其优先级设置为最大
ThreadGroup threadGroup1=new ThreadGroup("ThreadGroup1");
threadGroup1.setMaxPriority(Thread.MAX_PRIORITY);
Thread thread1=new Thread(threadGroup1,"Thread A");
thread1.setPriority(Thread.MAX_PRIORITY);
System.out.println("(3)创建一个线程组ThreadGroup1,并且将优先级设置最大(10),");
System.out.println("在其中创建一个线程Thread A,也将其优先级设置为最大");
threadGroup1.list();
//(4)将线程组ThreadGroup1的最大优先级减一,然后再将其设为最大
threadGroup1.setMaxPriority(Thread.MAX_PRIORITY-1);
threadGroup1.setMaxPriority(Thread.MAX_PRIORITY);
System.out.println("(4)将线程组ThreadGroup1的最大优先级减一,然后再将其设为最大(10)");
threadGroup1.list();
//(5)在线程组ThreadGroup1中新建一个线程Thread B,将其优先级设置为最大
thread1=new Thread(threadGroup1,"Thread B");
thread1.setPriority(Thread.MAX_PRIORITY );
System.out.println("(5)在线程组ThreadGroup1中新建一个线程Thread B,"+
"将其优先级设置为最大(10)");
threadGroup1.list();
//(6)将线程组ThreadGroup1的优先级设置为低于线程的默认优先级
//并且在其中创建一个新的线程Thread C
threadGroup1.setMaxPriority(Thread.MIN_PRIORITY+2);
Thread thread2=new Thread(threadGroup1,"Thread C");
System.out.println("(6)将线程组ThreadGroup1的优先级设置为低于线程的默认优先级");
System.out.println(" 并且在其中创建一个新的线程Thread C : ");
threadGroup1.list();
//(7)将线程Thread B和Thread C的优先级各减一
thread1.setPriority(thread1.getPriority()-1);
thread2.setPriority(thread2.getPriority()-1);
System.out.println("(7)将线程Thread B和Thread C的优先级各减一: ");
threadGroup1.list();
//(8)在线程组Thread Group1下创建一个子线程组ThreadGroup2
ThreadGroup threadGroup2=new ThreadGroup(threadGroup1,"ThreadGroup2");
System.out.println("(8)在线程组Thread Group1下创建一个子线程组ThreadGroup2: ");
threadGroup2.list();
//(9)将线程组ThreadGroup2的最大优先级设置为最大
threadGroup2.setMaxPriority(Thread.MAX_PRIORITY );
System.out.println("(9)将线程组ThreadGroup2的最大优先级设置为最大(10): ");
threadGroup2.list();
//(10)在线程组ThreadGroup2下创建线程Thread D和Thread E
thread1=new Thread(threadGroup2,"Thread D");
thread1=new Thread(threadGroup2,"Thread E");
System.out.println("(10)在线程组ThreadGroup2下创建线程Thread D和Thread E.");
System.out.println(" 打印出系统线程组的信息: ");
systemGroup.list();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -