乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      JAVA 并發(fā)編程-多個(gè)線程之間共享數(shù)據(jù)(六)

       跑不動(dòng)的豬 2020-05-05

      多線程共享數(shù)據(jù)的方式:

      1,如果每個(gè)線程執(zhí)行的代碼相同,可以使用同一個(gè)Runnable對(duì)象,這個(gè)Runnable對(duì)象中有那個(gè)共享數(shù)據(jù),例如,賣票系統(tǒng)就可以這么做。

      2,如果每個(gè)線程執(zhí)行的代碼不同,這時(shí)候需要用不同的Runnable對(duì)象,例如,設(shè)計(jì)4個(gè)線程。其中兩個(gè)線程每次對(duì)j增加1,另外兩個(gè)線程對(duì)j每次減1,銀行存取款

      有兩種方法來解決此類問題:

      將共享數(shù)據(jù)封裝成另外一個(gè)對(duì)象,然后將這個(gè)對(duì)象逐一傳遞給各個(gè)Runnable對(duì)象,每個(gè)線程對(duì)共享數(shù)據(jù)的操作方法也分配到那個(gè)對(duì)象身上完成,這樣容易實(shí)現(xiàn)針對(duì)數(shù)據(jù)進(jìn)行各個(gè)操作的互斥和通信

      將Runnable對(duì)象作為一個(gè)類的內(nèi)部類,共享數(shù)據(jù)作為這個(gè)類的成員變量,每個(gè)線程對(duì)共享數(shù)據(jù)的操作方法也封裝在外部類,以便實(shí)現(xiàn)對(duì)數(shù)據(jù)的各個(gè)操作的同步和互斥,作為內(nèi)部類的各個(gè)Runnable對(duì)象調(diào)用外部類的這些方法。

      下面逐一介紹

      每個(gè)線程執(zhí)行的代碼相同,可以使用同一個(gè)Runnable對(duì)象

      賣票系統(tǒng)demo:

      package com.tgb.hjy;

      /**

       * 多線程共享數(shù)據(jù)-賣票系統(tǒng)

       * @author hejingyuan

       *

       */

      public class SellTicket {

      /** 

           * @param args 

           */  

          public static void main(String[] args) {  

              Ticket t = new Ticket();  

              new Thread(t).start();  

              new Thread(t).start();  

          }  

      }

      class Ticket implements Runnable{  

          private int ticket = 10;  

          public void run() {  

              while(ticket>0){  

                  ticket--;  

                  System.out.println("當(dāng)前票數(shù)為:"+ticket);  

              }  

          }  

      簡(jiǎn)單的多線程間數(shù)據(jù)共享,每個(gè)線程執(zhí)行的代碼不同,用不同的Runnable對(duì)象

      設(shè)計(jì)4個(gè)線程。

      其中兩個(gè)線程每次對(duì)j增加1,另外兩個(gè)線程對(duì)j每次減1

      package com.tgb.hjy;

      public class TestThread {

      /** 

           * @param args 

           */  

          public static void main(String[] args) {  

              final MyData data = new MyData();  

              for(int i=0;i<2;i++){  

                  new Thread(new Runnable(){  

                      public void run() {  

                          data.add();  

                      }  

                  }).start();  

                  new Thread(new Runnable(){  

                      public void run() {  

                          data.dec();  

                      }  

                  }).start();  

              }  

          }  

      }

      class MyData {  

          private int j=0;  

          public  synchronized void add(){  

              j++;  

              System.out.println("線程"+Thread.currentThread().getName()+"j為:"+j);  

          }  

          public  synchronized void dec(){  

              j--;  

              System.out.println("線程"+Thread.currentThread().getName()+"j為:"+j);  

          }  

      }  

      銀行存取款實(shí)例:

      package com.tgb.hjy;

      public class Acount {

      private int money;

      public Acount(int money){

         this.money=money;

      }

      public synchronized void getMoney(int money){

        //注意這個(gè)地方必須用while循環(huán),因?yàn)榧幢阍俅嫒脲X也有可能比取的要少

        while(this.money<money){  

         System.out.println("取款:"+money+" 余額:"+this.money+" 余額不足,正在等待存款......");

         try{ wait();} catch(Exception e){}

        }

        this.money=this.money-money;

        System.out.println("取出:"+money+" 還剩余:"+this.money);

      }

      public synchronized void setMoney(int money){

        try{ Thread.sleep(10);}catch(Exception e){}

        this.money=this.money+money;

        System.out.println("新存入:"+money+" 共計(jì):"+this.money);

        notify();

      }

      public static void main(String args[]){

        Acount Acount=new Acount(0);

        Bank b=new Bank(Acount);

        Consumer c=new Consumer(Acount);

        new Thread(b).start();

        new Thread(c).start();

      }

      }

      //存款類

      class Bank implements Runnable {

      Acount Acount;

      public Bank(Acount Acount){

      this.Acount=Acount;

      }

      public void run(){

      while(true){

      int temp=(int)(Math.random()*1000);

      Acount.setMoney(temp); 

      }

      }

      }

      //取款類

      class Consumer implements Runnable {

      Acount Acount;

      public Consumer(Acount Acount){

      this.Acount=Acount;

      }

      public void run(){

      while(true){ 

      int temp=(int)(Math.random()*1000);

      Acount.getMoney(temp);

      }

      }

      }

      總結(jié):

          其實(shí)多線程間的共享數(shù)據(jù)最主要的還是互斥,多個(gè)線程共享一個(gè)變量,針對(duì)變量的操作實(shí)現(xiàn)原子性即可。

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多