多線程的join方法和Yield方法以及優先順序問題__線程

來源:互聯網
上載者:User

join方法 Yield方法 線程優先順序路權

join()方法

t1.join()意味著當前運行線程要等待t1,是執行個體方法

package sisuo;public class Test extends Thread {    @Override    public void run() {//主線程        ThreadDemo t=new ThreadDemo();        t.setName("線程A");        t.start();        JoinThread j= new JoinThread();        j.setName("加入的線程");        j.start();        t.b=false;        try {            j.join();//等待前邊所有線程結束,然後執行後邊的線程,約束主線程        } catch (InterruptedException e) {            System.out.println("等待線程結束時異常");        }        System.out.println("主線程開啟");        for (int i = 1; i < 101; i++) {            System.out.println(Thread.currentThread().getName()+"主線程執行了"+i+"次");        }        System.out.println("主線程執行結束。");    }    public static void main(String[] args) {        new Test().start();//開啟主線程    }}package sisuo;public class JoinThread extends Thread {    @Override    public void run() {        System.out.println("加入的線程開始執行");        for (int i = 1; i <11; i++) {            System.out.println(Thread.currentThread().getName()+"加入的線程執行了"+i+"次");        }        System.out.println(Thread.currentThread().getName()+"執行結束");    }}package sisuo;public class ThreadDemo extends Thread {    public boolean b=true;    @Override    public void run() {        while(b){            for (int i = 1; i < 6; i++) {                System.out.println(Thread.currentThread().getName()+"執行了"+i+"次");            }        }        System.out.println(Thread.currentThread().getName()+"執行結束");    }}
Yield方法

類方法,一個線程把CPU讓出來,讓給誰並不明確,Thread.yield();

    for (int i = 0; i < 10000; i++) {            System.out.println("按斷行符號鍵幹擾");            s.nextLine();                   t1.interrupt();            if(i==50){                Thread.yield();            }            try {                Thread.sleep(1000);            } catch (InterruptedException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }
線程優先順序(路權)

優先順序高的線程往往會得到CPU的優先載入,會先執行,Java線程Thread的優先順序共有10級,最低為1,最高為10,包括main線程在內預設優先順序都是5

    public static void main(String[] args) {//      主線程的優先順序        System.out.println(Thread.currentThread().getPriority());        System.out.println("最高優先順序:"+Thread.MAX_PRIORITY);        System.out.println("最低優先順序:"+Thread.MIN_PRIORITY);        System.out.println("預設優先順序:"+Thread.NORM_PRIORITY);        MyThread mt1 = new MyThread();        Thread t1 = new Thread(mt1);        t1.setName("追龍");        MyThread mt2 = new MyThread();        Thread t2 = new Thread(mt2);        t2.setName("奇門遁甲");        MyThread mt3 = new MyThread();        Thread t3 = new Thread(mt3);        t3.setName("機器之血");//      設定優先權        t1.setPriority(8);        t2.setPriority(2);        t1.start();        t2.start();        t3.start();        System.out.println(t1.getPriority());        System.out.println(t2.getPriority());        System.out.println(t3.getPriority());

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.