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

📄 exercise19_5.java

📁 java程序设计 机械工业出版社 书籍代码
💻 JAVA
字号:
// Exercise19_5.java: Demonstrate resource conflict
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;

public class Exercise19_5 extends JApplet {
  public void init() {
    Container container = getContentPane();
    container.setLayout(new GridLayout(1, 2));
    DisplayPanel syncPanel = new DisplayPanel(DisplayPanel.SYNC);
    syncPanel.setBorder(new TitledBorder("Synchronized Threads"));
    container.add(syncPanel);
    DisplayPanel nosyncPanel = new DisplayPanel(DisplayPanel.NOSYNC);
    nosyncPanel.setBorder(new TitledBorder("Unsynchronized Threads"));
    container.add(nosyncPanel);
  }

  // Main method
  public static void main(String[] args) {
    // Create a frame
    JFrame frame = new JFrame(
      "Exercise19_5: Demonstrate Synchronization");

    // Create an instance of the applet
    Exercise19_5 applet = new Exercise19_5();

    // Add the applet instance to the frame
    frame.getContentPane().add(applet, BorderLayout.CENTER);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Invoke init() and start()
    applet.init();
    applet.start();

    // Display the frame
    frame.setSize(400, 300);
    frame.setVisible(true);
  }
}

class DisplayPanel extends JPanel implements ActionListener {
  public static int SYNC = 0;
  public static int NOSYNC = 1;
  private int mode = SYNC;
  private JTextArea jta = new JTextArea();
  private Account account = new Account();
  private JButton jbt = new JButton("Add a penny 100 times");
  Thread[] thread = new Thread[100];

  public DisplayPanel(int mode) {
    this.mode = mode;
    JScrollPane jsp = new JScrollPane(jta = new JTextArea());
    setLayout(new BorderLayout());
    add(jsp, BorderLayout.CENTER);
    add(jbt, BorderLayout.SOUTH);

    // Register listener
    jbt.addActionListener(this);

    // Display initial balance
    jta.append("Balance: " + account.getBalance() + '\n');
  }

  public int getMode() {
    return mode;
  }

  public Account getAccount() {
    return account;
  }

  public void actionPerformed(ActionEvent e) {
    runThreads();
  }

  public void runThreads() {
    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(this), "t");
      thread[i].start();
    }

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

    jta.append("Balance: " + account.getBalance() + '\n');
  }
}

// A thread for adding a penny to the piggy account
class AddAPennyThread extends Thread {
  int mode;
  Account account;

  public AddAPennyThread(DisplayPanel display) {
    this.mode = display.getMode();
    this.account = display.getAccount();
  }

  public void run() {
    if (mode == DisplayPanel.SYNC)
      addAPennyWithSync(account);
    else
      addAPennyWithoutSync(account);
  }

  // Synchronize: add a penny one at a time
  private static synchronized void addAPennyWithSync
    (Account account) {
    account.deposit(1);
  }

  // Unsynchronize: add a penny one at a time
  private static void addAPennyWithoutSync(Account account) {
    account.deposit(1);
  }
}

// A class for piggy account
class Account {
  private int balance = 0;

  public int getBalance() {
    return balance;
  }

  public 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(5);
    }
    catch (InterruptedException ex) {
    }

    balance = newBalance;
  }
}

⌨️ 快捷键说明

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