免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1688 | 回复: 0
打印 上一主题 下一主题

多线程模拟对同一个账户,一个来存款一个来取款 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-13 11:01 |只看该作者 |倒序浏览
多线程访问共享数据
  1. package thread;

  2. /**
  3. * 多线程模拟一个存 一个取,存完通知另一个来取
  4. */
  5. public class ImitateDepositDrawTheadTest {
  6.      
  7.     public static void main(String[] args) {
  8.          
  9.         final DepositOrWithdrawBusiness business = new DepositOrWithdrawBusiness(1000);
  10.         new Thread(new Runnable(){
  11.             @Override
  12.             public void run() {
  13.                 while(true){
  14.                     business.deposit();
  15.                 }
  16.             }
  17.         }).start();
  18.         new Thread(new Runnable(){
  19.             @Override
  20.             public void run() {
  21.                 while(true){
  22.                     business.withdraw();
  23.                 }
  24.             }
  25.         }).start();
  26.          
  27.          
  28.     }
  29. }
  30. class DepositOrWithdrawBusiness{
  31.     int balance = 1000;
  32.     public DepositOrWithdrawBusiness(int balance){
  33.         this.balance = balance;
  34.     }
  35.     boolean isWithdraw = false;
  36.     public synchronized void deposit(){
  37.         if(isWithdraw){
  38.             try {
  39.                 this.wait();
  40.             } catch (InterruptedException e) {
  41.                 e.printStackTrace();
  42.             }
  43.         }
  44.         balance = balance+300;
  45.         System.out.println(Thread.currentThread().getName()+ " deposit $300,now balance:" + balance);
  46.         isWithdraw=true;
  47.         this.notify();
  48.          
  49.     }
  50.     public synchronized void withdraw(){
  51.         if(!isWithdraw){
  52.             try {
  53.                 this.wait();
  54.             } catch (InterruptedException e) {
  55.                 e.printStackTrace();
  56.             }
  57.         }
  58.         balance = balance-300;
  59.         System.out.println(Thread.currentThread().getName()+ " withdraw $300,now balance:" + balance);
  60.         isWithdraw=false;
  61.         this.notify();
  62.     }
  63. }
复制代码
运行结果:
Thread-0 deposit $300,now balance:1300
Thread-1 withdraw $300,now balance:1000
Thread-0 deposit $300,now balance:1300
Thread-1 withdraw $300,now balance:1000
Thread-0 deposit $300,now balance:1300
Thread-1 withdraw $300,now balance:1000
Thread-0 deposit $300,now balance:1300
Thread-1 withdraw $300,now balance:1000
Thread-0 deposit $300,now balance:1300
Thread-1 withdraw $300,now balance:1000
Thread-0 deposit $300,now balance:1300
Thread-1 withdraw $300,now balance:1000
Thread-0 deposit $300,now balance:1300
Thread-1 withdraw $300,now balance:1000
Thread-0 deposit $300,now balance:1300
Thread-1 withdraw $300,now balance:1000
。。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP