JAVA·多线程:线程优先级
每次结果不尽相同,优先级不能完全保证!
package multiThread; public class Thread04Priority { public static void main(String[] args) { // 三个线程 Thread t1 = new Thread(new MyThread()); // 第一个自定义线程 Thread t2 = new Thread(new MyThread()); // 第二个自定义线程 Thread tm = Thread.currentThread(); // 获得当前线程,即主线程 t1.setName("Thread MAX"); t2.setName("Thread MIN"); tm.setName("Thread NOR"); // 设置线程的优先级 t1.setPriority(Thread.MAX_PRIORITY); t2.setPriority(Thread.MIN_PRIORITY); t1.start(); t2.start(); Util.printMsg(); } } class MyThread implements Runnable { public void run() { Util.printMsg(); } } class Util { public static void printMsg() { for (int i = 0; i < 200; i++) { sleep(); if (199 == i) { Thread thisTread = Thread.currentThread(); System.out.println(); System.out.println("当前运行的线程是:" + thisTread); } } } public static void sleep() { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } }