场景:用户点赞、取消点赞。
问题:避免点赞与取消点赞冲突,在不考虑其他方法前提下,采用JAVA synchronized 进行同步锁操作,如果直接锁方法,那A用户点赞、取消点赞时,会导致其他用户无法点赞、取消点赞。所以采用锁用户ID的形式
public static void main(String[] args) { // System.out.println(DigestUtils.md5DigestAsHex(("20210928#888" + "1010").getBytes())); Test test = new Test(); new Thread(() -> { test.asd(1, 1); }).start(); new Thread(() -> { test.asd(2, 2); }).start(); } // id锁 private static final Map<Integer, Object> locks = new HashMap<>(); // 用完后,如果直接删除id锁,那如果此时id锁已被使用,同步会失效,所以要用下面这个中间件,判断是否还有ID的锁在用 private static final Map<Integer, AtomicInteger> waitingLocks = new HashMap<>(); private void asd(Integer i, Integer id) { System.out.println(i + ":before"); Object lock; synchronized (locks){ lock = locks.computeIfAbsent(id, j -> new Object()); waitingLocks.computeIfAbsent(id, j -> new AtomicInteger()).incrementAndGet(); } synchronized (lock) { System.out.println(i + ":in"); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(i + ":end"); // 用完删除 synchronized (locks){ if(waitingLocks.get(id).decrementAndGet() == 0){ locks.remove(id); waitingLocks.remove(id); } } System.out.println(JSON.toJSON(locks)); System.out.println(JSON.toJSON(waitingLocks)); } System.out.println(i + ":after"); System.out.println(""); }
标签:Java,不同,System,用户,点赞,println,new,id,out From: https://www.cnblogs.com/KaiStudy/p/17136455.html