{"action":"opened","issue":{"url":"https://api.github.com/repos/getheimdall/heimdall/issues/24","repository_url":"https://api.github.com/repos/getheimdall/heimdall","labels_url":"https://api.github.com/repos/getheimdall/heimdall/issues/24/labels{/name}","comments_url":"https://api.github.com/repos/getheimdall/heimdall/issues/24/comments","events_url":"https://api.github.com/repos/getheimdall/heimdall/issues/24/events","html_url":"https://github.com/getheimdall/heimdall/issues/24","id":326199090,"number":24,"title":"ThreadSafe Singleton","user":{"login":"marceloaguiarr","id":6094429,"avatar_url":"https://avatars0.githubusercontent.com/u/6094429?v=4","gravatar_id":"","url":"https://api.github.com/users/marceloaguiarr","html_url":"https://github.com/marceloaguiarr","followers_url":"https://api.github.com/users/marceloaguiarr/followers","following_url":"https://api.github.com/users/marceloaguiarr/following{/other_user}","gists_url":"https://api.github.com/users/marceloaguiarr/gists{/gist_id}","starred_url":"https://api.github.com/users/marceloaguiarr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/marceloaguiarr/subscriptions","organizations_url":"https://api.github.com/users/marceloaguiarr/orgs","repos_url":"https://api.github.com/users/marceloaguiarr/repos","events_url":"https://api.github.com/users/marceloaguiarr/events{/privacy}","received_events_url":"https://api.github.com/users/marceloaguiarr/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-05-24T16:29:33Z","updated_at":"2018-05-24T16:29:33Z","closed_at":null,"author_association":"CONTRIBUTOR","body":"**The TraceContextHolder class need to be a thread safe singleton**\r\nAs a singleton this class holds a instance of itself and has a getInstance method. But strategy used to create the singleton was to add the `synchronized` keyword to the get instance method and check if the instance is created before returning it. This creates a synchronization overhead to every call to the getInstance method.\r\n\r\n**Using the Initialization-on-demand holder idiom**\r\nBy using the Initialization-on-demand holder idiom we can remove the `synchronized` keyword from the getInstance method and still achieve thread safety by using the JVM class initialization optimisations.\r\n\r\n**Another alternative**\r\nAnother alternative is to use the Eager Initalization idiom. The diference from the Initialization-on-demand idiom is that the Initialization-on-demand idiom is also a lazy initialization alternative.\r\n\r\n**Benchmarks**\r\nI have created a benchmark project with all three strategies that I have cited above. Here is the [link](https://github.com/marceloaguiarr/SingletonBenchmark)\r\n\r\nSummary of the results:\r\n\r\n|Benchmark                             |Mode | Cnt |  Score |   Error | Units\r\n| :-                                   | :-: | :-: |   :-:  |   :-:   | :-:   \r\n|SingletonBenchmark.EagerHolderBench   |avgt |  10 |  0.065 | ± 0.001 | us/op\r\n|SingletonBenchmark.LazyHolderBench    |avgt |  10 |  0.064 | ± 0.001 | us/op\r\n|SingletonBenchmark.SynchronizedBench  |avgt |  10 | 38.562 | ± 0.171 | us/op"}}
{"actor":{"display_login":"marceloaguiarr"}}