📄 prioritydemo.java
字号:
import javax.swing.JOptionPane;
class Priority implements Runnable
{
int runtimes=0;
private boolean running=true;
Thread thread;
public Priority(int p)
{
thread=new Thread(this);
thread.setPriority(p);
}
public void run(){
while (running)
runtimes++;
}
public void stop()
{
running=false;
}
public void start()
{
thread.start();
}
}
class PriorityDemo{
public static void main(String args[])
{
Priority hpThread,lpThread;
String output;
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
hpThread=new Priority(Thread.MAX_PRIORITY-1);
lpThread=new Priority(Thread.MIN_PRIORITY+2);
hpThread.start();
lpThread.start();
try{
Thread.sleep(5000);
}
catch(InterruptedException e)
{
System.out.println(e.getMessage());
}
hpThread.start();
lpThread.start();
try{
hpThread.thread.join();
lpThread.thread.join();
}
catch(InterruptedException e)
{
System.out.println(e.getMessage());
}
output="High priority thread has run"+hpThread.runtimes+"times\n"+"Low priority thread has run"+lpThread.runtimes+"times\n";
JOptionPane.showMessageDialog(null,output);
System.exit(0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -