A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 左建飞 中级黑马   /  2012-8-2 12:59  /  1758 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 左建飞 于 2012-8-3 10:06 编辑
  1. public class BankDemo {

  2.         /**
  3.          * 毕老师基础视频11天11.
  4.          *需求:银行有一个金库。
  5.          *有两个储户分别存300元,每次存100,存3次。
  6.          */
  7. public static void main(String[] args)
  8.         {
  9.                 Cus c=new Cus();
  10.                 Thread t1=new Thread(c);//两个储户同时向银行存储。
  11.                 Thread t2=new Thread(c);
  12.                 t1.start();
  13.                 t2.start();
  14.         }

  15. }
  16. class Bank
  17. {
  18.         private int sum;//此处的sum相当与银行的总存款。
  19.         Object obj=new Object();
  20.         public void add(int n)
  21.         {
  22.                 synchronized(obj)
  23.                 {
  24.                         sum=sum+n;
  25.                         try{Thread.sleep(10);}catch(InterruptedException e){}
  26.                         System.out.println("sum="+sum);
  27.                 }
  28.         }
  29. }
  30. class Cus implements Runnable//储户类?
  31. {
  32.         Bank b=new Bank();
  33.         public void run()
  34.         {
  35.                 for (int i = 0; i < 3; i++)
  36.                 {
  37.                         b.add(100);
  38.                 }
  39.         }
  40. }
复制代码
我想让程序的结果既显示银行的总存款,又显示每个储户的总存款。
应该怎么设计?加了线程后,这个问题把本菜鸟想的快崩溃了。求高手解答。

3 个回复

倒序浏览

RE: 一个多线程小程序的改造问题

public void add(int n)
{
     String s=Thread.currentThread().getName();
   
     synchronized(obj)
     {
         if (s.equals("Thread-0"))
         {
             sum1=sum1+n;
             try{Thread.sleep(10);}catch(InterruptedException e){}
             System.out.println("sum1="+sum1);         
         }
        else
         {
            sum2=sum2+n;
            try{Thread.sleep(10);}catch(InterruptedException e){}
            System.out.println("sum2="+sum2);
         }
     
    }
            
  }
感觉将两个线程分开来就好了,不知道这样写合不合适,另外算总的sum,在Bank类中在写个得到sum1和sum2和的方法就可以
回复 使用道具 举报
  1. package cn.itcast.demo;

  2. import java.lang.Thread.State;

  3. public class Test {

  4.         /**
  5.          *
  6.          * 毕老师基础视频11天11.
  7.          *
  8.          *需求:银行有一个金库。
  9.          *
  10.          *有两个储户分别存300元,每次存100,存3次。
  11.          */

  12.         public static void main(String[] args) {

  13.                 Cus c = new Cus();
  14.                 Thread t1 = new Thread(c);// 两个储户同时向银行存储。

  15.                 Thread t2 = new Thread(c);
  16.                 t1.start();
  17.                 t2.start();


  18.         }

  19. }

  20. class Bank

  21. {
  22.         private int sum;// 此处的sum相当与银行的总存款。
  23.         private int x,y;
  24.         Object obj = new Object();

  25.         public void add(int n) {

  26.                 synchronized (obj)

  27.                 {
  28.                         sum = sum + n;
  29.                         String str = Thread.currentThread().getName();
  30.                         if(str.equals("Thread-0"))
  31.                         {
  32.                                 try {
  33.                                         x = x + n;
  34.                                 Thread.sleep(10);
  35.                         } catch (InterruptedException e) {
  36.                         }
  37.                         System.out.println(Thread.currentThread().getName()+"存款:"+x+";"+"银行总余额:" + sum);
  38.                         }
  39.                         else
  40.                         {
  41.                                 if(str.equals("Thread-1"))
  42.                                 {
  43.                                        
  44.                                         try {
  45.                                                 y = y + n;
  46.                                         Thread.sleep(10);
  47.                                 } catch (InterruptedException e) {
  48.                                 }
  49.                                 System.out.println(Thread.currentThread().getName()+"存款:"+y+";"+"银行总余额:" + sum);
  50.                                 }
  51.                         }

  52.                 }

  53.         }

  54. }

  55. class Cus implements Runnable// 储户类?

  56. {
  57.         Bank b = new Bank();

  58.         public void run()

  59.         {

  60.                 for (int i = 0; i < 3; i++) {

  61.                         b.add(100);
  62.                        
  63.                 }

  64.         }

  65. }
复制代码

QQ截图20120802185649.png (10.94 KB, 下载次数: 18)

QQ截图20120802185649.png
回复 使用道具 举报
问题已解决
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马