最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java多線程用法的實(shí)例詳解

 更新時間:2017年09月19日 10:21:04   投稿:lqh  
這篇文章主要介紹了Java多線程用法的實(shí)例詳解的相關(guān)資料,希望通過本文大家能夠理解掌握這部分內(nèi)容,需要的朋友可以參考下

Java多線程用法的實(shí)例詳解

前言:

最全面的java多線程用法解析,如果你對Java的多線程機(jī)制并沒有深入的研究,那么本文可以幫助你更透徹地理解Java多線程的原理以及使用方法。

1.創(chuàng)建線程

在Java中創(chuàng)建線程有兩種方法:使用Thread類和使用Runnable接口。在使用Runnable接口時需要建立一個Thread實(shí)例。因此,無論是通過Thread類還是Runnable接口建立線程,都必須建立Thread類或它的子類的實(shí)例。Thread構(gòu)造函數(shù):

public Thread( );
public Thread(Runnable target);
public Thread(String name);
public Thread(Runnable target, String name);
public Thread(ThreadGroup group, Runnable target);
public Thread(ThreadGroup group, String name);
public Thread(ThreadGroup group, Runnable target, String name);
public Thread(ThreadGroup group, Runnable target, String name, long stackSize);

方法一:繼承Thread類覆蓋run方法

public class ThreadDemo1 {
public static void main(String[] args){
     Demo d = new Demo();
     d.start();
for(int i=0;i<60;i++){
       System.out.println(Thread.currentThread().getName()+i);
     }

   }
 }
class Demo extends Thread{
public void run(){
for(int i=0;i<60;i++){
       System.out.println(Thread.currentThread().getName()+i);
     }
   }
 }

方法二:

public class ThreadDemo2 {
public static void main(String[] args){
    Demo2 d =new Demo2();
    Thread t = new Thread(d);
    t.start();
for(int x=0;x<60;x++){
      System.out.println(Thread.currentThread().getName()+x);
    }
  }
}
class Demo2 implements Runnable{
public void run(){
for(int x=0;x<60;x++){
      System.out.println(Thread.currentThread().getName()+x);
    }
  }
}

2.線程的生命周期

與人有生老病死一樣,線程也同樣要經(jīng)歷開始(等待)、運(yùn)行、掛起和停止四種不同的狀態(tài)。這四種狀態(tài)都可以通過Thread類中的方法進(jìn)行控制。下面給出了Thread類中和這四種狀態(tài)相關(guān)的方法。

// 開始線程
publicvoid start( );
publicvoid run( );
// 掛起和喚醒線程
publicvoid resume( ); // 不建議使用
publicvoid suspend( ); // 不建議使用
publicstaticvoid sleep(long millis);
publicstaticvoid sleep(long millis, int nanos);
// 終止線程
publicvoid stop( ); // 不建議使用
publicvoid interrupt( );
// 得到線程狀態(tài)
publicboolean isAlive( );
publicboolean isInterrupted( );
publicstaticboolean interrupted( );
// join方法
publicvoid join( ) throws InterruptedException;

線程在建立后并不馬上執(zhí)行run方法中的代碼,而是處于等待狀態(tài)。線程處于等待狀態(tài)時,可以通過Thread類的方法來設(shè)置線程不各種屬性,如線程的優(yōu)先級(setPriority)、線程名(setName)和線程的類型(setDaemon)等。

當(dāng)調(diào)用start方法后,線程開始執(zhí)行run方法中的代碼。線程進(jìn)入運(yùn)行狀態(tài)。可以通過Thread類的isAlive方法來判斷線程是否處于運(yùn)行狀態(tài)。當(dāng)線程處于運(yùn)行狀態(tài)時,isAlive返回true,當(dāng)isAlive返回false時,可能線程處于等待狀態(tài),也可能處于停止?fàn)顟B(tài)。下面的代碼演示了線程的創(chuàng)建、運(yùn)行和停止三個狀態(tài)之間的切換,并輸出了相應(yīng)的isAlive返回值。

一但線程開始執(zhí)行run方法,就會一直到這個run方法執(zhí)行完成這個線程才退出。但在線程執(zhí)行的過程中,可以通過兩個方法使線程暫時停止執(zhí)行。這兩個方法是suspend和sleep。在使用suspend掛起線程后,可以通過resume方法喚醒線程。而使用sleep使線程休眠后,只能在設(shè)定的時間后使線程處于就緒狀態(tài)(在線程休眠結(jié)束后,線程不一定會馬上執(zhí)行,只是進(jìn)入了就緒狀態(tài),等待著系統(tǒng)進(jìn)行調(diào)度)。

在使用sleep方法時有兩點(diǎn)需要注意:

1.sleep方法有兩個重載形式,其中一個重載形式不僅可以設(shè)毫秒,而且還可以設(shè)納秒(1,000,000納秒等于1毫秒)。但大多數(shù)操作系統(tǒng)平臺上的Java虛擬機(jī)都無法精確到納秒,因此,如果對sleep設(shè)置了納秒,Java虛擬機(jī)將取最接近這個值的毫秒。

2.在使用sleep方法時必須使用throws或try{…}catch{…}。因?yàn)閞un方法無法使用throws,所以只能使用try{…}catch{…}。當(dāng)在線程休眠的過程中,使用interrupt方法中斷線程時sleep會拋出一個InterruptedException異常。sleep方法的定義如下:

publicstaticvoid sleep(long millis) throws InterruptedException
publicstaticvoid sleep(long millis, int nanos) throws InterruptedException

有三種方法可以使終止線程。

  • 使用退出標(biāo)志,使線程正常退出,也就是當(dāng)run方法完成后線程終止。
  • 使用stop方法強(qiáng)行終止線程(這個方法不推薦使用,因?yàn)閟top和suspend、resume一樣,也可能發(fā)生不可預(yù)料的結(jié)果)。
  • 使用interrupt方法中斷線程。

1.使用退出標(biāo)志終止線程

當(dāng)run方法執(zhí)行完后,線程就會退出。但有時run方法是永遠(yuǎn)不會結(jié)束的。如在服務(wù)端程序中使用線程進(jìn)行監(jiān)聽客戶端請求,或是其他的需要循環(huán)處理的任務(wù)。在這種情況下,一般是將這些任務(wù)放在一個循環(huán)中,如while循環(huán)。如果想讓循環(huán)永遠(yuǎn)運(yùn)行下去,可以使用while(true){…}來處理。但要想使while循環(huán)在某一特定條件下退出,最直接的方法就是設(shè)一個boolean類型的標(biāo)志,并通過設(shè)置這個標(biāo)志為true或false來控制while循環(huán)是否退出。下面給出了一個利用退出標(biāo)志終止線程的例子。

join方法的功能就是使異步執(zhí)行的線程變成同步執(zhí)行。也就是說,當(dāng)調(diào)用線程實(shí)例的start方法后,這個方法會立即返回,如果在調(diào)用start方法后后需要使用一個由這個線程計(jì)算得到的值,就必須使用join方法。如果不使用join方法,就不能保證當(dāng)執(zhí)行到start方法后面的某條語句時,這個線程一定會執(zhí)行完。而使用join方法后,直到這個線程退出,程序才會往下執(zhí)行。下面的代碼演示了join的用法。

3.多線程安全問題

問題原因:當(dāng)多條語句在操作同一個線程共享數(shù)據(jù)時,一個線程對多條語句只執(zhí)行了一部分,還沒執(zhí)行完,另一個線程參與進(jìn)來執(zhí)行,導(dǎo)致共享數(shù)據(jù)的錯誤。

解決辦法:對多條操作共享數(shù)據(jù)的語句,只能讓一個線程都執(zhí)行完,在執(zhí)行過程中,其他線程不執(zhí)行。

同步代碼塊:

public class ThreadDemo3 {
  public static void main(String[] args){
    Ticket t =new Ticket();
    Thread t1 = new Thread(t,"窗口一");
    Thread t2 = new Thread(t,"窗口二");
    Thread t3 = new Thread(t,"窗口三");
    Thread t4 = new Thread(t,"窗口四");
    t1.start();
    t2.start();
    t3.start();
    t4.start();
  }
}
class Ticket implements Runnable{
  private int ticket =400;
  public void run(){
    while(true){
      synchronized (new Object()) {
        try {
          Thread.sleep(1);
        } catch (InterruptedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
        if(ticket<=0)
          break;
        System.out.println(Thread.currentThread().getName()+"---賣出"+ticket--);
      }
    }
  }
}

同步函數(shù)

public class ThreadDemo3 {
  public static void main(String[] args){
    Ticket t =new Ticket();
    Thread t1 = new Thread(t,"窗口一");
    Thread t2 = new Thread(t,"窗口二");
    Thread t3 = new Thread(t,"窗口三");
    Thread t4 = new Thread(t,"窗口四");
    t1.start();
    t2.start();
    t3.start();
    t4.start();
  }
}
class Ticket implements Runnable{
  private int ticket = 4000;
  public synchronized void saleTicket(){
    if(ticket>0)
      System.out.println(Thread.currentThread().getName()+"賣出了"+ticket--);

  }
  public void run(){
    while(true){
      saleTicket();
    }
  }
}

同步函數(shù)鎖是this 靜態(tài)同步函數(shù)鎖是class

線程間的通信

/**
 * Java學(xué)習(xí)交流QQ群:589809992 我們一起學(xué)Java!
 */
public class ThreadDemo3 {
  public static void main(String[] args){
    class Person{
      public String name;
      private String gender;
      public void set(String name,String gender){
        this.name =name;
        this.gender =gender;
      }
      public void get(){
        System.out.println(this.name+"...."+this.gender);
      }
    }
    final Person p =new Person();
    new Thread(new Runnable(){
      public void run(){
        int x=0;
        while(true){
          if(x==0){
            p.set("張三", "男");
          }else{
            p.set("lili", "nv");
          }
          x=(x+1)%2;
        }
      }
    }).start();
    new Thread(new Runnable(){
      public void run(){
        while(true){
          p.get();
        }
      }
    }).start();
  }
}
/*
張三....男
張三....男
lili....nv
lili....男
張三....nv
lili....男
*/

修改上面代碼

public class ThreadDemo3 {
   public static void main(String[] args){
     class Person{
       public String name;
       private String gender;
       public void set(String name,String gender){
         this.name =name;
         this.gender =gender;
       }
       public void get(){
         System.out.println(this.name+"...."+this.gender);
       }
     }
     final Person p =new Person();
     new Thread(new Runnable(){
       public void run(){
         int x=0;
         while(true){
           synchronized (p) {
             if(x==0){
               p.set("張三", "男");
             }else{
               p.set("lili", "nv");
             }
             x=(x+1)%2;  
           }

         }
       }
     }).start();
     new Thread(new Runnable(){
       public void run(){
         while(true){
           synchronized (p) {
             p.get();
           }
         }
       }
     }).start();
   }

 }
 /*
 lili....nv
 lili....nv
 lili....nv
 lili....nv
 lili....nv
 lili....nv
 張三....男
 張三....男
 張三....男
 張三....男
 */

等待喚醒機(jī)制

/*
 *線程等待喚醒機(jī)制
 *等待和喚醒必須是同一把鎖 
 */
public class ThreadDemo3 {
  private static boolean flags =false;
  public static void main(String[] args){
    class Person{
      public String name;
      private String gender;
      public void set(String name,String gender){
        this.name =name;
        this.gender =gender;
      }
      public void get(){
        System.out.println(this.name+"...."+this.gender);
      }
    }
    final Person p =new Person();
    new Thread(new Runnable(){
      public void run(){
        int x=0;
        while(true){
          synchronized (p) {
            if(flags)
              try {
                p.wait();
              } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
              };
            if(x==0){
              p.set("張三", "男");
            }else{
              p.set("lili", "nv");
            }
            x=(x+1)%2;
            flags =true;
            p.notifyAll();
          }
        }
      }
    }).start();
    new Thread(new Runnable(){
      public void run(){
        while(true){
          synchronized (p) {
            if(!flags)
              try {
                p.wait();
              } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
              };
            p.get();
            flags =false;
            p.notifyAll();
            }
        }
      }
    }).start();
  }
}

生產(chǎn)消費(fèi)機(jī)制一

/**
 * Java學(xué)習(xí)交流QQ群:589809992 我們一起學(xué)Java!
 */
public class ThreadDemo4 {
  private static boolean flags =false;
  public static void main(String[] args){
    class Goods{
      private String name;
      private int num;
      public synchronized void produce(String name){
        if(flags)
          try {
            wait();
          } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
        this.name =name+"編號:"+num++;
        System.out.println("生產(chǎn)了...."+this.name);
        flags =true;
        notifyAll();
      }
      public synchronized void consume(){
        if(!flags)
          try {
            wait();
          } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
        System.out.println("消費(fèi)了******"+name);
        flags =false;
        notifyAll();
      }

    }
    final Goods g =new Goods();
    new Thread(new Runnable(){
      public void run(){
        while(true){
          g.produce("商品");
        }
      }
    }).start();
    new Thread(new Runnable(){
      public void run(){
        while(true){
          g.consume();
        }
      }
    }).start();
  }
}

生產(chǎn)消費(fèi)機(jī)制二

public class ThreadDemo4 {
  private static boolean flags =false;
  public static void main(String[] args){
    class Goods{
      private String name;
      private int num;
      public synchronized void produce(String name){
        while(flags)
          try {
            wait();
          } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
        this.name =name+"編號:"+num++;
        System.out.println(Thread.currentThread().getName()+"生產(chǎn)了...."+this.name);
        flags =true;
        notifyAll();
      }
      public synchronized void consume(){
        while(!flags)
          try {
            wait();
          } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
        System.out.println(Thread.currentThread().getName()+"消費(fèi)了******"+name);
        flags =false;
        notifyAll();
      }

    }
    final Goods g =new Goods();
    new Thread(new Runnable(){
      public void run(){
        while(true){
          g.produce("商品");
        }
      }
    },"生產(chǎn)者一號").start();
    new Thread(new Runnable(){
      public void run(){
        while(true){
          g.produce("商品");
        }
      }
    },"生產(chǎn)者二號").start();
    new Thread(new Runnable(){
      public void run(){
        while(true){
          g.consume();
        }
      }
    },"消費(fèi)者一號").start();
    new Thread(new Runnable(){
      public void run(){
        while(true){
          g.consume();
        }
      }
    },"消費(fèi)者二號").start();
  }
}
/*
消費(fèi)者二號消費(fèi)了******商品編號:48049
生產(chǎn)者一號生產(chǎn)了....商品編號:48050
消費(fèi)者一號消費(fèi)了******商品編號:48050
生產(chǎn)者一號生產(chǎn)了....商品編號:48051
消費(fèi)者二號消費(fèi)了******商品編號:48051
生產(chǎn)者二號生產(chǎn)了....商品編號:48052
消費(fèi)者二號消費(fèi)了******商品編號:48052
生產(chǎn)者一號生產(chǎn)了....商品編號:48053
消費(fèi)者一號消費(fèi)了******商品編號:48053
生產(chǎn)者一號生產(chǎn)了....商品編號:48054
消費(fèi)者二號消費(fèi)了******商品編號:48054
生產(chǎn)者二號生產(chǎn)了....商品編號:48055
消費(fèi)者二號消費(fèi)了******商品編號:48055
*/

如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

  • java?集合工具類Collections及Comparable和Comparator排序詳解

    java?集合工具類Collections及Comparable和Comparator排序詳解

    這篇文章主要介紹了java集合工具類Collections及Comparable和Comparator排序詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-06-06
  • springboot項(xiàng)目啟動后執(zhí)行方法的三種方式

    springboot項(xiàng)目啟動后執(zhí)行方法的三種方式

    有時項(xiàng)目需求,需要項(xiàng)目啟動的時候向數(shù)據(jù)庫中查詢一下系統(tǒng)屬性,或者需要加載某個特定的方法,下面這篇文章主要給大家介紹了關(guān)于springboot項(xiàng)目啟動后執(zhí)行方法的三種方式,需要的朋友可以參考下
    2022-06-06
  • Java中java.sql.SQLException異常的正確解決方法(親測有效!)

    Java中java.sql.SQLException異常的正確解決方法(親測有效!)

    SQLException是在Java中處理數(shù)據(jù)庫操作過程中可能發(fā)生的異常,通常是由于底層數(shù)據(jù)庫操作錯誤或違反了數(shù)據(jù)庫規(guī)則而引起的,下面這篇文章主要給大家介紹了關(guān)于Java中java.sql.SQLException異常的正確解決方法,需要的朋友可以參考下
    2024-01-01
  • 基于Java Springboot + Vue + MyBatis實(shí)現(xiàn)音樂播放系統(tǒng)

    基于Java Springboot + Vue + MyBatis實(shí)現(xiàn)音樂播放系統(tǒng)

    這篇文章主要介紹了一個完整的音樂播放系統(tǒng)是基于Java Springboot + Vue + MyBatis編寫的,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-08-08
  • java實(shí)現(xiàn)動態(tài)上傳多個文件并解決文件重名問題

    java實(shí)現(xiàn)動態(tài)上傳多個文件并解決文件重名問題

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)動態(tài)上傳多個文件,并解決文件重名問題的方法,感興趣的小伙伴們可以參考一下
    2016-03-03
  • 帶你用Java全面剖析類和對象

    帶你用Java全面剖析類和對象

    下面小編就為大家?guī)硪黄胬斫釰ava類和對象。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2021-09-09
  • Java中CAS機(jī)制實(shí)現(xiàn)方法詳解

    Java中CAS機(jī)制實(shí)現(xiàn)方法詳解

    傳統(tǒng)的并發(fā)控制手段如synchronized和ReentrantLock雖有效防止資源競爭,卻可能引起性能開銷,相比之下,CAS(CompareAndSwap)提供一種輕量級的樂觀鎖策略,通過硬件級別的原子指令實(shí)現(xiàn)無鎖并發(fā),提高性能,需要的朋友可以參考下
    2024-09-09
  • 深入淺析springboot中static和templates區(qū)別

    深入淺析springboot中static和templates區(qū)別

    這篇文章主要介紹了springboot中static和templates區(qū)別,本文通過圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-02-02
  • SpringMVC五大組件與執(zhí)行原理分析總結(jié)

    SpringMVC五大組件與執(zhí)行原理分析總結(jié)

    這篇文章主要介紹了SpringMVC五大組件與執(zhí)行原理分析總結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-01-01
  • idea以任意順序debug多線程程序的具體用法

    idea以任意順序debug多線程程序的具體用法

    在idea中使用debug可以讓多個線程以任意順序執(zhí)行,接下來通過本文給大家介紹idea以任意順序debug多線程程序的具體用法,需要的朋友參考下吧
    2021-08-08

最新評論

阳高县| 拉孜县| 徐汇区| 那曲县| 来宾市| 固镇县| 滨海县| 东乌珠穆沁旗| 当阳市| 通许县| 沧州市| 兴业县| 德令哈市| 怀仁县| 陆良县| 汤阴县| 绿春县| 泗阳县| 五华县| 疏附县| 无极县| 乐亭县| 中阳县| 凌海市| 娄烦县| 临潭县| 偏关县| 鸡泽县| 留坝县| 阿坝县| 荔波县| 垫江县| 铜鼓县| 连南| 彝良县| 团风县| 高邑县| 札达县| 阳新县| 前郭尔| 洪雅县|