prioritydemo.java
来自「Java 程序设计源码 只提供了部分」· Java 代码 · 共 57 行
JAVA
57 行
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 + =
减小字号Ctrl + -
显示快捷键?