处理方案一: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+单独重入锁对象数据结构--可行,但是不满足互斥账号场景;缺点:锁对象只有一个,不同的账号场景互斥场景不太适合
-
核心逻辑文章来源:https://www.toymoban.com/news/detail-695328.html
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+重入锁对象数据结构+同步取塞对象--可行,满足互斥账号场景
-
核心逻辑文章来源地址https://www.toymoban.com/news/detail-695328.html
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
到了这里,关于互斥场景重入锁处理方案的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!