JAVA--多線程

來源:互聯網
上載者:User

標籤:.com   gpo   調用   圖片   表示   class   同步方法   oid   ima   

一、多線程的書寫步驟

1.線程類繼承Thread

2.線程類中重寫run()方法

3.測試類別中建立線程對象,並用start()方法啟動線程

 

Runnable介面的方式

1.線程類繼承

2.建立線程對象:假設線程類是Runnable,則用Runnable myRunnable = new MyRunnable();建立對象,然後用Thread t = new Thread(myRunnable);建立

3.啟動線程

 

注意:這種方法必須建立一個對象

 

線程對象調用start(),方法和調用run()方法的區別

1.run()只有主線程一條執行路徑

2.start():多條執行路徑,主線程與子線程並行交替執行

 

匿名內部類的方法:直接在Test類中重寫run方法

    Thread t1 = new Thread() {            @Override            public void run() {                // TODO Auto-generated method stub                while (!teemo.isDead()) {                    gareen.attack(teemo);                }            }        };        t1.start();

 

 

 線程的一些常用方法

優先順序有1~10表示,1最低,預設優先順序為5

同步方法:

 

JAVA--多線程

聯繫我們

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