{"id":19889335,"url":"https://github.com/codeprometheus/starry-lock","last_synced_at":"2026-06-09T12:31:42.395Z","repository":{"id":104067363,"uuid":"356446663","full_name":"CodePrometheus/Starry-Lock","owner":"CodePrometheus","description":"分布式锁，乐观锁，悲观锁，可重入锁，非可重入锁，互斥锁，读写锁，分段锁，类锁，行级锁，自旋锁，偏向锁，公平锁，非公平锁共享锁，排他锁","archived":false,"fork":false,"pushed_at":"2021-06-19T02:30:05.000Z","size":53,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-01T04:43:49.527Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CodePrometheus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-04-10T02:21:31.000Z","updated_at":"2021-06-19T02:30:08.000Z","dependencies_parsed_at":null,"dependency_job_id":"f1005d80-2ae2-4dca-ab58-ff7aa5071e52","html_url":"https://github.com/CodePrometheus/Starry-Lock","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/CodePrometheus/Starry-Lock","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CodePrometheus%2FStarry-Lock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CodePrometheus%2FStarry-Lock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CodePrometheus%2FStarry-Lock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CodePrometheus%2FStarry-Lock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CodePrometheus","download_url":"https://codeload.github.com/CodePrometheus/Starry-Lock/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CodePrometheus%2FStarry-Lock/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34107865,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-09T02:00:06.510Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-12T18:09:50.696Z","updated_at":"2026-06-09T12:31:42.378Z","avatar_url":"https://github.com/CodePrometheus.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Starry-Lock\n分布式锁，乐观锁，悲观锁，可重入锁，非可重入锁，互斥锁，读写锁，分段锁，类锁，行级锁，自旋锁，偏向锁，公平锁，非公平锁共享锁，排他锁\n\n\n## Zookeeper分布式锁\n\n- [Zookeeper分布式锁](https://codeprometheus.github.io/Starry-Notes/#/Concurrency/Zookeeper%E5%88%86%E5%B8%83%E5%BC%8F%E9%94%81)\n\n## Redis分布式锁\n\n![锁超时处理逻辑](https://wx1.sinaimg.cn/large/7dfa0a7bly1g24obim6cnj20u80jzgnf.jpg \"锁超时处理逻辑.jpg\")\n\n\n## 悲观锁和乐观锁\n\n- [悲观锁和乐观锁](https://codeprometheus.github.io/Starry-Notes/#/Concurrency/%E6%82%B2%E8%A7%82%E9%94%81%E5%92%8C%E4%B9%90%E8%A7%82%E9%94%81)\n\n\n## AQS\n\n如果被请求的共享资源空闲，那么就将当前请求资源的线程设置为有效的工作线程，将共享资源设置为锁定状态；如果共享资源被占用，就需要一定的阻塞等待唤醒机制来保证锁分配。这个机制主要用的是CLH队列的变体实现的，将暂时获取不到锁的线程加入到队列中。\n\n-[AQS](https://codeprometheus.github.io/Starry-Notes/#/Concurrency/AQS)\n\n## ReentrantLock\n\n可重入锁，也叫做递归锁，是指在同一个线程在调外层方法获取锁的时候，再进入内层方法会自动获取锁。ReentrantLock 和synchronized 都是 可重入锁。可重入锁的一个好处是可一定程度避免死锁。\n\n-[ReentrantLock](https://codeprometheus.github.io/Starry-Notes/#/Concurrency/ReentrantLock)\n\n\n## 公平锁\n\n公平锁是指多个线程按照申请锁的顺序来获取锁\n\n-[公平锁](https://codeprometheus.github.io/Starry-Notes/#/Concurrency/AQS)\n\n\n## 非公平锁\n\n非公平锁是指多个线程获取锁的顺序并不是按照申请锁的顺序，有可能后申请的线程比先申请的线程优先获取锁。\n\n可能造成优先级反转或者饥饿现象。对于Java ReentrantLock而言，通过构造函数 ReentrantLock(boolean fair) 指定该锁是否是公平锁，默认是非公平锁。\n\n非公平锁的优点在于吞吐量比公平锁大。对于Synchronized而言，也是一种非公平锁。\n\n-[非公平锁](https://codeprometheus.github.io/Starry-Notes/#/Concurrency/AQS)\n\n\n## 分段锁\n\n分段锁其实是一种锁的设计，目的是细化锁的粒度，并不是具体的一种锁，对于ConcurrentHashMap而言，其并发的实现就是通过分段锁的形式来实现高效的并发操作。\n\nConcurrentHashMap中的分段锁称为Segment，它即类似于HashMap（JDK7 中HashMap的实现）的结构，即内部拥有一个Entry数组，数组中的每个元素又是一个链表；同时又是一个ReentrantLock（Segment继承了ReentrantLock)。\n\n当需要put元素的时候，并不是对整个HashMap加锁，而是先通过hashcode知道要放在哪一个分段中，然后对这个分段加锁，所以当多线程put时，只要不是放在同一个分段中，可支持并行插入。\n\n-[分段锁](https://codeprometheus.github.io/Starry-Notes/#/KeyPoints/ConcurrentHashMap1.6\u00261.7)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeprometheus%2Fstarry-lock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodeprometheus%2Fstarry-lock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeprometheus%2Fstarry-lock/lists"}