question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Thread scope for Guice

See original GitHub issue

From robbie.vanbrabant on June 08, 2007 06:26:25

As a follow-up to issue #100 , Guice could support a pure Thread scope:

    public static final Scope THREAD = new Scope() {         public <T> Provider<T> scope(final Key<T> key, final Provider<T> creator) {             return new Provider<T>() {                 public T get() {                     ThreadLocalCache cache = ThreadLocalCache.getInstance();                     T value = cache.get(key);                     if (value == null) {                         value = creator.get();                         cache.add(key, value);                     }                     return value;                 }             };         }     };          private static final class ThreadLocalCache {         private Map<Key<?>, Object> map = new HashMap<Key<?>, Object>();         // use lazy init to avoid memory overhead when not using the scope?         private static final ThreadLocal<ThreadLocalCache> THREAD_LOCAL =             new ThreadLocal<ThreadLocalCache>() {                 @Override protected ThreadLocalCache initialValue() {                     return new ThreadLocalCache();                 }         };         // suppress warnings because the add method         // captures the type         @SuppressWarnings(“unchecked”)         public <T> T get(Key<T> key) {             return (T) map.get(key);         }         public <T> void add(Key<T> key, T value) {             map.put(key, value);         }         public static ThreadLocalCache getInstance() {             return THREAD_LOCAL.get();         }     }

As Kevin said, it could be useful when using classes that are not thread safe, like SimpleDateFormat.

_Original issue: http://code.google.com/p/google-guice/issues/detail?id=114_

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:12

github_iconTop GitHub Comments

1reaction
gissuebotcommented, Jul 7, 2014

From crazyboblee on October 05, 2009 11:48:35

Guice has request scope which is similar but better. The problem with the impl above (and just about any thread scope impl) is that it doesn’t remove the thread local values, so it will leak memory in many cases.

0reactions
bit-cracker007commented, Aug 4, 2021

Thanks @GedMarc. Will check 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Scopes · google/guice Wiki - GitHub
Guice (pronounced 'juice') is a lightweight dependency injection framework for Java 8 and above, brought to you by Google. - Scopes ...
Read more >
How / Should I create my own Guice Scope - Stack Overflow
Guice has no internal concept of threads, aside from synchronizing Singleton creation. Singleton scopes are just a Provider-wrapper that creates ...
Read more >
Google Guice - Scopes - Tutorialspoint
Following are the scopes that Guice supports: @Singleton - Single instance for lifetime of the application. @Singleton object needs to be threadsafe.
Read more >
ServletScopes (Guice 4.1 API) - Google
Wraps the given callable in a contextual callable that "continues" the HTTP request in another thread. This acts as a way of transporting...
Read more >
SingletonScope (Google Guice - Core Library 4.0 API)
There is almost no code with Guice that propagates state between threads. And Singleton scope is The exception. 2) Guice supports circular dependencies...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found