java多線程編程

來源:互聯網
上載者:User

標籤:處理   sum   resume   cep   res   線程   技術   thread   one   

參考書籍:Java多線程編程核心技術(高洪岩)

一、java多線程技能

  1.線程的啟動

    • 進程:QQ.exe
    • 線程:QQ發一條訊息由一個線程處理,QQ傳輸檔案由一個線程處理
    • 建立線程:使用多線程的時候,代碼運行結果與代碼執行順序和調用順序是無關的。使用多線程的時候可能上一行的代碼還沒執行完,就執行了下一行代碼。
      • 繼承Thread類
 1 class A extends Thread { 2     public void run(){ 3         //... 4     } 5 } 6 public class Demo { 7     public static void main(String[] args) { 8         A a = new A(); 9         a.start();10     }11 }
      • 實現Runnable介面
 1 class A implements Runnable{ 2     public void run() { 3         //... 4     } 5 } 6 public class Demo { 7     public static void main(String[] args) { 8         A a = new A(); 9         Thread t = new Thread(a);10         t.start();11     }12 }
    • 多線程訪問同一資源避免非安全執行緒問題(使用synchronized修飾)
public class Mythread extends Thread {        private int count = 5;        @Override    synchronized public void run() {        super.run();        count--;                System.out.println("由"+this.currentThread().getName()+"計算,count="+count);    }}public class Test {    public static void main(String[] args) {        try {            Mythread thread = new Mythread();            Thread a = new Thread(thread,"A");            Thread b = new Thread(thread,"B");            Thread c = new Thread(thread,"C");            Thread d = new Thread(thread,"D");            Thread e = new Thread(thread,"E");            a.start();            b.start();            c.start();            d.start();            e.start();         } catch (Exception e) {            e.printStackTrace();        }    }}
    • 常用方法
public class Test {    public static void main(String[] args) {        try {            Thread.currentThread();//傳回碼段正在被哪個線程調用的資訊            Thread.currentThread().getName();//擷取當前線程的名稱            Thread.currentThread().isAlive();//當前線程是否處於活躍狀態            Thread.sleep(1000);//讓線程休眠1秒            Thread.currentThread().getId();//擷取當前線程的唯一標識            Thread.currentThread().interrupt();//停止當前線程(此方法只是打一個停止線程的標記,並非真正停止線程,線程會繼續執行)            Thread.currentThread().interrupted();//測試當前線程是否已經中斷,同時清除該中斷狀態            Thread.currentThread().isInterrupted();//測試當前線程是否已經中斷,只是測試,不做其他處理            Thread.currentThread().stop();//棄用的方法,直接停止線程,使一些請理性工作無法完成,同時解鎖鎖定的對象,不能保證資料一致            Thread.currentThread().suspend();//棄用的方法,暫停線程,使用此方法可能會造成對公用對象的獨佔,使其他線程無法訪問公用同步對象            Thread.currentThread().resume();//棄用的方法,繼續暫停線程            Thread.currentThread().setPriority(10);//設定優先權,1 <= x <= 10;            Thread.currentThread().yield();//放棄當前cpu資源,讓給其他任務去獨佔cpu         } catch (Exception e) {            e.printStackTrace();        }    }}

 

 

   2.如何使線程暫停

   3.如何使線程停止

   4.線程的優先順序

   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.