JAVA線程式控制制,java線程

來源:互聯網
上載者:User

JAVA線程式控制制,java線程

  可以通過線程的方法進行基本的線程式控制制,除了已知的start、run、sleep方法外還有isAlive、currentThread、interrupt方法。

isAlive:該方法用來測試線程是否處於活動狀態。線程由start方法啟動後,直至被終止之間的任何時刻都處於Alive狀態。當處於建立狀態和死亡狀態時,該方法返回false。

currentThread:該方法是Thread類的的類方法,返回正在使用CPU資源的線程。

interrupt:當線程處於休眠狀態時,一個佔有CPU資源的線程可以讓休眠的線程調用interrupt方法喚醒自己,導致休眠的線程發生InterruptedException異常結束休眠,重新排隊等待CPU資源。

class A implements Runnable{    Thread student,teacher;    A(){        teacher=new Thread(this,"王教授");        student=new Thread(this,"張三");    }        public void run(){        if(Thread.currentThread()==student){            try{                System.out.println(student.getName()+"正在睡覺,不聽課");                Thread.sleep(1000*60*60);            }catch(InterruptedException e){                System.out.println(student.getName()+"被老師叫醒了");            }        }        else if(Thread.currentThread()==teacher){            for(int i=1;i<=3;i++){                System.out.println("上課!");            }try{                Thread.sleep(500);            }catch(InterruptedException e){}            student.interrupt();        }    }}public class BasicControlThread{    public static void main(String args[]){        A a=new A();        a.student.start();        a.teacher.start();    }}

 

此外還有stop和join方法

stop():通過調用線程的執行個體方法stop()來終止線程,終止後即進入死亡狀態,不能再被調度。

join():一個線程在佔有CPU資源期間,可以讓其他線程調用join()方法和本線程聯合。當前線程等待調用該方法線程結束後,再重新排隊等待CPU資源,以便恢複執行。

class TV{    float price;    String name;    TV(String name,float price){        this.price=price;        this.name=name;    }}class ThreadJoin implements Runnable{    TV tv;    Thread customer,tvMaker;    ThreadJoin(){        customer=new Thread(this,"顧客");        tvMaker=new Thread(this,"電視製造商");    }    public void run(){        if(Thread.currentThread()==customer){            System.out.println(customer.getName()+"等"+tvMaker.getName()+"生產電視機");            try{                tvMaker.join();//線程customer開始等待tvMaker結束            }catch(InterruptedException e){ }            System.out.println(customer.getName()+"買了一台電視機"+tv.name+"價格:"+tv.price);            }        else if(Thread.currentThread()==tvMaker){            System.out.println(tvMaker.getName()+"開始生產電視機...");            try{                tvMaker.sleep(2000);            }catch(InterruptedException e){ }            tv=new TV("長虹牌",4500);            System.out.println(tvMaker.getName()+"生產完畢!!!");        }    }}public class JoinThread{    public static void main(String args[]){        ThreadJoin th=new ThreadJoin();        th.customer.start();        th.tvMaker.start();    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.