处理方案一: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`