線程操作API


1、Thread.currendThead

Thread的靜態方法currendThead方法可以用於獲取運行當前代碼片段的線程。

    public static void main(String[] args){
        System.out.println(Thread.currentThread());
        //輸出 Thread[main,5,main]
    }

 

2、獲取線程信息

Thread提供了獲取線程信息的相關方法

public class testThread implements Runnable{

    public void run(){
        for(int i=0;i<5;i++){
                System.out.println(i);
        }    
    }
    public static void main(String[] args){
        testThread t1=new testThread();
        Thread t=new Thread(t1);
Thread t2=new Thread("自定義的名字");
t.start(); System.out.println(t.getId());//返回線程標識符 8 System.out.println(t.getName());//返回線程名字 Thread-0 System.out.println(t.getPriority());//返回線程優先級 5 System.out.println(t.getState());//返回線程狀態 BLOCKED System.out.println(t.isAlive());//線程是否處於活動狀態 false System.out.println(t.isDaemon());//線程是否是守護線程 false System.out.println(t.isInterrupted());//線程是否是已中斷 false } }

3、線程優先級

線程切換時由線程調度控制的,我們無法通過代碼來干涉,但是我們可以通過提高線程優先級來最大程度的改善線程獲取時間片的幾率。

線程的優先級划分為10級,分別為1-10(1最低,10最高),

 

public class testThread { 
    public static void main(String[] args) { 
            Thread t1 = new MyThread1(); 
            Thread t2 = new MyThread2(); 
            t1.setPriority(10); 
            t2.setPriority(1); 
            t2.start(); 
            t1.start(); 
    } 
} 

class MyThread1 extends Thread { 
    public void run() { 
            for (int i = 0; i < 5; i++) { 
                    System.out.println("線程1第" + i + "次執行!"); 
            } 
    } 
} 
class MyThread2 extends Thread{ 
    public void run() { 
            for (int i = 0; i < 5; i++) { 
                    System.out.println("線程2第" + i + "次執行!"); 
   
            } 
    } 
}
線程1第0次執行!
線程1第1次執行!
線程1第2次執行!
線程2第0次執行!
線程1第3次執行!
線程2第1次執行!
線程1第4次執行!
線程2第2次執行!
線程2第3次執行!
線程2第4次執行!

說明:優先級高的線程獲取CPU資源的概率較大,優先級低的並非沒機會執行。

但在java中,優先級高的好像並沒有太大的優勢,按理說實際的應該明顯優於1級的。

4、守護線程

守護線程和普通線程在表現上沒什么區別,我們只需要通過Thread提供的方法來設定即可;

void.setDaemon(boolean),當參數為true時該線程為守護線程。

當線程中指剩下守護線程時,所有守護線程強制終止。

  Daemon的作用是為其他線程的運行提供服務,比如說GC線程。其實User Thread線程和Daemon Thread守護線程本質上來說去沒啥區別的,唯一的區別之處就在虛擬機的離開:如果User Thread全部撤離,那么Daemon Thread也就沒啥線程好服務的了,所以虛擬機也就退出了。

5、sleep方法

Thread的靜態方法

sleep用於使當前線程進入阻塞狀態,該方法會使線程進入阻塞狀態指定毫秒,當阻塞指定毫秒后,當前線程重新進入Runnable狀態,等待分配時間片(重新分配)

該方法聲明拋出一個InterruptException,必須捕獲異常

不釋放鎖,不出讓系統資源,其他線程不能占用CPU

public class testThread { 
    public static void main(String[] args) { Thread t = new Thread(){ public void run(){ while(true){ System.out.println("測試守護線程"); try{ Thread.sleep(1000); }catch(InterruptedException e){ } } } }; t.setDaemon(true); t.start(); try{ Thread.sleep(5000); }catch(InterruptedException e1){ } //進程中所有前台進程結束了,后台線程強制結束 System.out.println("main進線程結束了"); } } 
測試守護線程
測試守護線程
測試守護線程
測試守護線程
測試守護線程
測試守護線程
main進線程結束了

6、yield方法

Thread的靜態方法,該方法用於使當前線程主動讓出檔次CPU時間片回到Runnable狀態,等待分配時間片

public class Testyield extends Thread{
    public Testyield(){
         
    }
    public Testyield(String name){
        super(name);
    }
    public  void run(){
        for(int i=0;i<5;i++){
 //           System.out.println(getName()+"  "+i);
            if (i==2){
                System.out.println(getName()+"------"+getPriority());
                Thread.yield();
            }
        }
    }
    public static void main(String[] args){
        Testyield y1=new Testyield("高級");
        y1.setPriority(Thread.MAX_PRIORITY);
        y1.start();
        Testyield y2=new Testyield("低級");
        y2.setPriority(Thread.MIN_PRIORITY);
        y2.start();
    }
}

 

高級------10
低級------1

 

7、join方法

讓一個線程B“加入”到另外一個線程A的尾部。在A執行完畢之前,B不能工作

該方法聲明拋出IntertuptException.

 

public class JoinTest {
    public static void main(String [] args) throws InterruptedException {
        ThreadJoinTest t1 = new ThreadJoinTest("test_1");
        ThreadJoinTest t2 = new ThreadJoinTest("test_2");
        t1.start();
        /**join的意思是使得放棄當前線程的執行,並返回對應的線程,例如下面代碼的意思就是:
         程序在main線程中調用t1線程的join方法,則main線程放棄cpu控制權,並返回t1線程繼續執行直到線程t1執行完畢
         所以結果是t1線程執行完后,才到主線程執行,相當於在main線程中同步t1線程,t1執行完了,main線程才有執行的機會
         */
        t1.join();
        t2.start();
    }

}
class ThreadJoinTest extends Thread{
    public ThreadJoinTest(String name){
        super(name);
    }
    @Override
    public void run(){
        for(int i=0;i<5;i++){
            System.out.println(this.getName() + ":" + i);
        }
    }
}

test_1:0
test_1:1
test_1:2
test_1:3
test_1:4
test_2:0
test_2:1
test_2:2
test_2:3
test_2:4

 


免責聲明!

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



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