📄 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; } public void run() { System.out.println(thrd.getName() + " excuting."); do { count++; if(currentName.compareTo(thrd.getName()) != 0){ currentName = thrd.getName(); System.out.println("at " + currentName); } } while((stop == false) && (count < 100000000)); stop = true; System.out.println("\n" + thrd.getName() + " end."); }}public class PriorityDemo { public static void main(String[] args) { Priority p1 = new Priority("high priority"); Priority p2 = new Priority("low priority"); p1.thrd.setPriority(Thread.NORM_PRIORITY + 2); p2.thrd.setPriority(Thread.NORM_PRIORITY - 2); p1.thrd.start(); p2.thrd.start(); try { p1.thrd.join(); p2.thrd.join(); } catch(InterruptedException e) { System.out.println("main Thread Interrupted"); } System.out.println("\n high priority count: " + p1.count); System.out.println("\n low priority count: " + p2.count); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -