亲宝软件园·资讯

展开

Java多线程银行存钱

小虚竹and掘金 人气:0

一、题目描述

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

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

二、解题思路

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

写一个内部类Bank

写一个内部类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;
/**
 * 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("使用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;

/**
 * Description: 证明线程是存在缓存内存的
 *
 * @author xiaoxuzhu
 * @version 1.0
 *
 * <pre>
 * 修改记录:
 * 修改后版本	        修改人		修改日期			修改内容
 * 2022/5/14.1	    xiaoxuzhu		2022/5/14		    Create
 * </pre>
 * @date 2022/5/14
 */
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了。证实每个线程是存在缓存内存的

加载全部内容

相关教程
猜你喜欢
用户评论