zoukankan      html  css  js  c++  java
  • 线程同步之模拟银行取款

      线程同步有三种办法,同步块,同步方法,可重入锁ReentrantLock。用实例说明:

      1.可重入锁ReentrantLock。  

    import java.util.Random;
    import java.util.concurrent.locks.ReentrantLock;
    
    
    public class WithDrawMoney implements Runnable{
        static int total;
        static int left;
        Random random = new Random();
        private final ReentrantLock lock = new ReentrantLock();
    
        public WithDrawMoney(int n) {
            super();
            this.total = n;
            left = total;
        }
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
            lock.lock();
            if (left > 0){
                int take_out = random.nextInt(100);
                if (take_out > left){
                    take_out = left;
                }
                System.out.printf("线程%s,目前余额为%d元,取走了%d元,剩余%d元
    ",Thread.currentThread().getName(),left,take_out,left-take_out);
                left -= take_out;
            }
            lock.unlock();
        }
        
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            WithDrawMoney bank = new WithDrawMoney(2000);
            for (int i=0; i<10; i++){
                Thread thread = new Thread(bank);
                thread.start();
            }
        }
    
    
    }

      2.同步方法。

      

    import java.util.Random;
    import java.util.concurrent.locks.ReentrantLock;
    
    
    public class WithDrawMoney implements Runnable{
        static int total;
        static int left;
        Random random = new Random();
    
        public WithDrawMoney(int n) {
            super();
            this.total = n;
            left = total;
        }
    
        @Override
        public synchronized void run() {
            // TODO Auto-generated method stub
            if (left > 0){
                int take_out = random.nextInt(100);
                if (take_out > left){
                    take_out = left;
                }
                System.out.printf("线程%s,目前余额为%d元,取走了%d元,剩余%d元
    ",Thread.currentThread().getName(),left,take_out,left-take_out);
                left -= take_out;
            }
        }
        
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            WithDrawMoney bank = new WithDrawMoney(2000);
            for (int i=0; i<10; i++){
                Thread thread = new Thread(bank);
                thread.start();
            }
        }
    
    
    }

      3.同步块

    import java.util.Random;
    import java.util.concurrent.locks.ReentrantLock;
    
    
    public class WithDrawMoney implements Runnable{
        static int total;
        static int left;
        Random random = new Random();
    
        public WithDrawMoney(int n) {
            super();
            this.total = n;
            left = total;
        }
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
            synchronized(WithDrawMoney.class){
                if (left > 0){
                    int take_out = random.nextInt(100);
                    if (take_out > left){
                        take_out = left;
                    }
                    System.out.printf("线程%s,目前余额为%d元,取走了%d元,剩余%d元
    ",Thread.currentThread().getName(),left,take_out,left-take_out);
                    left -= take_out;
                }
            }
        }
        
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            WithDrawMoney bank = new WithDrawMoney(200);
            for (int i=0; i<10; i++){
                Thread thread = new Thread(bank);
                thread.start();
            }
        }
    
    
    }
  • 相关阅读:
    SD卡的控制方法(指令集和控制时序)
    MDK的优化应用(转)
    SD卡的SPI模式的初始化顺序(转)
    SD卡读写扇区注意事项(转)
    MDK下调试时提示AXF文件无法导入的解决方法(转)
    把一个 int 数字 n 格式化成16进制的字符串(前面补零成0位)
    DB9 公头母头引脚定义及连接、封装
    RS232 DB9 公头 母头 串口引脚定义
    Codeforces 91C Ski Base 加边求欧拉回路数量
    Cocos Code IDE + Lua初次使用FastTiledMap的坑
  • 原文地址:https://www.cnblogs.com/ljy-1471914707/p/7016038.html
Copyright © 2011-2022 走看看