Java利用多線程模擬銀行系統存錢問題
多線程6(模擬銀行系統存錢)
1.題目
模擬一個簡單的銀行系統,使用兩個不同的線程向同一個賬戶存錢。
實現:使用synchronized關鍵字,將存錢的方法修改成同步的。
2.解題思路
創建一個類:SynchronizedBankFrame,繼承JFrame類
寫一個內部類Bank
- 定義一個account變量,來表示賬戶。
- deposit():一個存錢的方法
- getAccount():顯示賬戶餘額的方法。
寫一個內部類Transfer,實現Runnable接口
在run方法中實現向賬戶存錢的功能。
同步方法就是用synchronized關鍵字修飾的方法。
JDK為每個JAVA對象配置瞭內置鎖,如果方法使用synchronized關鍵字修飾,內置鎖就會保護整個方法。
3.代碼詳解
SynchronizedBankFrame
package com.xiaoxuzhu; import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JButton; import javax.swing.JScrollPane; import javax.swing.JTextArea; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.GridLayout; import javax.swing.JLabel; import javax.swing.SwingConstants; import java.awt.Font; import javax.swing.UIManager; /** * Description: * * @author xiaoxuzhu * @version 1.0 * * <pre> * 修改記錄: * 修改後版本 修改人 修改日期 修改內容 * 2022/5/14.1 xiaoxuzhu 2022/5/14 Create * </pre> * @date 2022/5/14 */ public class SynchronizedBankFrame extends JFrame { /** * */ private static final long serialVersionUID = 2671056183299397274L; private JPanel contentPane; private JTextArea thread1TextArea; private JTextArea thread2TextArea; /** * Launch the application. */ public static void main(String[] args) { try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); } catch (Throwable e) { e.printStackTrace(); } EventQueue.invokeLater(new Runnable() { public void run() { try { SynchronizedBankFrame frame = new SynchronizedBankFrame(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public SynchronizedBankFrame() { setTitle("使用Synchronized實現線程同步"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(new BorderLayout(0, 0)); JPanel buttonPanel = new JPanel(); contentPane.add(buttonPanel, BorderLayout.SOUTH); JButton startButton = new JButton("開始存錢"); startButton.setFont(new Font("微軟雅黑", Font.PLAIN, 16)); startButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { do_button_actionPerformed(arg0); } }); buttonPanel.add(startButton); JPanel processPanel = new JPanel(); contentPane.add(processPanel, BorderLayout.CENTER); processPanel.setLayout(new GridLayout(1, 2, 5, 5)); JPanel thread1Panel = new JPanel(); processPanel.add(thread1Panel); thread1Panel.setLayout(new BorderLayout(0, 0)); JLabel thread1Label = new JLabel("一號線程"); thread1Label.setFont(new Font("微軟雅黑", Font.PLAIN, 16)); thread1Label.setHorizontalAlignment(SwingConstants.CENTER); thread1Panel.add(thread1Label, BorderLayout.NORTH); JScrollPane thread1ScrollPane = new JScrollPane(); thread1Panel.add(thread1ScrollPane, BorderLayout.CENTER); thread1TextArea = new JTextArea(); thread1TextArea.setFont(new Font("微軟雅黑", Font.PLAIN, 16)); thread1ScrollPane.setViewportView(thread1TextArea); JPanel thread2Panel = new JPanel(); processPanel.add(thread2Panel); thread2Panel.setLayout(new BorderLayout(0, 0)); JLabel thread2Label = new JLabel("二號線程"); thread2Label.setFont(new Font("微軟雅黑", Font.PLAIN, 16)); thread2Label.setHorizontalAlignment(SwingConstants.CENTER); thread2Panel.add(thread2Label, BorderLayout.NORTH); JScrollPane thread2ScrollPane = new JScrollPane(); thread2Panel.add(thread2ScrollPane, BorderLayout.CENTER); thread2TextArea = new JTextArea(); thread2TextArea.setFont(new Font("微軟雅黑", Font.PLAIN, 16)); thread2ScrollPane.setViewportView(thread2TextArea); } protected void do_button_actionPerformed(ActionEvent arg0) { Bank bank = new Bank(); Thread thread1 = new Thread(new Transfer(bank, thread1TextArea)); thread1.start(); Thread thread2 = new Thread(new Transfer(bank, thread2TextArea)); thread2.start(); } private class Transfer implements Runnable { private Bank bank; private JTextArea textArea; public Transfer(Bank bank, JTextArea textArea) {// 初始化變量 this.bank = bank; this.textArea = textArea; } public void run() { for (int i = 0; i < 10; i++) {// 向賬戶中存入10次錢 bank.deposit(10);// 向賬戶中存入10塊錢 String text = textArea.getText();// 獲得文本域中的文本 // 在文本域中顯示賬戶中的餘額 textArea.setText(text + "賬戶的餘額是:" + bank.getAccount() + "\n"); } } } private class Bank { private int account = 100;// 每個賬戶的初始金額是100元 public synchronized void deposit(int money) {// 向賬戶中存入money元 account += money; } public int getAccount() {// 查詢賬戶餘額 return account; } } }
到此這篇關於Java利用多線程模擬銀行系統存錢問題的文章就介紹到這瞭,更多相關Java多線程銀行存錢內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!