Java并发编程实战(使用synchronized实现同步方法)

发表于:2015-7-23 09:21

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:有梦想的小人物    来源:51Testing软件测试网采编

分享:
  5:实现一个ATM模拟类Bank,它使用subtractAmount()方法对账户的余额进行扣除,实现Runabl接口。
  具体代码:
1 public class Bank implements Runnable {
2
3     /**
4      * The account affected by the operations
5      */
6     private Account account;
7
8     /**
9      * Constructor of the class. Initializes the account
10      * @param account The account affected by the operations
11      */
12     public Bank(Account account) {
13         this.account=account;
14     }
15
16
17     /**
18      * Core method of the Runnable
19      */
20     public void run() {
21         for (int i=0; i<100; i++){
22             account.subtractAmount(1000);
23         }
24     }
25
26 }
  6:实现公司模拟类,调用addAmount()方法进行存钱,实现Runabl接口。
  具体代码:
1 public class Company implements Runnable {
2
3     /**
4      * The account affected by the operations
5      */
6     private Account account;
7
8     /**
9      * Constructor of the class. Initializes the account
10      * @param account the account affected by the operations
11      */
12     public Company(Account account) {
13         this.account=account;
14     }
15
16     /**
17      * Core method of the Runnable
18      */
19     public void run() {
20         for (int i=0; i<100; i++){
21             account.addAmount(1000);
22         }
23     }
  7:在主方法中调用测试:通过线程的join方法,在存期那,取钱线程模拟完毕后打印出结构。
1 public class Main {
2
3     /**
4      * Main method of the example
5      * @param args
6      */
7     public static void main(String[] args) {
8         // Creates a new account ...
9         Account    account=new Account();
10         // an initialize its balance to 1000
11         account.setBalance(1000);
12
13         // Creates a new Company and a Thread to run its task
14         Company    company=new Company(account);
15         Thread companyThread=new Thread(company);
16         // Creates a new Bank and a Thread to run its task
17         Bank bank=new Bank(account);
18         Thread bankThread=new Thread(bank);
19
20         // Prints the initial balance
21         System.out.printf("Account : Initial Balance: %f\n",account.getBalance());
22
23         // Starts the Threads
24         companyThread.start();
25         bankThread.start();
26
27         try {
28             // Wait for the finalization of the Threads
29             companyThread.join();
30             bankThread.join();
31             // Print the final balance
32             System.out.printf("Account : Final Balance: %f\n",account.getBalance());
33         } catch (InterruptedException e) {
34             e.printStackTrace();
35         }
36     }
37 }
  结果,相同时间内,存与取执行后应该是相等的。如果我们在方法中不去使用synchronized关键字,那么得出的结果就不对了。
  Account : Initial Balance: 1000.000000
  Account : Final Balance: 1000.000000
22/2<12
重磅发布,2022软件测试行业现状调查报告~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计

法律顾问:上海漕溪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2023
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号