zoukankan      html  css  js  c++  java
  • Java 练习(线程的同步)

    银行有一个账户。
    有两个储户分别向同一个账户存3000元,每次存1000,存3次。每次存完打印账户余额。
    问题:该程序是否有安全问题,如果有,如何解决?

    分析:

    1. 是否是多线程问题?是,两个储户线程
    2. 是否有共享数据?有,账户(或账户余额)
    3. 是否有线程安全问题?有
    4. 需要考虑如何解决线程安全问题?同步机制:有三种方式。

    使用继承 Thread 方式

    package com.klvchen.exer;
    
    class Account{
        private double balance;
    
        public Account(double balance){
            this.balance = balance;
        }
    
        //存钱
        public synchronized void deposit(double amt) {
            if(amt > 0){
                balance += amt;
    
                try{
                    Thread.sleep(100);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
    
    
                System.out.println(Thread.currentThread().getName() + " 存钱成功,余额为: " + balance);
            }
        }
    
    }
    
    class Customer extends Thread{
    
        private Account acct;
    
        public Customer(Account acct){
            this.acct = acct;
        }
    
        @Override
        public void run(){
            for (int i = 0; i < 3; i++){
                acct.deposit(1000);
    
            }
        }
    }
    
    
    public class AccountTest {
        public static void main(String[] args) {
            Account acct = new Account(0);
            Customer c1 = new Customer(acct);
            Customer c2 = new Customer(acct);
    
            c1.setName("甲");
            c2.setName("乙");
    
            c1.start();
            c2.start();
        }
    }
    

    实现Runnable接口的方式

    package com.klvchen.exer;
    
    class Account{
        private double balance;
    
        public Account(double balance){
            this.balance = balance;
        }
    
        //存钱
        public synchronized void deposit(double amt) {
            if(amt > 0){
                balance += amt;
    
                try{
                    Thread.sleep(1000);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
    
    
                System.out.println(Thread.currentThread().getName() + " 存钱成功,余额为: " + balance);
            }
        }
    
    }
    
    class Customer implements Runnable{
    
        private Account acct;
    
        public Customer(Account acct){
            this.acct = acct;
        }
    
        @Override
        public void run(){
            for (int i = 0; i < 3; i++){
                acct.deposit(1000);
    
            }
        }
    }
    
    
    public class AccountTest {
        public static void main(String[] args) {
            Account acct = new Account(0);
            Customer c = new Customer(acct);
    
            Thread t1 = new Thread(c);
            Thread t2 = new Thread(c);
    
            t1.setName("甲");
            t2.setName("乙");
    
            t1.start();
            t2.start();
        }
    }
    
  • 相关阅读:
    POJ 2828 Buy Tickets (线段树 单点更新 变形)
    HDU 1754 I Hate It (线段树 单点更新)
    HDU 1166 敌兵布阵 (线段树 单点更新)
    sdut 2934 人活着系列之平方数 (完全背包变形)
    Codeforces Round #259 (Div. 2) C
    poj 1724 ROADS (bfs+优先队列)
    hdu 4901 The Romantic Hero (dp)
    MemSQL Start[c]UP 2.0
    BestCoder Round #2 1001 (简单处理)
    tc 2014 college tour 250 500
  • 原文地址:https://www.cnblogs.com/klvchen/p/14652903.html
Copyright © 2011-2022 走看看