{"action":"opened","issue":{"url":"https://api.github.com/repos/google/guava/issues/2662","repository_url":"https://api.github.com/repos/google/guava","labels_url":"https://api.github.com/repos/google/guava/issues/2662/labels{/name}","comments_url":"https://api.github.com/repos/google/guava/issues/2662/comments","events_url":"https://api.github.com/repos/google/guava/issues/2662/events","html_url":"https://github.com/google/guava/issues/2662","id":192253926,"number":2662,"title":"RateLimiter.tryAcquire(long,TimeUnit) ","user":{"login":"daniel-huss","id":3043973,"avatar_url":"https://avatars.githubusercontent.com/u/3043973?v=3","gravatar_id":"","url":"https://api.github.com/users/daniel-huss","html_url":"https://github.com/daniel-huss","followers_url":"https://api.github.com/users/daniel-huss/followers","following_url":"https://api.github.com/users/daniel-huss/following{/other_user}","gists_url":"https://api.github.com/users/daniel-huss/gists{/gist_id}","starred_url":"https://api.github.com/users/daniel-huss/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/daniel-huss/subscriptions","organizations_url":"https://api.github.com/users/daniel-huss/orgs","repos_url":"https://api.github.com/users/daniel-huss/repos","events_url":"https://api.github.com/users/daniel-huss/events{/privacy}","received_events_url":"https://api.github.com/users/daniel-huss/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-11-29T11:15:15Z","updated_at":"2016-11-29T11:15:15Z","closed_at":null,"body":"Hi,\r\n\r\nat first glance, the following program works as expected. However, it causes constant 12% load on an i7-4790 CPU @ 3.60Ghz:\r\n\r\n```java\r\npublic static void main(final String[] args) throws InterruptedException\r\n    {\r\n        RateLimiter limiter = RateLimiter.create(0.1);\r\n        Stopwatch sw = Stopwatch.createStarted();\r\n        while (true) {\r\n          if (Thread.interrupted()) {\r\n              throw new InterruptedException();\r\n          }\r\n          if  (limiter.tryAcquire(200, TimeUnit.MILLISECONDS)) {\r\n              System.out.println(sw.elapsed(TimeUnit.MILLISECONDS) + \" ms\");\r\n          }\r\n }\r\n´´´\r\n**So what's wrong with my code?**\r\n\r\nThe reason for the high CPU usage is that tryAcquire _immediately returns false without waiting_ if it detects that a permit will not become available within the timout period. \r\n\r\nI find this to be very surprising behavior since almost any other Java method that is named `tryDoStuff(timeout)` can be reasonably expected to satisfy _at least one_ of the following postconditions when it returns normally:\r\n\r\n- _Stuff_ has been done\r\n- `timeout` has passed\r\n\r\nBut most of the time, `tryAcquire` will return without satisfying any of those. This is mentioned in the Javadocs but I think it's really easy to read past that part without realizing the consequences. \r\n\r\nIf I had to propose a solution, I'd probably introduce a new method called `acquireOrWait` that implements my use case and/or highlight the consequences of early returns in the `tryAcquire` JavaDocs.\r\n\r\nPlease forgive me if this is a duplicate, I've tried searching first but couldn't find a related issue.\r\n\r\n(Guava version: 19.0)"}}
{"actor":{"display_login":"daniel-huss"}}