zoukankan      html  css  js  c++  java
  • 银行叫号系统线程安全版

    使用synchronized(object)实现线程安全

    package com.dwz.concurrency.chapter7;
    
    public class TicketWindowRunnable implements Runnable {
        private int index = 1;
        private final static int MAX = 500;
        private final Object MONITOR = new Object();
        
        @Override
        public void run() {
            while(true) {
                synchronized (MONITOR) {
                    if(index > MAX) {
                        break;
                    }
                    
                    try {
                        Thread.sleep(5);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread() + " 的号码是:" + (index++));
                }
            }
        }
    
    }

    测试代码

    package com.dwz.concurrency.chapter7;
    
    public class BankVersion2 {
        public static void main(String[] args) {
            TicketWindowRunnable wr = new TicketWindowRunnable();
            Thread thread1 = new Thread(wr, "柜台一");
            Thread thread2 = new Thread(wr, "柜台二");
            Thread thread3 = new Thread(wr, "柜台三");
            
            thread1.start();
            thread2.start();
            thread3.start();
        }
    }

    使用synchronized给方法签名加锁实现线程安全

    package com.dwz.concurrency.chapter7;
    
    public class SynchronizedRunnable2 implements Runnable {
        private int index = 1;
        private final static int MAX = 500;
        private final Object MONITOR = new Object();
        
        @Override
        public void run() {
            while(true) {
                if(ticket()) {
                    break;
                }
            }
        }
        
        private synchronized boolean ticket() {
            if(index > MAX) {
                return true;
            }
            
            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread() + " 的号码是:" + (index++));
            return false;
        }
    }

    使用synchronized(this)实现线程安全

    package com.dwz.concurrency.chapter7;
    
    public class SynchronizedRunnable2 implements Runnable {
        private int index = 1;
        private final static int MAX = 500;
        private final Object MONITOR = new Object();
        
        @Override
        public void run() {
            while(true) {
                if(ticket()) {
                    break;
                }
            }
        }
        private boolean ticket() {
            synchronized (this) {
                if(index > MAX) {
                    return true;
                }
                
                try {
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //index++ => index = index + 1
                //1.get field index
                //2.index = index + 1
                //3.put field index
                System.out.println(Thread.currentThread() + " 的号码是:" + (index++));
                return false;
            }
        }
    }

    使用synchronized加锁的时候尽量减小加锁部分代码的粒度,在保证必要的数据安全前提下减少程序执行时间

  • 相关阅读:
    FHQ Treap(无旋 Treap)详解
    [CSP-S 2021] 廊桥分配 题解
    Splay Tree(伸展树)详解
    爬虫工程师也应该会的 NodeJS 知识(一)
    网站加密和混淆技术总结
    ip地址
    索引
    go try。。。catch
    python常见问题
    python实现发布订阅
  • 原文地址:https://www.cnblogs.com/zheaven/p/12057959.html
Copyright © 2011-2022 走看看