線程的互斥和協作
阿新 • • 發佈:2018-08-11
print read star stat code ext cep mon ESS
public class Synchronized{ static class Account{ private double money = 1000.0d; //異步存錢 public void noSynDesposit(double fFees){ System.out.println("Account noSynDesposit begin! money = "+ this.money + "; fFees = "+fFees); System.out.println("noSynDesposit sleep begin");try{ Thread.sleep(3000); }catch(InterruptedException e){ e.printStackTrace(); } System.out.println("noSynDesposit end"); this.money = this.money + fFees; System.out.println("Account noSynDesposit end! money = "+ this.money); } //異步取錢 public void noSynWithdraw(double fFees){ System.out.println("Account noSynWithdraw begin! money = "+ this.money + "; fFees = "+fFees); System.out.println("noSynWithdraw sleep begin"); try{ Thread.sleep(3000); }catch(InterruptedException e){ e.printStackTrace(); } System.out.println("noSynWithdraw sleep end"); this.money = this.money - fFees; System.out.println("Account noSynWithdraw end! money = "+ this.money); } //同步存錢 public synchronized void synDesposit(double fFees){ System.out.println("Account synDesposit begin! money = "+ this.money + "; fFees = "+fFees); System.out.println("synDesposit sleep begin"); try{ Thread.sleep(3000); }catch(InterruptedException e){ e.printStackTrace(); } System.out.println("synDesposit end"); this.money = this.money + fFees; System.out.println("Account synDesposit end! money = "+ this.money); } //同步取錢 public synchronized void synWithdraw(double fFees){ System.out.println("Account synWithdraw begin! money = "+ this.money + "; fFees = "+fFees); System.out.println("synWithdraw sleep begin"); try{ Thread.sleep(3000); }catch(InterruptedException e){ e.printStackTrace(); } System.out.println("synWithdraw sleep end"); this.money = this.money - fFees; System.out.println("Account synWithdraw end! money = "+ this.money); } } static class AccessThread extends Thread{ private Account account = null; private String method = ""; public AccessThread(Account account, String method){ this.account = account; this.method = method; } public void run(){ if(method.equals("noSynDesposit")){ account.noSynDesposit(500.0); }else if(method.equals("noSynWithdraw")){ account.noSynWithdraw(200.0); }else if(method.equals("synDesposit")){ account.synDesposit(500.0); }else if(method.equals("synWithdraw")){ account.synWithdraw(200.0); } } } public static void main(String[] args){ //線程的運行具有不確定性,與啟動順序無關,取決於JVM,所以異步就可能出現 Account account = new Account(); //同一個賬戶多個線程調用不同的方法,修改同一個變量值 System.out.println("account 1 :"+account.toString()); Thread threadA = new AccessThread(account, "noSynDesposit"); Thread threadB = new AccessThread(account, "noSynWithdraw"); threadA.start(); threadB.start(); try{ //waits for this thread to die threadA.join(); threadB.join(); }catch(InterruptedException e){ e.printStackTrace(); } System.out.println(); account = new Account(); System.out.println("account 2 :"+account.toString()); Thread threadC= new AccessThread(account, "synDesposit"); Thread threadD = new AccessThread(account, "synWithdraw"); threadC.start(); threadD.start(); } }
G:\maul keyboard\thread>java Synchronized account 1 :Synchronized$Account@153bcbc8 Account noSynDesposit begin! money = 1000.0; fFees = 500.0 Account noSynWithdraw begin! money = 1000.0; fFees = 200.0 noSynWithdraw sleep begin noSynDesposit sleep begin noSynWithdraw sleep end Account noSynWithdraw end! money = 800.0 noSynDesposit end Account noSynDesposit end! money = 1300.0 account 2 :Synchronized$Account@1b61d282 Account synDesposit begin! money = 1000.0; fFees = 500.0 synDesposit sleep begin synDesposit end Account synDesposit end! money = 1500.0 Account synWithdraw begin! money = 1500.0; fFees = 200.0 synWithdraw sleep begin synWithdraw sleep end Account synWithdraw end! money = 1300.0 G:\maul keyboard\thread>
線程的互斥和協作