package chapter29;import java.util.concurrent.*;public class AccountWithSync {
  private static Account account = new Account();  public static void main(String[] args) {
    ExecutorService executor = Executors.newCachedThreadPool();
    AddAPennyTask ta = new AddAPennyTask();
    MinuTask tb = new MinuTask();
    // Create and launch 10 threads
    for (int i = 0; i < 10; i++) {
      executor.execute(new AddAPennyTask());
    }
    
      if(account.getBalance() >= 5) {
        executor.execute(new MinuTask());
    }    executor.shutdown();    // Wait until all tasks are finished
    while (!executor.isTerminated()) {
    }    System.out.println("What is balance? " + account.getBalance());
  }  // A thread for adding a penny to the account
  private static class AddAPennyTask implements Runnable {
    public void run() {
      account.deposit(1);
      System.out.println("add 1");
    }
  }
  private static class MinuTask implements Runnable {
      public void run() {
          account.takeout(3);
          System.out.println("take out 3.");
      }
  }
    // An inner class for account
  private static class Account {
    private int balance = 0;    public int getBalance() {
      return balance;
    }    public synchronized void deposit(int amount) {
           int newBalance = balance + amount;
      // This delay is deliberately added to magnify the
      // data-corruption problem and make it easy to see.
        try {                                  //只有去掉takeout()才有机会抢到资源
        }
        catch (InterruptedException ex) {
        }
            balance = newBalance;
    }
    
    public synchronized void takeout(int amount) {
        int newBalance = balance - amount;
        try {                                        //去掉后只能在最后两次中抢到资源
            Thread.sleep(100);
        }
        catch (InterruptedException ex) {
        }
        balance = newBalance;
    }
    
  }
}

解决方案 »

  1.   

    public static void sleep(long millis)
                      throws InterruptedException在指定的毫秒数内让当前正在执行的线程休眠(暂停执行),此操作受到系统计时器和调度程序精度和准确性的影响。该线程不丢失任何监视器的所属权。 import java.util.concurrent.*;public class AccountWithSync 
    {
      private static Account account = new Account();  public static void main(String[] args)
      {
        ExecutorService executor = Executors.newCachedThreadPool();
        AddAPennyTask ta = new AddAPennyTask();
        MinuTask tb = new MinuTask();
        // Create and launch 10 threads
        for (int i = 0; i < 10; i++)
        {
          executor.execute(new AddAPennyTask());
        }
        
          if(account.getBalance() >= 5) 
          {
            executor.execute(new MinuTask());
        }    executor.shutdown();    // Wait until all tasks are finished
        while (!executor.isTerminated())
        {    }    System.out.println("What is balance? " + account.getBalance());
      }  // A thread for adding a penny to the account
      private static class AddAPennyTask implements Runnable
      {
        public void run()
        {
          account.deposit(1);
          System.out.println("add 1");
        }
      }
      private static class MinuTask implements Runnable
      {
          public void run() {
              account.takeout(3);
              System.out.println("take out 3.");
          }
      }
        // An inner class for account
      private static class Account 
      {
        private int balance = 0;    public int getBalance()
        {
          return balance;
        }    public synchronized void deposit(int amount) 
        {
               int newBalance = balance + amount;
          // This delay is deliberately added to magnify the
          // data-corruption problem and make it easy to see.
            try
            {    
                Thread.sleep(100); //只有去掉takeout()才有机会抢到资源
            }
            catch (InterruptedException ex)
            {
            }
                balance = newBalance;
        }
        
        public synchronized void takeout(int amount)
        {
            int newBalance = balance - amount;
            try {                                        //去掉后只能在最后两次中抢到资源
                Thread.sleep(100);
            }
            catch (InterruptedException ex) 
    {
            }
            balance = newBalance;
        }
        
      }

      

  2.   

    synchronized方法里面可以不用sleep吗 求高手指点
      

  3.   

    同步代码段要求尽可能的简短!你还往里面Sleep,你不嫌卡啊~~~
      

  4.   

    可是好多地方都sleep,这怎么解释啊