{"action":"created","comment":{"url":"https://api.github.com/repos/iluwatar/java-design-patterns/pulls/comments/49061112","id":49061112,"diff_hunk":"@@ -0,0 +1,147 @@\n+package com.iluwatar.reader.writer.lock;\n+\n+import java.util.HashSet;\n+import java.util.Set;\n+\n+/**\n+ * Class responsible for control the access for reader or writer\n+ */\n+public class ReaderWriterLock {\n+\n+  /**\n+   * Mutex for reader\n+   */\n+  private Object r = new Object();\n+\n+  /**\n+   * Global mutex for reader or writer, use to save the holding object\n+   */\n+  private Set<Object> g = new HashSet<>();\n+\n+  /**\n+   * Current reader count\n+   */\n+  private int readerCount = 0;\n+\n+  private ReaderLock readLock = new ReaderLock();\n+  private WriterLock writeLock = new WriterLock();\n+\n+\n+  public Lock readLock() {\n+    return readLock;\n+  }\n+\n+\n+  public Lock writeLock() {\n+    return writeLock;\n+  }\n+\n+\n+  /**\n+   * Reader Lock, can be access for more than one reader concurrently if no writer get the lock\n+   */\n+  private class ReaderLock implements Lock {\n+\n+    @Override\n+    public void lock() {\n+\n+      synchronized (r) {\n+\n+        readerCount++;\n+        if (readerCount == 1) {\n+\n+          synchronized (g) {\n+\n+            while (true) {\n+              if (isLockFree() || isReaderOwnThisLock()) {\n+                g.add(this);\n+                break;\n+              } else {\n+                waitUninterruptely(g);\n+              }\n+            }\n+          }\n+\n+        }\n+      }\n+    }\n+\n+\n+    @Override\n+    public void unlock() {\n+\n+      synchronized (r) {\n+        readerCount--;\n+        if (readerCount == 0) {\n+          synchronized (g) {\n+            g.remove(this);\n+            g.notifyAll();\n+          }\n+        }\n+      }\n+\n+    }\n+\n+  }\n+\n+\n+  /**\n+   * Writer Lock, can only be accessed by one writer concurrently\n+   */\n+  private class WriterLock implements Lock {\n+\n+    @Override\n+    public void lock() {\n+\n+      synchronized (g) {\n+\n+        while (true) {\n+\n+          if (isLockFree()) {\n+            g.add(this);\n+            break;\n+          } else if (isWriterOwnThisLock()) {\n+            waitUninterruptely(g);\n+          } else if (isReaderOwnThisLock()) {\n+            waitUninterruptely(g);\n+          } else {\n+            throw new RuntimeException(\"it should never reach here\");\n+          }\n+        }\n+      }\n+    }\n+\n+\n+    @Override\n+    public void unlock() {\n+\n+      synchronized (g) {\n+        g.remove(this);\n+        g.notifyAll();\n+      }\n+    }\n+  }\n+\n+  private boolean isWriterOwnThisLock() {\n+    return g.contains(writeLock);\n+  }\n+\n+  private boolean isReaderOwnThisLock() {\n+    return g.contains(readLock);\n+  }\n+\n+  private boolean isLockFree() {\n+    return g.isEmpty();\n+  }\n+\n+\n+\n+  private void waitUninterruptely(Object o) {","path":"reader-writer-lock/src/main/java/com/iluwatar/reader/writer/lock/ReaderWriterLock.java","position":139,"original_position":139,"commit_id":"3731d26f6d0ac7e89c1504e6527587df07292c5f","original_commit_id":"3731d26f6d0ac7e89c1504e6527587df07292c5f","user":{"login":"npathai","id":1792515,"avatar_url":"https://avatars.githubusercontent.com/u/1792515?v=3","gravatar_id":"","url":"https://api.github.com/users/npathai","html_url":"https://github.com/npathai","followers_url":"https://api.github.com/users/npathai/followers","following_url":"https://api.github.com/users/npathai/following{/other_user}","gists_url":"https://api.github.com/users/npathai/gists{/gist_id}","starred_url":"https://api.github.com/users/npathai/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/npathai/subscriptions","organizations_url":"https://api.github.com/users/npathai/orgs","repos_url":"https://api.github.com/users/npathai/repos","events_url":"https://api.github.com/users/npathai/events{/privacy}","received_events_url":"https://api.github.com/users/npathai/received_events","type":"User","site_admin":false},"body":"Typo: `waitUninterruptibly()`","created_at":"2016-01-07T10:43:09Z","updated_at":"2016-01-07T10:43:09Z","html_url":"https://github.com/iluwatar/java-design-patterns/pull/326#discussion_r49061112","pull_request_url":"https://api.github.com/repos/iluwatar/java-design-patterns/pulls/326","_links":{"self":{"href":"https://api.github.com/repos/iluwatar/java-design-patterns/pulls/comments/49061112"},"html":{"href":"https://github.com/iluwatar/java-design-patterns/pull/326#discussion_r49061112"},"pull_request":{"href":"https://api.github.com/repos/iluwatar/java-design-patterns/pulls/326"}}},"pull_request":{"url":"https://api.github.com/repos/iluwatar/java-design-patterns/pulls/326","id":54597944,"html_url":"https://github.com/iluwatar/java-design-patterns/pull/326","diff_url":"https://github.com/iluwatar/java-design-patterns/pull/326.diff","patch_url":"https://github.com/iluwatar/java-design-patterns/pull/326.patch","issue_url":"https://api.github.com/repos/iluwatar/java-design-patterns/issues/326","number":326,"state":"open","locked":false,"title":"implants the #75 reader writer lock","user":{"login":"hoswey","id":3689445,"avatar_url":"https://avatars.githubusercontent.com/u/3689445?v=3","gravatar_id":"","url":"https://api.github.com/users/hoswey","html_url":"https://github.com/hoswey","followers_url":"https://api.github.com/users/hoswey/followers","following_url":"https://api.github.com/users/hoswey/following{/other_user}","gists_url":"https://api.github.com/users/hoswey/gists{/gist_id}","starred_url":"https://api.github.com/users/hoswey/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hoswey/subscriptions","organizations_url":"https://api.github.com/users/hoswey/orgs","repos_url":"https://api.github.com/users/hoswey/repos","events_url":"https://api.github.com/users/hoswey/events{/privacy}","received_events_url":"https://api.github.com/users/hoswey/received_events","type":"User","site_admin":false},"body":"Please help to review,  it seems the test case is hard to write for such concurrent pattern. :(","created_at":"2015-12-26T06:29:40Z","updated_at":"2016-01-07T10:43:09Z","closed_at":null,"merged_at":null,"merge_commit_sha":"1b3649b9c2321ed000cbaa0925dc0e934e698dfb","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/iluwatar/java-design-patterns/pulls/326/commits","review_comments_url":"https://api.github.com/repos/iluwatar/java-design-patterns/pulls/326/comments","review_comment_url":"https://api.github.com/repos/iluwatar/java-design-patterns/pulls/comments{/number}","comments_url":"https://api.github.com/repos/iluwatar/java-design-patterns/issues/326/comments","statuses_url":"https://api.github.com/repos/iluwatar/java-design-patterns/statuses/3731d26f6d0ac7e89c1504e6527587df07292c5f","head":{"label":"hoswey:master","ref":"master","sha":"3731d26f6d0ac7e89c1504e6527587df07292c5f","user":{"login":"hoswey","id":3689445,"avatar_url":"https://avatars.githubusercontent.com/u/3689445?v=3","gravatar_id":"","url":"https://api.github.com/users/hoswey","html_url":"https://github.com/hoswey","followers_url":"https://api.github.com/users/hoswey/followers","following_url":"https://api.github.com/users/hoswey/following{/other_user}","gists_url":"https://api.github.com/users/hoswey/gists{/gist_id}","starred_url":"https://api.github.com/users/hoswey/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hoswey/subscriptions","organizations_url":"https://api.github.com/users/hoswey/orgs","repos_url":"https://api.github.com/users/hoswey/repos","events_url":"https://api.github.com/users/hoswey/events{/privacy}","received_events_url":"https://api.github.com/users/hoswey/received_events","type":"User","site_admin":false},"repo":{"id":46169071,"name":"java-design-patterns","full_name":"hoswey/java-design-patterns","owner":{"login":"hoswey","id":3689445,"avatar_url":"https://avatars.githubusercontent.com/u/3689445?v=3","gravatar_id":"","url":"https://api.github.com/users/hoswey","html_url":"https://github.com/hoswey","followers_url":"https://api.github.com/users/hoswey/followers","following_url":"https://api.github.com/users/hoswey/following{/other_user}","gists_url":"https://api.github.com/users/hoswey/gists{/gist_id}","starred_url":"https://api.github.com/users/hoswey/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hoswey/subscriptions","organizations_url":"https://api.github.com/users/hoswey/orgs","repos_url":"https://api.github.com/users/hoswey/repos","events_url":"https://api.github.com/users/hoswey/events{/privacy}","received_events_url":"https://api.github.com/users/hoswey/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/hoswey/java-design-patterns","description":"Design pattern samples implemented in Java","fork":true,"url":"https://api.github.com/repos/hoswey/java-design-patterns","forks_url":"https://api.github.com/repos/hoswey/java-design-patterns/forks","keys_url":"https://api.github.com/repos/hoswey/java-design-patterns/keys{/key_id}","collaborators_url":"https://api.github.com/repos/hoswey/java-design-patterns/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/hoswey/java-design-patterns/teams","hooks_url":"https://api.github.com/repos/hoswey/java-design-patterns/hooks","issue_events_url":"https://api.github.com/repos/hoswey/java-design-patterns/issues/events{/number}","events_url":"https://api.github.com/repos/hoswey/java-design-patterns/events","assignees_url":"https://api.github.com/repos/hoswey/java-design-patterns/assignees{/user}","branches_url":"https://api.github.com/repos/hoswey/java-design-patterns/branches{/branch}","tags_url":"https://api.github.com/repos/hoswey/java-design-patterns/tags","blobs_url":"https://api.github.com/repos/hoswey/java-design-patterns/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/hoswey/java-design-patterns/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/hoswey/java-design-patterns/git/refs{/sha}","trees_url":"https://api.github.com/repos/hoswey/java-design-patterns/git/trees{/sha}","statuses_url":"https://api.github.com/repos/hoswey/java-design-patterns/statuses/{sha}","languages_url":"https://api.github.com/repos/hoswey/java-design-patterns/languages","stargazers_url":"https://api.github.com/repos/hoswey/java-design-patterns/stargazers","contributors_url":"https://api.github.com/repos/hoswey/java-design-patterns/contributors","subscribers_url":"https://api.github.com/repos/hoswey/java-design-patterns/subscribers","subscription_url":"https://api.github.com/repos/hoswey/java-design-patterns/subscription","commits_url":"https://api.github.com/repos/hoswey/java-design-patterns/commits{/sha}","git_commits_url":"https://api.github.com/repos/hoswey/java-design-patterns/git/commits{/sha}","comments_url":"https://api.github.com/repos/hoswey/java-design-patterns/comments{/number}","issue_comment_url":"https://api.github.com/repos/hoswey/java-design-patterns/issues/comments{/number}","contents_url":"https://api.github.com/repos/hoswey/java-design-patterns/contents/{+path}","compare_url":"https://api.github.com/repos/hoswey/java-design-patterns/compare/{base}...{head}","merges_url":"https://api.github.com/repos/hoswey/java-design-patterns/merges","archive_url":"https://api.github.com/repos/hoswey/java-design-patterns/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/hoswey/java-design-patterns/downloads","issues_url":"https://api.github.com/repos/hoswey/java-design-patterns/issues{/number}","pulls_url":"https://api.github.com/repos/hoswey/java-design-patterns/pulls{/number}","milestones_url":"https://api.github.com/repos/hoswey/java-design-patterns/milestones{/number}","notifications_url":"https://api.github.com/repos/hoswey/java-design-patterns/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/hoswey/java-design-patterns/labels{/name}","releases_url":"https://api.github.com/repos/hoswey/java-design-patterns/releases{/id}","created_at":"2015-11-14T09:33:42Z","updated_at":"2015-11-14T09:33:44Z","pushed_at":"2015-12-26T06:17:30Z","git_url":"git://github.com/hoswey/java-design-patterns.git","ssh_url":"git@github.com:hoswey/java-design-patterns.git","clone_url":"https://github.com/hoswey/java-design-patterns.git","svn_url":"https://github.com/hoswey/java-design-patterns","homepage":"http://java-design-patterns.com","size":8288,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":1,"mirror_url":null,"open_issues_count":0,"forks":1,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"iluwatar:master","ref":"master","sha":"cec9a99410a0544ff0f1a5853443ef9dcff0a9e7","user":{"login":"iluwatar","id":582346,"avatar_url":"https://avatars.githubusercontent.com/u/582346?v=3","gravatar_id":"","url":"https://api.github.com/users/iluwatar","html_url":"https://github.com/iluwatar","followers_url":"https://api.github.com/users/iluwatar/followers","following_url":"https://api.github.com/users/iluwatar/following{/other_user}","gists_url":"https://api.github.com/users/iluwatar/gists{/gist_id}","starred_url":"https://api.github.com/users/iluwatar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/iluwatar/subscriptions","organizations_url":"https://api.github.com/users/iluwatar/orgs","repos_url":"https://api.github.com/users/iluwatar/repos","events_url":"https://api.github.com/users/iluwatar/events{/privacy}","received_events_url":"https://api.github.com/users/iluwatar/received_events","type":"User","site_admin":false},"repo":{"id":22790488,"name":"java-design-patterns","full_name":"iluwatar/java-design-patterns","owner":{"login":"iluwatar","id":582346,"avatar_url":"https://avatars.githubusercontent.com/u/582346?v=3","gravatar_id":"","url":"https://api.github.com/users/iluwatar","html_url":"https://github.com/iluwatar","followers_url":"https://api.github.com/users/iluwatar/followers","following_url":"https://api.github.com/users/iluwatar/following{/other_user}","gists_url":"https://api.github.com/users/iluwatar/gists{/gist_id}","starred_url":"https://api.github.com/users/iluwatar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/iluwatar/subscriptions","organizations_url":"https://api.github.com/users/iluwatar/orgs","repos_url":"https://api.github.com/users/iluwatar/repos","events_url":"https://api.github.com/users/iluwatar/events{/privacy}","received_events_url":"https://api.github.com/users/iluwatar/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/iluwatar/java-design-patterns","description":"Design patterns implemented in Java","fork":false,"url":"https://api.github.com/repos/iluwatar/java-design-patterns","forks_url":"https://api.github.com/repos/iluwatar/java-design-patterns/forks","keys_url":"https://api.github.com/repos/iluwatar/java-design-patterns/keys{/key_id}","collaborators_url":"https://api.github.com/repos/iluwatar/java-design-patterns/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/iluwatar/java-design-patterns/teams","hooks_url":"https://api.github.com/repos/iluwatar/java-design-patterns/hooks","issue_events_url":"https://api.github.com/repos/iluwatar/java-design-patterns/issues/events{/number}","events_url":"https://api.github.com/repos/iluwatar/java-design-patterns/events","assignees_url":"https://api.github.com/repos/iluwatar/java-design-patterns/assignees{/user}","branches_url":"https://api.github.com/repos/iluwatar/java-design-patterns/branches{/branch}","tags_url":"https://api.github.com/repos/iluwatar/java-design-patterns/tags","blobs_url":"https://api.github.com/repos/iluwatar/java-design-patterns/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/iluwatar/java-design-patterns/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/iluwatar/java-design-patterns/git/refs{/sha}","trees_url":"https://api.github.com/repos/iluwatar/java-design-patterns/git/trees{/sha}","statuses_url":"https://api.github.com/repos/iluwatar/java-design-patterns/statuses/{sha}","languages_url":"https://api.github.com/repos/iluwatar/java-design-patterns/languages","stargazers_url":"https://api.github.com/repos/iluwatar/java-design-patterns/stargazers","contributors_url":"https://api.github.com/repos/iluwatar/java-design-patterns/contributors","subscribers_url":"https://api.github.com/repos/iluwatar/java-design-patterns/subscribers","subscription_url":"https://api.github.com/repos/iluwatar/java-design-patterns/subscription","commits_url":"https://api.github.com/repos/iluwatar/java-design-patterns/commits{/sha}","git_commits_url":"https://api.github.com/repos/iluwatar/java-design-patterns/git/commits{/sha}","comments_url":"https://api.github.com/repos/iluwatar/java-design-patterns/comments{/number}","issue_comment_url":"https://api.github.com/repos/iluwatar/java-design-patterns/issues/comments{/number}","contents_url":"https://api.github.com/repos/iluwatar/java-design-patterns/contents/{+path}","compare_url":"https://api.github.com/repos/iluwatar/java-design-patterns/compare/{base}...{head}","merges_url":"https://api.github.com/repos/iluwatar/java-design-patterns/merges","archive_url":"https://api.github.com/repos/iluwatar/java-design-patterns/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/iluwatar/java-design-patterns/downloads","issues_url":"https://api.github.com/repos/iluwatar/java-design-patterns/issues{/number}","pulls_url":"https://api.github.com/repos/iluwatar/java-design-patterns/pulls{/number}","milestones_url":"https://api.github.com/repos/iluwatar/java-design-patterns/milestones{/number}","notifications_url":"https://api.github.com/repos/iluwatar/java-design-patterns/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/iluwatar/java-design-patterns/labels{/name}","releases_url":"https://api.github.com/repos/iluwatar/java-design-patterns/releases{/id}","created_at":"2014-08-09T16:45:18Z","updated_at":"2016-01-07T08:57:19Z","pushed_at":"2016-01-06T17:59:47Z","git_url":"git://github.com/iluwatar/java-design-patterns.git","ssh_url":"git@github.com:iluwatar/java-design-patterns.git","clone_url":"https://github.com/iluwatar/java-design-patterns.git","svn_url":"https://github.com/iluwatar/java-design-patterns","homepage":"http://java-design-patterns.com","size":9876,"stargazers_count":9454,"watchers_count":9454,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":3063,"mirror_url":null,"open_issues_count":119,"forks":3063,"open_issues":119,"watchers":9454,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/iluwatar/java-design-patterns/pulls/326"},"html":{"href":"https://github.com/iluwatar/java-design-patterns/pull/326"},"issue":{"href":"https://api.github.com/repos/iluwatar/java-design-patterns/issues/326"},"comments":{"href":"https://api.github.com/repos/iluwatar/java-design-patterns/issues/326/comments"},"review_comments":{"href":"https://api.github.com/repos/iluwatar/java-design-patterns/pulls/326/comments"},"review_comment":{"href":"https://api.github.com/repos/iluwatar/java-design-patterns/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/iluwatar/java-design-patterns/pulls/326/commits"},"statuses":{"href":"https://api.github.com/repos/iluwatar/java-design-patterns/statuses/3731d26f6d0ac7e89c1504e6527587df07292c5f"}}}}