互斥场景重入锁处理方案

处理方案一:map+超时重入锁数据结构(注:该方案并发时可以获取到锁进行操作)

  • 核心逻辑模拟--加超时时间

    复制代码
    `import java.util.Collections;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class Test {
        public static void main(String[] args) {
    
            final Long adminId = 123L;
            final Map<Long, ReentrantLock> locks = Collections.synchronizedMap(new HashMap<>());
    
            new Thread(new Runnable() {
                @Override
                public void run() {
                    ReentrantLock lock = locks.get(adminId);
                    if (lock == null) {
                        lock = new ReentrantLock();
                        locks.put(adminId, lock);
                    }
                    try {
                        if (lock.tryLock(5, TimeUnit.SECONDS)) {
                            try {
                                System.out.println("11核心逻辑处理开始");
                                Thread.sleep(10000);
                                System.out.println("11核心逻辑处理结束");
                            } catch (Throwable e) {
                                e.printStackTrace();
                            } finally {
                                lock.unlock();
                            }
                        } else {
                            System.out.println("11已经上锁");
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
    
            new Thread(new Runnable() {
                @Override
                public void run() {
                    ReentrantLock lock = locks.get(adminId);
                    if (lock == null) {
                        lock = new ReentrantLock();
                        locks.put(adminId, lock);
                    }
                    try {
                        if (lock.tryLock(5, TimeUnit.SECONDS)) {
                            try {
                                System.out.println("22核心逻辑处理开始");
                                Thread.sleep(15000);
                                System.out.println("22核心逻辑处理结束");
                            } catch (Throwable e) {
                                e.printStackTrace();
                            } finally {
                                lock.unlock();
                            }
                        } else {
                            System.out.println("22已经上锁");
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
    `
  • 并发问题结果数据

    复制代码
    `11核心逻辑处理开始
    22核心逻辑处理开始
    11核心逻辑处理结束
    22核心逻辑处理结束
    `
  • 问题

    • 问题:锁对象创建并发问题

处理方案二:map+不超时重入锁数据结构(注:该方案并发时可以获取到锁进行操作)

  • 核心逻辑模拟--不加超时时间

    复制代码
    `import java.util.Collections;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class Test {
        public static void main(String[] args) {
    
            final Long adminId = 123L;
            final Map<Long, ReentrantLock> locks = Collections.synchronizedMap(new HashMap<>());
    
            new Thread(new Runnable() {
                @Override
                public void run() {
                    ReentrantLock lock = locks.get(adminId);
                    if (lock == null) {
                        lock = new ReentrantLock();
                        locks.put(adminId, lock);
                    }
                    try {
    //                    if (lock.tryLock(5, TimeUnit.SECONDS)) {
                        if (lock.tryLock()) {
                            try {
                                System.out.println("11核心逻辑处理开始");
                                Thread.sleep(2000);
                                System.out.println("11核心逻辑处理结束");
                            } catch (Throwable e) {
                                e.printStackTrace();
                            } finally {
                                lock.unlock();
                            }
                        } else {
                            System.out.println("11已经上锁");
                        }
                    } catch (Throwable e){
                        e.printStackTrace();
                    }
                }
            }).start();
    
            new Thread(new Runnable() {
                @Override
                public void run() {
                    ReentrantLock lock = locks.get(adminId);
                    if (lock == null) {
                        lock = new ReentrantLock();
                        locks.put(adminId, lock);
                    }
                    try {
    //                    if (lock.tryLock(5, TimeUnit.SECONDS)) {
                        if (lock.tryLock()) {
                            try {
                                System.out.println("22核心逻辑处理开始");
                                Thread.sleep(1000);
                                System.out.println("22核心逻辑处理结束");
                            } catch (Throwable e) {
                                e.printStackTrace();
                            } finally {
                                lock.unlock();
                            }
                        } else {
                            System.out.println("22已经上锁");
                        }
                    } catch (Throwable e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
    `
  • 并发结果问题数据

    复制代码
    `11核心逻辑处理开始
    22核心逻辑处理开始
    22核心逻辑处理结束
    11核心逻辑处理结束
    `
  • 问题

    • 问题:锁对象创建并发问题,这个只是简化后的超时为0的重入锁对象,和方案一其实道理一样

处理方案三:map+单独重入锁对象数据结构--可行,但是不满足互斥账号场景;缺点:锁对象只有一个,不同的账号场景互斥场景不太适合****

  • 核心逻辑

    复制代码
    `import java.util.Collections;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class Test {
        public static void main(String[] args) {
    
            final Long adminId = 123L;
            final Map<Long, ReentrantLock> locks = Collections.synchronizedMap(new HashMap<>());
            final ReentrantLock lockTemp = new ReentrantLock();
    
            new Thread(new Runnable() {
                @Override
                public void run() {
                    ReentrantLock lock = locks.get(adminId);
                    if (lock == null) {
                        lock = lockTemp;
                        locks.put(adminId, lockTemp);
                    }
                    try {
    //                    if (lock.tryLock(5, TimeUnit.SECONDS)) {
                        if (lock.tryLock()) {
                            try {
                                System.out.println("11核心逻辑处理开始");
                                Thread.sleep(2000);
                                System.out.println("11核心逻辑处理结束");
                            } catch (Throwable e) {
                                e.printStackTrace();
                            } finally {
                                lock.unlock();
                            }
                        } else {
                            System.out.println("11已经上锁");
                        }
                    } catch (Throwable e) {
                        e.printStackTrace();
                    }
                }
            }).start();
    
            new Thread(new Runnable() {
                @Override
                public void run() {
                    ReentrantLock lock = locks.get(adminId);
                    if (lock == null) {
                        lock = lockTemp;
                        locks.put(adminId, lockTemp);
                    }
                    try {
    //                    if (lock.tryLock(5, TimeUnit.SECONDS)) {
                        if (lock.tryLock()) {
                            try {
                                System.out.println("22核心逻辑处理开始");
                                Thread.sleep(1000);
                                System.out.println("22核心逻辑处理结束");
                            } catch (Throwable e) {
                                e.printStackTrace();
                            } finally {
                                lock.unlock();
                            }
                        } else {
                            System.out.println("22已经上锁");
                        }
                    } catch (Throwable e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
    `
  • 结果数据

    复制代码
    `22核心逻辑处理开始
    11已经上锁
    22核心逻辑处理结束
    `

处理方案四:map+重入锁对象数据结构+同步取塞对象--可行,满足互斥账号场景****

  • 核心逻辑

    复制代码
    `import java.util.Collections;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class Test {
        final static Map<Long, ReentrantLock> locks = Collections.synchronizedMap(new HashMap<>());
    
        public static void main(String[] args) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    methodZx(123L);
                }
            }).start();
    
            new Thread(new Runnable() {
                @Override
                public void run() {
                    methodZx(123L);
    
                }
            }).start();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    methodZx(123L);
    
                }
            }).start();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    methodZx(123L);
    
                }
            }).start();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    methodZx(123L);
    
                }
            }).start();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    methodZx(123L);
    
                }
            }).start();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    methodZx(456L);
    
                }
            }).start();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    methodZx(456L);
    
                }
            }).start();
        }
    
        public static void methodZx(Long adminId) {
            ReentrantLock lock = null;
            synchronized (adminId) {
                lock = locks.get(adminId);
                if (lock == null) {
                    lock = new ReentrantLock();
                    locks.put(adminId, lock);
                }
            }
            try {
                if (lock.tryLock()) {
                    try {
                        System.out.println(adminId + "处理start");
                        Thread.sleep(5000);
                        System.out.println(adminId + "处理end");
                    } catch (Throwable e) {
                        e.printStackTrace();
                    } finally {
                        lock.unlock();
                    }
                } else {
                    System.out.println(adminId + "--已经上锁");
                }
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }
    `
  • 结果数据

    复制代码
    `123处理start
    123--已经上锁
    123--已经上锁
    456处理start
    456--已经上锁
    123--已经上锁
    123--已经上锁
    123--已经上锁
    123处理end
    456处理end`
相关推荐
白子寰3 分钟前
【C++打怪之路Lv14】- “多态“篇
开发语言·c++
王俊山IT15 分钟前
C++学习笔记----10、模块、头文件及各种主题(一)---- 模块(5)
开发语言·c++·笔记·学习
为将者,自当识天晓地。17 分钟前
c++多线程
java·开发语言
小政爱学习!19 分钟前
封装axios、环境变量、api解耦、解决跨域、全局组件注入
开发语言·前端·javascript
daqinzl25 分钟前
java获取机器ip、mac
java·mac·ip
k093334 分钟前
sourceTree回滚版本到某次提交
开发语言·前端·javascript
激流丶41 分钟前
【Kafka 实战】如何解决Kafka Topic数量过多带来的性能问题?
java·大数据·kafka·topic
神奇夜光杯42 分钟前
Python酷库之旅-第三方库Pandas(202)
开发语言·人工智能·python·excel·pandas·标准库及第三方库·学习与成长
Themberfue44 分钟前
Java多线程详解⑤(全程干货!!!)线程安全问题 || 锁 || synchronized
java·开发语言·线程·多线程·synchronized·
plmm烟酒僧1 小时前
Windows下QT调用MinGW编译的OpenCV
开发语言·windows·qt·opencv