Java 線程的優先級(setPriority)案例詳解
線程可以劃分優先級,優先級高的線程得到的CPU資源比較多,也就是CPU優先執行優先級高的線程對象中的任務。
設置線程優先級有助於幫助線程規劃器確定下一次選中哪一個線程優先執行。
java中優先級分為1-10個級別
線程優先級的繼承特性 例如a線程啟迪b線程,則b線程的優先級與a的一樣。
代碼說話:(很簡單)
public class MyThread1 extends Thread { @Override public void run() { System.out.println("MyThread1 run priority=" + this.getPriority()); MyThread2 thread2 = new MyThread2(); thread2.start(); } } public class MyThread2 extends Thread { @Override public void run() { System.out.println("MyThread2 run priority=" + this.getPriority()); } } public static void main(String[] args) { System.out.println("main thread begin priority=" + Thread.currentThread().getPriority()); Thread.currentThread().setPriority(6); System.out.println("main thread end priority=" + Thread.currentThread().getPriority()); MyThread1 thread1 = new MyThread1(); thread1.start(); }
優先級具有規則性
public class MyThread1 extends Thread { @Override public void run() { long beginTime = System.currentTimeMillis(); long addResult = 0; for (int j = 0; j < 10; j++) { for (int i = 0; i < 50000; i++) { Random random = new Random(); random.nextInt(); addResult = addResult + i; } } long endTime = System.currentTimeMillis(); System.out.println("★★★★★thread 1 use time=" + (endTime - beginTime)); } } public class MyThread2 extends Thread { @Override public void run() { long beginTime = System.currentTimeMillis(); long addResult = 0; for (int j = 0; j < 10; j++) { for (int i = 0; i < 50000; i++) { Random random = new Random(); random.nextInt(); addResult = addResult + i; } } long endTime = System.currentTimeMillis(); System.out.println("☆☆☆☆☆thread 2 use time=" + (endTime - beginTime)); } } public static void main(String[] args) { for (int i = 0; i < 5; i++) { MyThread1 thread1 = new MyThread1(); thread1.setPriority(1); thread1.start(); MyThread2 thread2 = new MyThread2(); thread2.setPriority(10); thread2.start(); } }
高優先級的線程總是先執行完
線程的優先級和代碼的執行順序沒有關系
優先級具有隨機性
一般優先級較高的線程先執行run()方法,但是這個不能說的但肯定,因為線程的優先級具有 “隨機性”也就是較高線程不一定每一次都先執行完。
public class MyThread1 extends Thread { @Override public void run() { long beginTime = System.currentTimeMillis(); for (int i = 0; i < 1000; i++) { Random random = new Random(); random.nextInt(); } long endTime = System.currentTimeMillis(); System.out.println("★★★★★thread 1 use time=" + (endTime - beginTime)); } } public class MyThread2 extends Thread { @Override public void run() { long beginTime = System.currentTimeMillis(); for (int i = 0; i < 1000; i++) { Random random = new Random(); random.nextInt(); } long endTime = System.currentTimeMillis(); System.out.println("☆☆☆☆☆thread 2 use time=" + (endTime - beginTime)); } } public static void main(String[] args) { for (int i = 0; i < 5; i++) { MyThread1 thread1 = new MyThread1(); thread1.setPriority(5); thread1.start(); MyThread2 thread2 = new MyThread2(); thread2.setPriority(6); thread2.start(); } }
守護線程介紹:
java 中有兩種線程 一個是用戶線程,一個是守護(Daemon)線程
典型的守護線程就是垃圾回收線程,如果進程中沒有非守護線程瞭,則守護線程自動銷毀。
守護線程作用就是為其他線程的運行提供便利的服務,比如GC。
public class MyThread extends Thread { private int i = 0; @Override public void run() { try { while (true) { i++; System.out.println("i=" + (i)); Thread.sleep(1000); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] args) { try { MyThread thread = new MyThread(); thread.setDaemon(true);//設置守護線程 thread.start(); Thread.sleep(5000); System.out.println("我離開thread對象也不再打印瞭,也就是停止瞭!"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
到此這篇關於Java 線程的優先級(setPriority)案例詳解的文章就介紹到這瞭,更多相關Java 線程的優先級(setPriority)內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!