Home  >  Article  >  Java  >  How to use multi-threading in Java to simulate the problem of depositing money in the bank system

How to use multi-threading in Java to simulate the problem of depositing money in the bank system

王林
王林forward
2023-05-04 18:28:071035browse

Multi-threading 6 (Simulating deposits in the banking system)

1. Topic

Simulate a simple banking system and use two different threads to deposit money into the same account.

Implementation: Use the synchronized keyword to modify the method of saving money to be synchronous.

2. Problem-solving ideas

Create a class: SynchronizedBankFrame, inherit the JFrame class

Write an internal class Bank

  • Definition An account variable to represent the account.

  • deposit(): A method to deposit money

  • getAccount(): A method to display the account balance.

Write an internal class Transfer to implement the Runnable interface

Implement the function of depositing money into the account in the run method.

The synchronization method is a method modified with the synchronized keyword.

JDK configures a built-in lock for each JAVA object. If the method is modified with the synchronized keyword, the built-in lock will protect the entire method.

3. Detailed code explanation

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
 *
 * 
 * 修改记录:
 * 修改后版本	        修改人		修改日期			修改内容
 * 2022/5/14.1	    xiaoxuzhu		2022/5/14		    Create
 * 
 * @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 

How to use multi-threading in Java to simulate the problem of depositing money in the bank system

The above is the detailed content of How to use multi-threading in Java to simulate the problem of depositing money in the bank system. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete