{"id":648,"date":"2023-12-14T09:01:02","date_gmt":"2023-12-14T01:01:02","guid":{"rendered":""},"modified":"2023-12-14T09:01:02","modified_gmt":"2023-12-14T01:01:02","slug":"redis\u7f13\u5b58\u4e3a\u4ec0\u4e48\u8981\u5ef6\u65f6\u53cc\u5220","status":"publish","type":"post","link":"https:\/\/mushiming.com\/648.html","title":{"rendered":"redis\u7f13\u5b58\u4e3a\u4ec0\u4e48\u8981\u5ef6\u65f6\u53cc\u5220"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

\u4e00\u3001\u53ea\u5148\u5220\u7f13\u5b58<\/h3>\n

\u95ee\u9898\uff1a\u5148\u5220\u7f13\u5b58\uff0c\u5728\u6539\u5e93\u524d\uff0c\u5176\u4ed6\u4e8b\u52a1\u53c8\u628a\u65e7\u6570\u636e\u653e\u5230\u7f13\u5b58\u91cc\u53bb\u4e86\u3002<\/p>\n

\"redis\u7f13\u5b58\u4e3a\u4ec0\u4e48\u8981\u5ef6\u65f6\u53cc\u5220<\/p>\n

\u4e8c\u3001\u53ea\u540e\u5220\u7f13\u5b58<\/h3>\n

\u95ee\u98981\uff1a\u6539\u4e86\u5e93\uff0c\u6e05\u7406\u7f13\u5b58\u524d\uff0c\u6709\u90e8\u5206\u4e8b\u52a1\u8fd8\u662f\u4f1a\u62ff\u5230\u65e7\u7f13\u5b58<\/p>\n

\"redis\u7f13\u5b58\u4e3a\u4ec0\u4e48\u8981\u5ef6\u65f6\u53cc\u5220<\/p>\n

\u95ee\u98982\uff1a\u4e3a\u4e86\u907f\u514d\u7f13\u5b58\u6e05\u9664\u5931\u8d25\u5e26\u6765\u7684\u98ce\u9669\uff0c\u8d77\u5230\u5728\u6bcf\u6b21\u64cd\u4f5c\u6570\u636e\u5e93\u4e4b\u524d\uff0c\u90fd\u8fd8\u539f\u5230\u6ca1\u6709\u4ea7\u751f\u7f13\u5b58\u4e4b\u524d\u7684\u6548\u679c\u3002\uff08\u5f53\u7136\u5982\u679c\u80fd\u591f\u505a\u597d\u56de\u6eda\u4e0e\u914d\u7f6e\u597d\u6570\u636e\u5e93\u9694\u79bb\u7ea7\u522b\uff0c\u8fd9\u6761\u53ef\u4ee5\u5ffd\u7565\uff09
\"redis\u7f13\u5b58\u4e3a\u4ec0\u4e48\u8981\u5ef6\u65f6\u53cc\u5220<\/p>\n

\u4e09\u3001\u666e\u901a\u53cc\u5220<\/h3>\n

\u95ee\u9898\uff1a\u7b2c\u4e00\u6b21\u6e05\u7a7a\u7f13\u5b58\u540e\u3001\u66f4\u65b0\u6570\u636e\u5e93\u524d\uff1a\u5176\u4ed6\u4e8b\u52a1\u67e5\u8be2\u4e86\u6570\u636e\u5e93hang\u4f4f
\u7b2c\u4e8c\u6b21\u6e05\u7a7a\u7f13\u5b58\u540e\uff1a\u5176\u4ed6\u4e8b\u52a1\u66f4\u65b0\u7f13\u5b58\uff0c\u6b64\u65f6\u53c8\u4f1a\u628a\u65e7\u6570\u636e\u66f4\u65b0\u5230\u7f13\u5b58<\/p>\n

\"redis\u7f13\u5b58\u4e3a\u4ec0\u4e48\u8981\u5ef6\u65f6\u53cc\u5220<\/p>\n

\u56db\u3001\u4e3a\u4ec0\u4e48\u9700\u8981\u5ef6\u65f6\u53cc\u5220\uff1f<\/h3>\n

\u5728\u4e09\u4e2d\uff0c\u7b2c\u4e8c\u6b21\u6e05\u7a7a\u7f13\u5b58\u4e4b\u524d\uff0c\u591a\u5ef6\u65f6\u4e00\u4f1a\u513f\uff0c\u7b49B\u66f4\u65b0\u7f13\u5b58\u7ed3\u675f\u4e86\uff0c\u518d\u5220\u9664\u7f13\u5b58\uff0c\u8fd9\u6837\u5c31\u7f13\u5b58\u5c31\u4e0d\u5b58\u5728\u4e86\uff0c\u5176\u4ed6\u4e8b\u52a1\u67e5\u8be2\u5230\u7684\u4e3a\u65b0\u7f13\u5b58\u3002<\/p>\n

\u5ef6\u65f6\u662f\u786e\u4fdd \u4fee\u6539\u6570\u636e\u5e93 -> \u6e05\u7a7a\u7f13\u5b58\u524d\uff0c\u5176\u4ed6\u4e8b\u52a1\u7684\u66f4\u6539\u7f13\u5b58\u64cd\u4f5c\u5df2\u7ecf\u6267\u884c\u5b8c\u3002<\/strong>
\"redis\u7f13\u5b58\u4e3a\u4ec0\u4e48\u8981\u5ef6\u65f6\u53cc\u5220<\/p>\n

\u4e94\u3001\u4ee5\u4e0a\u7b56\u7565\u8fd8\u80fd\u4e0d\u80fd\u5b8c\u5584<\/h3>\n

\u56db\u4e2d\u8bf4\u5230\uff0c\u91c7\u7528\u5ef6\u65f6\u5220\u6700\u540e\u4e00\u6b21\u7f13\u5b58\uff0c\u4f46\u8fd9\u5176\u4e2d\u96be\u514d\u8fd8\u662f\u4f1a\u5927\u91cf\u7684\u67e5\u8be2\u5230\u65e7\u7f13\u5b58\u6570\u636e\u7684\u3002
\"redis\u7f13\u5b58\u4e3a\u4ec0\u4e48\u8981\u5ef6\u65f6\u53cc\u5220
\u8fd9\u65f6\u5019\u53ef\u4ee5\u901a\u8fc7\u52a0\u9501\u6765\u89e3\u51b3\uff0c\u4e00\u6b21\u6027\u4e0d\u8ba9\u592a\u591a\u7684\u7ebf\u7a0b\u90fd\u6765\u8bf7\u6c42\uff0c\u53e6\u5916\u4ece\u56fe\u4e0a\u770b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c3d\u91cf\u7f29\u77ed\u7b2c\u4e00\u6b21\u5220\u9664\u7f13\u5b58<\/code>\u548c\u66f4\u65b0\u6570\u636e\u5e93<\/code>\u7684\u65f6\u95f4\u5dee\uff0c\u8fd9\u6837\u53ef\u4ee5\u4f7f\u5f97\u5176\u4ed6\u4e8b\u52a1\u7b2c\u4e00\u65f6\u95f4\u83b7\u53d6\u5230\u66f4\u65b0\u6570\u636e\u5e93\u540e\u7684\u6570\u636e\u3002\u53e6\u5916\uff0c\u8be5\u65b9\u5f0f\uff08\u7b2c5\u79cd\uff0c\u76f8\u5bf9\u7b2c2\u79cd\uff0c\u53ea\u540e\u5220\u7f13\u5b58\u7684\uff0c\u53ef\u4ee5\u5927\u5927\u7684\u51cf\u5c11\u83b7\u53d6\u5230\u65e7\u7f13\u5b58\u7684\u6570\u91cf\uff09<\/p>\n

\u53ef\u4ee5\u53c2\u8003\uff1a\u7f13\u5b58\u5904\u7406\u6d41\u7a0b<\/p>\n

\u53c2\u8003redis\u7f13\u5b58\u4e3a\u4ec0\u4e48\u8981\u5ef6\u65f6\u53cc\u5220<\/p>\n

\u66f4\u7ec6\u7c92\u5ea6\u7684\uff08Key\u7ea7\u522b\uff09\u4e92\u65a5\u9501<\/p>\n

\u5206\u6bb5\u9501\u4f7f\u7528\u6ce8\u610f\u4e8b\u9879<\/p>\n

\u5206\u6bb5\u9501<\/h3>\n
\/** * * @Explain: key\u9501\uff08\u8981\u4fdd\u8bc1key\u7684hashCode\u4e0d\u53d8,\u5426\u5219\u65e0\u6cd5\u91ca\u653e\u9501\u3002\u5373\u52a0\u9501\u4e4b\u540e\u4e0d\u8981\u624b\u52a8\u66f4\u6539lockMap\uff09 *\/<\/span>\n@Component<\/span>\npublic<\/span> class<\/span> LoadKeyLock<\/span><<\/span>T<\/span>><\/span><\/span> { \n   <\/span>\n    \/\/\u9ed8\u8ba4\u5206\u6bb5\u6570\u91cf<\/span>\n    private<\/span> Integer<\/span> segments =<\/span> 16<\/span>;<\/span>\n    private<\/span> final<\/span> HashMap<\/span><<\/span>Integer<\/span>,<\/span> ReentrantLock<\/span>><\/span><\/span> lockMap =<\/span> new<\/span> HashMap<\/span><<\/span>><\/span><\/span>(<\/span>)<\/span>;<\/span>\n    public<\/span> LoadKeyLock<\/span>(<\/span>)<\/span> { \n   <\/span>\n        init<\/span>(<\/span>null<\/span>,<\/span> false<\/span>)<\/span>;<\/span>\n    }<\/span>\n    public<\/span> LoadKeyLock<\/span>(<\/span>Integer<\/span> counts,<\/span> boolean<\/span> fair)<\/span> { \n   <\/span>\n        init<\/span>(<\/span>counts,<\/span> fair)<\/span>;<\/span>\n    }<\/span>\n    private<\/span> void<\/span> init<\/span>(<\/span>Integer<\/span> counts,<\/span> boolean<\/span> fair)<\/span> { \n   <\/span>\n        if<\/span> (<\/span>counts !=<\/span> null<\/span>)<\/span> { \n   <\/span>\n            segments =<\/span> counts;<\/span>\n        }<\/span>\n        for<\/span> (<\/span>int<\/span> i =<\/span> 0<\/span>;<\/span> i <<\/span> segments;<\/span> i++<\/span>)<\/span> { \n   <\/span>\n            lockMap.<\/span>put<\/span>(<\/span>i,<\/span> new<\/span> ReentrantLock<\/span>(<\/span>fair)<\/span>)<\/span>;<\/span>\n        }<\/span>\n    }<\/span>\n    public<\/span> void<\/span> lock<\/span>(<\/span>T<\/span> key)<\/span> { \n   <\/span>\n        ReentrantLock<\/span> lock =<\/span> lockMap.<\/span>get<\/span>(<\/span>key.<\/span>hashCode<\/span>(<\/span>)<\/span> %<\/span> segments)<\/span>;<\/span>\n        lock.<\/span>lock<\/span>(<\/span>)<\/span>;<\/span>\n    }<\/span>\n    public<\/span> void<\/span> unlock<\/span>(<\/span>T<\/span> key)<\/span> { \n   <\/span>\n        ReentrantLock<\/span> lock =<\/span> lockMap.<\/span>get<\/span>(<\/span>key.<\/span>hashCode<\/span>(<\/span>)<\/span> %<\/span> segments)<\/span>;<\/span>\n        lock.<\/span>unlock<\/span>(<\/span>)<\/span>;<\/span>\n    }<\/span>\n}<\/span>\n<\/code><\/pre>\n

\u54c8\u5e0c\u9501<\/h3>\n

\u5206\u6bb5\u9501\u7684\u57fa\u7840\u4e0a\u53d1\u5c55\u8d77\u6765\u7684\u7b2c\u4e8c\u79cd\u9501\u7b56\u7565\uff0c\u76ee\u7684\u662f\u5b9e\u73b0\u771f\u6b63\u610f\u4e49\u4e0a\u7684\u7ec6\u7c92\u5ea6\u9501\u3002\u6bcf\u4e2a\u54c8\u5e0c\u503c\u4e0d\u540c\u7684\u5bf9\u8c61\u90fd\u80fd\u83b7\u5f97\u81ea\u5df1\u72ec\u7acb\u7684\u9501\u3002<\/p>\n

public<\/span> class<\/span> HashLock<\/span><<\/span>T<\/span>><\/span><\/span> { \n   <\/span>\n    private<\/span> boolean<\/span> isFair =<\/span> false<\/span>;<\/span>\n    private<\/span> final<\/span> SegmentLock<\/span><<\/span>T<\/span>><\/span><\/span> segmentLock =<\/span> new<\/span> SegmentLock<\/span><<\/span>><\/span><\/span>(<\/span>)<\/span>;<\/span>\/\/\u5206\u6bb5\u9501<\/span>\n    private<\/span> final<\/span> ConcurrentHashMap<\/span><<\/span>T<\/span>,<\/span> LockInfo<\/span>><\/span><\/span> lockMap =<\/span> new<\/span> ConcurrentHashMap<\/span><<\/span>><\/span><\/span>(<\/span>)<\/span>;<\/span>\n\n    public<\/span> HashLock<\/span>(<\/span>)<\/span> { \n   <\/span>\n    }<\/span>\n\n    public<\/span> HashLock<\/span>(<\/span>boolean<\/span> fair)<\/span> { \n   <\/span>\n        isFair =<\/span> fair;<\/span>\n    }<\/span>\n\n    public<\/span> void<\/span> lock<\/span>(<\/span>T<\/span> key)<\/span> { \n   <\/span>\n        LockInfo<\/span> lockInfo;<\/span>\n        segmentLock.<\/span>lock<\/span>(<\/span>key)<\/span>;<\/span>\n        try<\/span> { \n   <\/span>\n            lockInfo =<\/span> lockMap.<\/span>get<\/span>(<\/span>key)<\/span>;<\/span>\n            if<\/span> (<\/span>lockInfo ==<\/span> null<\/span>)<\/span> { \n   <\/span>\n                lockInfo =<\/span> new<\/span> LockInfo<\/span>(<\/span>isFair)<\/span>;<\/span>\n                lockMap.<\/span>put<\/span>(<\/span>key,<\/span> lockInfo)<\/span>;<\/span>\n            }<\/span> else<\/span> { \n   <\/span>\n                lockInfo.<\/span>count.<\/span>incrementAndGet<\/span>(<\/span>)<\/span>;<\/span>\n            }<\/span>\n        }<\/span> finally<\/span> { \n   <\/span>\n            segmentLock.<\/span>unlock<\/span>(<\/span>key)<\/span>;<\/span>\n        }<\/span>\n        lockInfo.<\/span>lock.<\/span>lock<\/span>(<\/span>)<\/span>;<\/span>\n    }<\/span>\n\n    public<\/span> void<\/span> unlock<\/span>(<\/span>T<\/span> key)<\/span> { \n   <\/span>\n        LockInfo<\/span> lockInfo =<\/span> lockMap.<\/span>get<\/span>(<\/span>key)<\/span>;<\/span>\n        if<\/span> (<\/span>lockInfo.<\/span>count.<\/span>get<\/span>(<\/span>)<\/span> ==<\/span> 1<\/span>)<\/span> { \n   <\/span>\n            segmentLock.<\/span>lock<\/span>(<\/span>key)<\/span>;<\/span>\n            try<\/span> { \n   <\/span>\n                if<\/span> (<\/span>lockInfo.<\/span>count.<\/span>get<\/span>(<\/span>)<\/span> ==<\/span> 1<\/span>)<\/span> { \n   <\/span>\n                    lockMap.<\/span>remove<\/span>(<\/span>key)<\/span>;<\/span>\n                }<\/span>\n            }<\/span> finally<\/span> { \n   <\/span>\n                segmentLock.<\/span>unlock<\/span>(<\/span>key)<\/span>;<\/span>\n            }<\/span>\n        }<\/span>\n        lockInfo.<\/span>count.<\/span>decrementAndGet<\/span>(<\/span>)<\/span>;<\/span>\n        lockInfo.<\/span>unlock<\/span>(<\/span>)<\/span>;<\/span>\n    }<\/span>\n\n    private<\/span> static<\/span> class<\/span> LockInfo<\/span> { \n   <\/span>\n        public<\/span> ReentrantLock<\/span> lock;<\/span>\n        public<\/span> AtomicInteger<\/span> count =<\/span> new<\/span> AtomicInteger<\/span>(<\/span>1<\/span>)<\/span>;<\/span>\n\n        private<\/span> LockInfo<\/span>(<\/span>boolean<\/span> fair)<\/span> { \n   <\/span>\n            this<\/span>.<\/span>lock =<\/span> new<\/span> ReentrantLock<\/span>(<\/span>fair)<\/span>;<\/span>\n        }<\/span>\n\n        public<\/span> void<\/span> lock<\/span>(<\/span>)<\/span> { \n   <\/span>\n            this<\/span>.<\/span>lock.<\/span>lock<\/span>(<\/span>)<\/span>;<\/span>\n        }<\/span>\n\n        public<\/span> void<\/span> unlock<\/span>(<\/span>)<\/span> { \n   <\/span>\n            this<\/span>.<\/span>lock.<\/span>unlock<\/span>(<\/span>)<\/span>;<\/span>\n        }<\/span>\n    }<\/span>\n}<\/span>\n<\/code><\/pre>\n

\u5f31\u5f15\u7528\u9501<\/h3>\n

\u54c8\u5e0c\u9501\u56e0\u4e3a\u5f15\u5165\u7684\u5206\u6bb5\u9501\u6765\u4fdd\u8bc1\u9501\u521b\u5efa\u548c\u9500\u6bc1\u7684\u540c\u6b65\uff0c\u8fd9\u4e2a\u9501\u7684\u601d\u60f3\u662f\u501f\u52a9java\u7684\u5f31\u5f15\u7528\u6765\u521b\u5efa\u9501\uff0c\u628a\u9501\u7684\u9500\u6bc1\u4ea4\u7ed9jvm\u7684\u5783\u573e\u56de\u6536\uff0c\u6765\u907f\u514d\u989d\u5916\u7684\u6d88\u8017\u3002<\/p>\n

\/** * \u5f31\u5f15\u7528\u9501\uff0c\u4e3a\u6bcf\u4e2a\u72ec\u7acb\u7684\u54c8\u5e0c\u503c\u63d0\u4f9b\u72ec\u7acb\u7684\u9501\u529f\u80fd *\/<\/span>\npublic<\/span> class<\/span> WeakHashLock<\/span><<\/span>T<\/span>><\/span><\/span> { \n   <\/span>\n    private<\/span> ConcurrentHashMap<\/span><<\/span>T<\/span>,<\/span> WeakLockRef<\/span><<\/span>T<\/span>,<\/span> ReentrantLock<\/span>><\/span>><\/span><\/span> lockMap =<\/span> new<\/span> ConcurrentHashMap<\/span><<\/span>><\/span><\/span>(<\/span>)<\/span>;<\/span>\n    private<\/span> ReferenceQueue<\/span><<\/span>ReentrantLock<\/span>><\/span><\/span> queue =<\/span> new<\/span> ReferenceQueue<\/span><<\/span>><\/span><\/span>(<\/span>)<\/span>;<\/span>\n\n    public<\/span> ReentrantLock<\/span> get<\/span>(<\/span>T<\/span> key)<\/span> { \n   <\/span>\n        if<\/span> (<\/span>lockMap.<\/span>size<\/span>(<\/span>)<\/span> ><\/span> 1000<\/span>)<\/span> { \n   <\/span>\n            clearEmptyRef<\/span>(<\/span>)<\/span>;<\/span>\n        }<\/span>\n        WeakReference<\/span><<\/span>ReentrantLock<\/span>><\/span><\/span> lockRef =<\/span> lockMap.<\/span>get<\/span>(<\/span>key)<\/span>;<\/span>\n        ReentrantLock<\/span> lock =<\/span> (<\/span>lockRef ==<\/span> null<\/span> ?<\/span> null<\/span> :<\/span> lockRef.<\/span>get<\/span>(<\/span>)<\/span>)<\/span>;<\/span>\n        while<\/span> (<\/span>lock ==<\/span> null<\/span>)<\/span> { \n   <\/span>\n            lockMap.<\/span>putIfAbsent<\/span>(<\/span>key,<\/span> new<\/span> WeakLockRef<\/span><<\/span>><\/span><\/span>(<\/span>new<\/span> ReentrantLock<\/span>(<\/span>)<\/span>,<\/span> queue,<\/span> key)<\/span>)<\/span>;<\/span>\n            lockRef =<\/span> lockMap.<\/span>get<\/span>(<\/span>key)<\/span>;<\/span>\n            lock =<\/span> (<\/span>lockRef ==<\/span> null<\/span> ?<\/span> null<\/span> :<\/span> lockRef.<\/span>get<\/span>(<\/span>)<\/span>)<\/span>;<\/span>\n            if<\/span> (<\/span>lock !=<\/span> null<\/span>)<\/span> { \n   <\/span>\n                return<\/span> lock;<\/span>\n            }<\/span>\n            clearEmptyRef<\/span>(<\/span>)<\/span>;<\/span>\n        }<\/span>\n        return<\/span> lock;<\/span>\n    }<\/span>\n\n    @SuppressWarnings<\/span>(<\/span>\"unchecked\"<\/span>)<\/span>\n    private<\/span> void<\/span> clearEmptyRef<\/span>(<\/span>)<\/span> { \n   <\/span>\n        Reference<\/span><<\/span>?<\/span> extends<\/span> ReentrantLock<\/span>><\/span><\/span> ref;<\/span>\n        while<\/span> (<\/span>(<\/span>ref =<\/span> queue.<\/span>poll<\/span>(<\/span>)<\/span>)<\/span> !=<\/span> null<\/span>)<\/span> { \n   <\/span>\n            WeakLockRef<\/span><<\/span>T<\/span>,<\/span> ?<\/span> extends<\/span> ReentrantLock<\/span>><\/span><\/span> weakLockRef =<\/span> (<\/span>WeakLockRef<\/span><<\/span>T<\/span>,<\/span> ?<\/span> extends<\/span> ReentrantLock<\/span>><\/span><\/span>)<\/span> ref;<\/span>\n            lockMap.<\/span>remove<\/span>(<\/span>weakLockRef.<\/span>key)<\/span>;<\/span>\n        }<\/span>\n    }<\/span>\n\n    private<\/span> static<\/span> final<\/span> class<\/span> WeakLockRef<\/span><<\/span>T<\/span>,<\/span> K<\/span>><\/span><\/span> extends<\/span> WeakReference<\/span><<\/span>K<\/span>><\/span><\/span> { \n   <\/span>\n        final<\/span> T<\/span> key;<\/span>\n\n        private<\/span> WeakLockRef<\/span>(<\/span>K<\/span> referent,<\/span> ReferenceQueue<\/span><<\/span>?<\/span> super<\/span> K<\/span>><\/span><\/span> q,<\/span> T<\/span> key)<\/span> { \n   <\/span>\n            super<\/span>(<\/span>referent,<\/span> q)<\/span>;<\/span>\n            this<\/span>.<\/span>key =<\/span> key;<\/span>\n        }<\/span>\n    }<\/span>\n}<\/span>\n<\/code><\/pre>\n

\u9002\u5408\u8017\u65f6\u957f\u573a\u666f\u7684\u4e92\u65a5key\u9501<\/h3>\n

\u4e00\u4e2a\u7ec6\u7c92\u5ea6\u7684\u9501\uff0c\u5728\u67d0\u4e9b\u573a\u666f\u80fd\u6bd4synchronized\uff0cReentrantLock\u7b49\u83b7\u5f97\u66f4\u9ad8\u7684\u5e76\u884c\u5ea6\u66f4\u597d\u7684\u6027\u80fd<\/p>\n

public<\/span> class<\/span> KeyLock<\/span><<\/span>K<\/span>><\/span><\/span> { \n   <\/span>\n    \/\/ \u4fdd\u5b58\u6240\u6709\u9501\u5b9a\u7684KEY\u53ca\u5176\u4fe1\u53f7\u91cf<\/span>\n    private<\/span> final<\/span> ConcurrentMap<\/span><<\/span>K<\/span>,<\/span> Semaphore<\/span>><\/span><\/span> map =<\/span> new<\/span> ConcurrentHashMap<\/span><<\/span>K<\/span>,<\/span> Semaphore<\/span>><\/span><\/span>(<\/span>)<\/span>;<\/span>\n    \/\/ \u4fdd\u5b58\u6bcf\u4e2a\u7ebf\u7a0b\u9501\u5b9a\u7684KEY\u53ca\u5176\u9501\u5b9a\u8ba1\u6570<\/span>\n    private<\/span> final<\/span> ThreadLocal<\/span><<\/span>Map<\/span><<\/span>K<\/span>,<\/span> LockInfo<\/span>><\/span>><\/span><\/span> local =<\/span> new<\/span> ThreadLocal<\/span><<\/span>Map<\/span><<\/span>K<\/span>,<\/span> LockInfo<\/span>><\/span>><\/span><\/span>(<\/span>)<\/span> { \n   <\/span>\n        @Override<\/span>\n        protected<\/span> Map<\/span><<\/span>K<\/span>,<\/span> LockInfo<\/span>><\/span><\/span> initialValue<\/span>(<\/span>)<\/span> { \n   <\/span>\n            return<\/span> new<\/span> HashMap<\/span><<\/span>K<\/span>,<\/span> LockInfo<\/span>><\/span><\/span>(<\/span>)<\/span>;<\/span>\n        }<\/span>\n    }<\/span>;<\/span>\n\n    \/** * \u9501\u5b9akey\uff0c\u5176\u4ed6\u7b49\u5f85\u6b64key\u7684\u7ebf\u7a0b\u5c06\u8fdb\u5165\u7b49\u5f85\uff0c\u76f4\u5230\u8c03\u7528{@link #unlock(K)} * \u4f7f\u7528hashcode\u548cequals\u6765\u5224\u65adkey\u662f\u5426\u76f8\u540c\uff0c\u56e0\u6b64key\u5fc5\u987b\u5b9e\u73b0{@link #hashCode()}\u548c * {@link #equals(Object)}\u65b9\u6cd5 * * @param key *\/<\/span>\n    public<\/span> void<\/span> lock<\/span>(<\/span>K<\/span> key)<\/span> { \n   <\/span>\n        if<\/span> (<\/span>key ==<\/span> null<\/span>)<\/span>\n            return<\/span>;<\/span>\n        LockInfo<\/span> info =<\/span> local.<\/span>get<\/span>(<\/span>)<\/span>.<\/span>get<\/span>(<\/span>key)<\/span>;<\/span>\n        if<\/span> (<\/span>info ==<\/span> null<\/span>)<\/span> { \n   <\/span>\n            Semaphore<\/span> current =<\/span> new<\/span> Semaphore<\/span>(<\/span>1<\/span>)<\/span>;<\/span>\n            current.<\/span>acquireUninterruptibly<\/span>(<\/span>)<\/span>;<\/span>\n            Semaphore<\/span> previous =<\/span> map.<\/span>put<\/span>(<\/span>key,<\/span> current)<\/span>;<\/span>\n            if<\/span> (<\/span>previous !=<\/span> null<\/span>)<\/span>\n                previous.<\/span>acquireUninterruptibly<\/span>(<\/span>)<\/span>;<\/span>\n            local.<\/span>get<\/span>(<\/span>)<\/span>.<\/span>put<\/span>(<\/span>key,<\/span> new<\/span> LockInfo<\/span>(<\/span>current)<\/span>)<\/span>;<\/span>\n        }<\/span> else<\/span> { \n   <\/span>\n            info.<\/span>lockCount++<\/span>;<\/span>\n        }<\/span>\n    }<\/span>\n    \/** * \u91ca\u653ekey\uff0c\u5524\u9192\u5176\u4ed6\u7b49\u5f85\u6b64key\u7684\u7ebf\u7a0b * @param key *\/<\/span>\n    public<\/span> void<\/span> unlock<\/span>(<\/span>K<\/span> key)<\/span> { \n   <\/span>\n        if<\/span> (<\/span>key ==<\/span> null<\/span>)<\/span>\n            return<\/span>;<\/span>\n        LockInfo<\/span> info =<\/span> local.<\/span>get<\/span>(<\/span>)<\/span>.<\/span>get<\/span>(<\/span>key)<\/span>;<\/span>\n        if<\/span> (<\/span>info !=<\/span> null<\/span> &&<\/span> --<\/span>info.<\/span>lockCount ==<\/span> 0<\/span>)<\/span> { \n   <\/span>\n            info.<\/span>current.<\/span>release<\/span>(<\/span>)<\/span>;<\/span>\n            map.<\/span>remove<\/span>(<\/span>key,<\/span> info.<\/span>current)<\/span>;<\/span>\n            local.<\/span>get<\/span>(<\/span>)<\/span>.<\/span>remove<\/span>(<\/span>key)<\/span>;<\/span>\n        }<\/span>\n    }<\/span>\n \n    \/** * \u9501\u5b9a\u591a\u4e2akey * \u5efa\u8bae\u5728\u8c03\u7528\u6b64\u65b9\u6cd5\u524d\u5148\u5bf9keys\u8fdb\u884c\u6392\u5e8f\uff0c\u4f7f\u7528\u76f8\u540c\u7684\u9501\u5b9a\u987a\u5e8f\uff0c\u9632\u6b62\u6b7b\u9501\u53d1\u751f * @param keys *\/<\/span>\n    public<\/span> void<\/span> lock<\/span>(<\/span>K<\/span>[<\/span>]<\/span> keys)<\/span> { \n   <\/span>\n        if<\/span> (<\/span>keys ==<\/span> null<\/span>)<\/span>\n            return<\/span>;<\/span>\n        for<\/span> (<\/span>K<\/span> key :<\/span> keys)<\/span> { \n   <\/span>\n            lock<\/span>(<\/span>key)<\/span>;<\/span>\n        }<\/span>\n    }<\/span>\n\n    \/** * \u91ca\u653e\u591a\u4e2akey * @param keys *\/<\/span>\n    public<\/span> void<\/span> unlock<\/span>(<\/span>K<\/span>[<\/span>]<\/span> keys)<\/span> { \n   <\/span>\n        if<\/span> (<\/span>keys ==<\/span> null<\/span>)<\/span>\n            return<\/span>;<\/span>\n        for<\/span> (<\/span>K<\/span> key :<\/span> keys)<\/span> { \n   <\/span>\n            unlock<\/span>(<\/span>key)<\/span>;<\/span>\n        }<\/span>\n    }<\/span>\n\n    private<\/span> static<\/span> class<\/span> LockInfo<\/span> { \n   <\/span>\n        private<\/span> final<\/span> Semaphore<\/span> current;<\/span>\n        private<\/span> int<\/span> lockCount;<\/span>\n\n        private<\/span> LockInfo<\/span>(<\/span>Semaphore<\/span> current)<\/span> { \n   <\/span>\n            this<\/span>.<\/span>current =<\/span> current;<\/span>\n            this<\/span>.<\/span>lockCount =<\/span> 1<\/span>;<\/span>\n        }<\/span>\n    }<\/span>\n<\/code><\/pre>\n

\u8fd9\u91cc\u518d\u63a8\u8350\u4e00\u7bc7\uff1a\u7f13\u5b58\u5904\u7406\u6d41\u7a0b<\/p>\n","protected":false},"excerpt":{"rendered":"redis\u7f13\u5b58\u4e3a\u4ec0\u4e48\u8981\u5ef6\u65f6\u53cc\u5220redis\u7f13\u5b58\u4e3a\u4ec0\u4e48\u8981\u5ef6\u65f6\u53cc\u5220_\u5ef6\u8fdf\u53cc\u5220\u4e3a\u4ec0\u4e48\u8981\u5ef6\u8fdf","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/648"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=648"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/648\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}