標籤:
多線程共用資料的方式:
1,如果每個線程執行的代碼相同,可以使用同一個Runnable對象,這個Runnable對象中有那個共用資料,例如,賣票系統就可以這麼做。
2,如果每個線程執行的代碼不同,這時候需要用不同的Runnable對象,例如,設計4個線程。其中兩個線程每次對j增加1,另外兩個線程對j每次減1,銀行存取款
有兩種方法來解決此類問題:
將共用資料封裝成另外一個對象,然後將這個對象逐一傳遞給各個Runnable對象,每個線程對共用資料的操作方法也分配到那個對象身上完成,這樣容易實現針對資料進行各個操作的互斥和通訊
將Runnable對象作為一個類的內部類,共用資料作為這個類的成員變數,每個線程對共用資料的操作方法也封裝在外部類,以便實現對資料的各個操作的同步和互斥,作為內部類的各個Runnable對象調用外部類的這些方法。
下面逐一介紹
每個線程執行的代碼相同,可以使用同一個Runnable對象
賣票系統demo:
package com.tgb.hjy;/** * 多線程共用資料-賣票系統 * @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("當前票數為:"+ticket); } } }
簡單的多線程間資料共用,每個線程執行的代碼不同,用不同的Runnable對象
設計4個線程。
其中兩個線程每次對j增加1,另外兩個線程對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); } }
銀行存取款執行個體:
package com.tgb.hjy;public class Acount { private int money; public Acount(int money){ this.money=money; } public synchronized void getMoney(int money){ //注意這個地方必須用while迴圈,因為即便再存入錢也有可能比取的要少 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+" 共計:"+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);}}}
總結:
其實多線程間的共用資料最主要的還是互斥,多個線程共用一個變數,針對變數的操作實現原子性即可。
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
JAVA 並發編程-多個線程之間共用資料(六)