`

JAVA每日一题22

阅读更多

     题目:有两各线程accountant和cashier,他俩共同拥有一个帐本,他俩都可以使用saveOrTake(int mumber)对账本进行访问,会计使用saveOrTake方法时,向账本上写入存钱记录;出纳使用saveOrTake方法时,向账本写入取钱记录。因此,当会计正在使用saveOrTake方法时,出纳被禁止使用,反之也是这样。

    

class Bank  implements Runnable
{  
   int money=300;
   int time1,time2;
   Thread accountant,cashier;
   public Bank()
   { 
      accountant=new Thread(this);
      accountant.setName("会计");
      cashier=new Thread(this);
      cashier.setName("出纳");   
   }
  public void run()
   { 
       for(int i=1;i<=3;i++) //从周一到周三会计和出纳都要使用saveOrTake方法。
          {  
             if(Thread.currentThread()==accountant)  
              {
                 time1=i;
                 saveOrTake(30);  //accountant线程占有CUP资源期间调用了同步方法。
              }
             else if(Thread.currentThread()==cashier)  
              {
                 time2=i;
                 saveOrTake(30); //cashier线程占有CUP资源期间调用了同步方法。
              }
          }
   }
  public synchronized void saveOrTake(int number) //同步方法。
   {  
       if(Thread.currentThread()==accountant)    //如果是accountant占有CPU资源。
       {  
          System.out.printf("%s%d\n","今天是星期",time1);
          for(int i=1;i<=3;i++)        //accountant使用该方法存入90万,存入30万,稍歇一下,
            {                                          //这时cashier仍不能使用该方法,
                money=money+number;                   //因为accountant还没使用该方法。
                try {  
                       Thread.sleep(1000);         
                    }             
                catch(InterruptedException e)
                    {
                    }
                System.out.printf("%s,目前帐上有%d万圆\n","我是"+accountant.getName(),money);
            }
       }
      else if(Thread.currentThread()==cashier) //如果是cashier占有CPU资源。
      {    
           System.out.printf("%35s%d\n","今天是星期",time2);
           for(int i=1;i<=2;i++)        //cashier使用该方法取出30元,取出15元,稍歇一下,
            {                                       //这时accountant仍不能使用该方法,
               money=money-number/2;                //因为cashier还没使用完该方法。
               try  {
                      Thread.sleep(1000);    
                    }
               catch(InterruptedException e)
                    {
                    }
               System.out.printf("%35s,目前帐上有%d万圆\n","我是"+cashier.getName(),money);
            }
      }
   }
}
class Example
{
    public static void main(String args[ ])
    {
        Bank a=new Bank();
        a.accountant.start();
        a.cashier.start();
   }
}

 

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics