📄 threadprioritydemo.java
字号:
import java.util.*;
class 优先级线程 extends Thread{
public 优先级线程(String 线程名,int 优先级){
super(线程名);
setPriority(优先级);
}
public void run(){
System.out.println(Thread.currentThread().getName() + "开始运行");
int j = 0;
Random r = new Random();
for(int i =0; i<10000; i++){
j = (i*r.nextInt())% 100 ;
if(i %2000 == 0)
System.out.println(Thread.currentThread().getName() + ": j = " + j);
}
System.out.println(Thread.currentThread().getName() + "结束运行");
}
}
public class ThreadPriorityDemo{
public static void main(String arg[]){
Thread t1 = Thread.currentThread();
if(t1.getPriority() == Thread.NORM_PRIORITY)
System.out.println("主线程优先级:NORM_PRIORITY");
优先级线程 t2 = new 优先级线程("高优先级线程",Thread.MAX_PRIORITY );
优先级线程 t3 = new 优先级线程("底优先级线程",Thread.MIN_PRIORITY );
t3.start();
t2.start();
t2.run();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -