📄 prioritydemo.java
字号:
//优先级演示.
class Priority implements Runnable{
int count;
Thread thrd;
static boolean stop = false;
static String currentName;
/*创建一个新的线程. 注意构造方法并没有真正开始这个线程的运行*/
Priority(String name){
thrd = new Thread(this, name);
count =0;
currentName = name;
}
//Begin execution of new thread.
public void run(){
System.out.println(thrd.getName()+" 开始执行.");
do{
count++;
if(currentName.compareTo(thrd.getName())!=0){
currentName = thrd.getName();
System.out.println("在 "+currentName);
}
}while((stop==false)&&(count<100000000));
stop= true;
System.out.println("\n"+thrd.getName()+" 结束.");
}
}
class PriorityDemo {
public static void main(String args[]){
Priority mt1 = new Priority("高优先级.");
Priority mt2 = new Priority("低优先级.");
//设置优先级.
mt1.thrd.setPriority(Thread.NORM_PRIORITY+2);
mt2.thrd.setPriority(Thread.NORM_PRIORITY-2);
//开始线程
mt1.thrd.start();
mt2.thrd.start();
try{
mt1.thrd.join();
mt2.thrd.join();
}catch(InterruptedException e){
System.out.println("主线程被打断.");
}
System.out.println("\n高优先级线程计数到 "+
mt1.count);
System.out.println("\n低优先级线程计数到 "+
mt2.count);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -