hilopri.java
来自「使用Swing编写的记事本」· Java 代码 · 共 47 行
JAVA
47 行
class clicker implements Runnable{
int click=0;
Thread t;
private volatile boolean running=true;
public clicker(int p){
t=new Thread(this);
t.setPriority(p);
}
public void run(){
while(running){
click++;
}
}
public void stop(){
running=false;
}
public void start(){
t.start();
}
}
class HiLoPri{
public static void main(String args[]){
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
clicker hi=new clicker(Thread.NORM_PRIORITY+2);
clicker lo=new clicker(Thread.NORM_PRIORITY-2);
lo.start();
hi.start();
try{
Thread.sleep(10000);
}
catch(InterruptedException e){
System.out.println("Main thread Interrupted.");
}
lo.stop();
hi.stop();
try{
lo.t.join();
hi.t.join();
}
catch(InterruptedException e){
System.out.println("InterruptedException caught.");
}
System.out.println("Low-priority thread:"+lo.click);
System.out.println("High-priority thread:"+hi.click);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?