synchronized锁级别的一个坑

在实现一次对限流接口访问时,我错误的使用了单例+synchronized修饰方法的形式实现,这样在限流方规则为不同接口不同限制,单独限制时,同一个实例中的所有被synchronized修饰的方法竞争同一个。因为synchronized是 对象!

因此,可以创建多个ReentrantLock锁,在方法中进行灵活锁定和解锁。

package com.cb.account.controller; import java.util.concurrent.locks.ReentrantLock; public class SingleD { private static SingleD singleD; private final ReentrantLock test1_lock = new ReentrantLock(); private final ReentrantLock test2_lock = new ReentrantLock(); public static SingleD getInstance() { if (singleD == null) { singleD = new SingleD(); } return singleD; } public synchronized void test1() { test1_lock.lock(); System.out.println("test1"); try { Thread.sleep(2000L); } catch (InterruptedException e) { e.printStackTrace(); }finally { test1_lock.unlock(); } } public void test2() { test2_lock.lock(); System.out.println("test2"); try { Thread.sleep(2000L); } catch (InterruptedException e) { e.printStackTrace(); }finally { test2_lock.unlock(); } } public void test3() { System.out.println("test3"); try { Thread.sleep(2000L); } catch (InterruptedException e) { e.printStackTrace(); } } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zygxxw.html