⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exercise19_4.java

📁 java程序设计导论(daniel liang著) 所有偶数课后习题答案
💻 JAVA
字号:
public class Exercise19_4 {
  private Account account = new Account();
  private Thread[] thread = new Thread[100];

  public static void main(String[] args) {
    Exercise19_4 test = new Exercise19_4();
    System.out.println("What is balance ? " +
      test.account.getBalance());
  }

  public Exercise19_4() {
    ThreadGroup g = new ThreadGroup("group");
    boolean done = false;

    // Create and launch 100 threads
    for (int i = 0; i < 100; i++) {
      thread[i] = new Thread(g, new AddAPennyThread(), "t");
      thread[i].start();
    }

    // Check if all the threads are finished
    while(!done)
      if (g.activeCount() == 0)
        done = true;
  }


  // A thread for adding a penny to the piggy account
  class AddAPennyThread extends Thread {
    public void run() {
      synchronized (account) {
        account.deposit(1);
      }
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -