Java多线程模拟银行系统存钱问题详解

2022-11-13 17:11:34 多线程 存钱 详解

一、题目描述

题目:模拟一个简单的银行系统,使用两个不同的线程向同一个账户存钱。

实现:使用特殊域变量volatile实现同步。

二、解题思路

创建一个类:SynchronizedBankFrame,继承JFrame类

写一个内部类Bank

  • 定义一个account变量,来表示账户。
  • deposit():一个存钱的方法
  • getAccount():显示账户余额的方法。

写一个内部类Transfer,实现Runnable接口

在run方法中实现向账户存钱的功能。

volatile关键字为变量访问提供了一种免机制。使用volatile关键字修饰变量,每次使用该变量就要重新计算,而不是使用寄存器中的值。

volatile不会提供原子操作,也不能用来修饰final类型的变量。

三、代码详解

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;

public class SynchronizedBankFrame extends JFrame {

    
    private static final long serialVersionUID = 2671056183299397274L;
    private JPanel contentPane;
    private JTextArea thread1TextArea;
    private JTextArea thread2TextArea;

    
    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();
                }
            }
        });
    }

    
    public SynchronizedBankFrame() {
        setTitle("使用volatile实现线程同步");
        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++) {
                bank.deposit(10);
                String text = textArea.getText();
                textArea.setText(text + "账户的余额是:" + bank.getAccount() + "\n");
            }
        }
    }

    private class Bank {
        private volatile int account = 100;// 将域变量用volatile修饰

        public void deposit(int money) {// 向账户中存钱
            account += money;
        }

        public int getAccount() {// 获得账户余额
            return account;
        }
    }

}

多学一个知识点

每个线程是存在缓存内存的。且缓存内存是对其他线程不可见的。这就是内存不可见问题。

来验证下

package com.xiaoxuzhu;


public class VolatileDemo {


    public static void main(String[] args) {
        ThreadDemo threadDemo = new ThreadDemo();
        //启动线程  改值
        new Thread(threadDemo).start();

        while (true){
            if(threadDemo.isFlag()){
                System.out.println("主线程读到的flag是true");
                break;
            }
        }
    }

    static class  ThreadDemo implements Runnable{
        private boolean flag = false;

        public boolean isFlag() {
            return flag;
        }

        public void setFlag(boolean flag) {
            this.flag = flag;
        }

        @Override
        public void run() {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            flag = true;
            System.out.println("flag 线程执行改为:"+isFlag());
        }
    }
}

主线程得到的是false,新线程已经把值修改为true了。证实每个线程是存在缓存内存的

到此这篇关于Java多线程模拟银行系统存钱问题详解的文章就介绍到这了,更多相关Java多线程银行存钱内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关文章