线程的互斥和协作
Posted celine
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了线程的互斥和协作相关的知识,希望对你有一定的参考价值。
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 hread>java Synchronized account 1 :[email protected] 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 :[email protected] 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 hread>
以上是关于线程的互斥和协作的主要内容,如果未能解决你的问题,请参考以下文章