Thread中的方法


 

Thread類中的方法調用方式

調用Thread中的方法的時候,在線程類中,有兩種方式:

1.this.xxx()

這種方式的線程是線程實例本身。

2.Thread.currentThread.xxx()或Thread.xxx()

這種表示線程執行Thread.currenThread.xxx()所在代碼塊的線程。

 

 

Thread類的實例方法

1.start()

這個方法的作用就是通知線程規划器此現場可以運行了。要注意,調用start方法的順序不代表線程啟動的順序,也就是cpu執行哪個線程的代碼具有不確定性。。

2.run()

這個方法是線程類調用start后執行的方法,如果在直接調用run而不是start方法,那么和普通方法一樣,沒有區別。

3.isAlive()

是判斷當前線程是否處於活動狀態。活動狀態就是已經啟動尚未終止。

4.getPriority()和setPriority(int newPriority)

這兩個方法是用於獲取當前和設置線程的優先級。優先級高的線程得到的cpu多。也就是說,兩個等待的線程,優先級高的線程容易被cpu執行。

默認情況下,線程的優先級是5。線程的優先級分為1~10等級。

優先級是具有繼承性的:

1 public class MyThread1 extends Thread{
2     public void run() {
3         System.out.println("MyThread1的線程優先級是"+this.getPriority());
4         MyThread2 myThread2 = new MyThread2();
5         myThread2.start();
6     }
7 }
1 public class MyThread2 extends Thread{
2     @Override
3     public void run() {
4         System.out.println("MyThread2的線程優先級是"+this.getPriority());
5     }
6 }
1 public static void main(String[] args) {
2         System.out.println("main thread begin priority="+Thread.currentThread().getPriority());
3         //Thread.currentThread().setPriority(6);
4         System.out.println("main thread end priority="+Thread.currentThread().getPriority());
5         MyThread1 myThread1 = new MyThread1();
6         myThread1.start();
7     }

結果是

main thread begin priority=5
main thread end priority=5
MyThread1的線程優先級是5
MyThread2的線程優先級是5

把注釋去掉再執行,結果是

main thread begin priority=5
main thread end priority=6
MyThread1的線程優先級是6
MyThread2的線程優先級是6

證明了線程優先級是具有繼承性的。

5.isDaeMon、setDaemon(boolean on)

java線程有兩種,一種是用戶線程,一種是守護線程。守護線程是一個特殊的線程,任何一個守護線程都是jvm中所有非守護線程的保姆。當進程中不存在非守護線程時,守護線程會自動銷毀。典型的守護線程就是垃圾回收線程。

第一個是判斷線程是不是守護線程,第二個是設置線程為守護線程,必須在線程start之前setDaemon(true)。

6.interrupt()

使用這個方法並不會中斷線程。實際上,調用interrupt實際作用是,在線程受到阻塞時拋出一個中斷信號,這樣線程就得以退出阻塞狀態。

7.join()

 1 public class MyThread1 extends Thread {
 2     public void run() {
 3         try {
 4             int secondValue = (int) (Math.random() * 10000);
 5             Thread.sleep(1000);
 6             System.out.println(secondValue);
 7             Thread.sleep(secondValue);
 8         } catch (Exception e) {
 9             e.printStackTrace();
10         }
11     }
12 }

 

1 public static void main(String[] args) throws Exception {
2         MyThread1 myThread1 = new MyThread1();
3         myThread1.start();
4         //myThread1.join();
5         System.out.println("main");
6     }

 

結果是

main
6479

 

很明顯是main方法先結束。那么去掉main中第4行的注釋。結果是

7487
main

 

main晚於線程myThread1結束。

join方法會使得調用join方法的線程(myThread1線程)所在的線程(main線程)無限阻塞,直到調用join方法的線程銷毀為止。也就是說,在這個例子當中,當myThread1線程銷毀以后,main線程才會繼續執行,在這期間都是阻塞的。

join方法內部使用的是wait(),所以會釋放鎖。

 

Thread類的靜態方法

1.currentThread()

該方法返回的當前正在執行線程對象的引用。

 1 public class MyThread1 extends Thread {
 2     static {
 3         System.out.println("靜態塊的打印:" + Thread.currentThread().getName());
 4     }
 5 
 6     public MyThread1() {
 7         System.out.println("構造方法的打印:" + Thread.currentThread().getName());
 8     }
 9 
10     public void run() {
11         System.out.println("run()方法的打印:" + Thread.currentThread().getName());
12     }
13 }

 

1 public static void main(String[] args) throws Exception {
2         MyThread1 myThread1 = new MyThread1();
3         myThread1.setName("myThread1");
4         myThread1.start();
5     }

 

結果

靜態塊的打印:main
構造方法的打印:main
run()方法的打印:myThread1

 

說明線程類的靜態代碼塊,構造方法是被main線程調用的,run方法是線程自己調用的。

2.sleep(long millis)

sleep方法的作用就是在指定的時間讓正在執行的線程休眠。並不釋放鎖。

3.yield()

暫停當前執行的線程對象,並執行其他線程。這個暫停會放棄cpu資源,放棄的時間不確定。

 1 public class MyThread1 extends Thread {
 2 
 3     public void run() {
 4         long beginTime = System.currentTimeMillis();
 5         int count = 0;
 6         for (int i = 0; i < 50000000; i++)
 7         {
 8             Thread.yield();
 9             count = count + i + 1;
10         }
11         long endTime = System.currentTimeMillis();
12         System.out.println("用時:" + (endTime - beginTime) + "毫秒!");
13     }
14 }

 

main

1 public static void main(String[] args) throws Exception {
2         MyThread1 myThread1 = new MyThread1();
3         myThread1.setName("myThread1");
4         myThread1.start();
5     }

 

結果:第一次調用main

用時:4029毫秒!

 

結果:第二次調用main

用時:4030毫秒!

 

結果:第三次調用main

用時:4067毫秒!

 

每次執行main方法得到的結果基本不同,證明了yield放棄cpu的時間不確定。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM