<\/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
<\/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
<\/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
<\/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
<\/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>
<\/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
\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>