You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by Keith McNeill <mc...@streambase.com> on 2007/03/27 23:59:09 UTC
per thread state
I need to store some state that can be shared on a per thread basis (not
per session). What pattern does mina use for this? Does mina have a
spot for data like this or should one use ThreadLocal to acomplish this?
Thanks,
Keith
Re: per thread state
Posted by Keith McNeill <mc...@streambase.com>.
I need to be able to cache some low frequency network connections
somewhere (think something like a database connection). These
connections don't need to be maintained on a session basis, just on a
basis so that they are thread safe. The usage is low frequency so I
could just synchronize around one or a few of these connections but it
would be nice if there was some what for mina to help manage the state.
I'll sort it out some other way, either via thread local or some other
mechanism.
Thanks!
Keith
Trustin Lee wrote:
> Hi Keith,
>
> On 3/28/07, Keith McNeill <mc...@streambase.com> wrote:
>> I need to store some state that can be shared on a per thread basis (not
>> per session). What pattern does mina use for this? Does mina have a
>> spot for data like this or should one use ThreadLocal to acomplish this?
>
> MINA doesn't provide any particular utility to store shared data on a
> per-thread basis because I don't find any use case for it. As you
> know, once thread pool is employed, the assigned thread can be changed
> whenever. Session attributes are the only reliable storage IMHO. I
> could give you better answer if more detailed use case is described.
>
> HTH,
> Trustin
Re: per thread state
Posted by Trustin Lee <tr...@gmail.com>.
Hi Greg,
On 3/28/07, Greg Duffy <gd...@gmail.com> wrote:
> Per ThreadLocal JavaDoc, "Each thread holds an implicit reference to its
> copy of a thread-local variable as long as the thread is alive and the
> ThreadLocal instance is accessible; after a thread goes away, all of its
> copies of thread-local instances are subject to garbage collection (unless
> other references to these copies exist)."
>
> AFAIK, ThreadLocal<T> is implemented with WeakHashMap<Thread, T>, so as long
> as there isn't a reference to the Thread in T, T will be available for
> garbage collection after thread death.
>
> Also, I should clarify: it's not that the resource couldn't be created for
> every session, it just has a somewhat expensive initialization time and can
> be shared non-concurrently ... so it's nice to associate it with a thread.
>
> I've also passed values between IoFilters with ThreadLocals. Maybe there's a
> better way to do these things that I am just missing.
>
> Of course, please correct me if my understanding is faulty, because I'll
> need to go fix some code ASAP! :)
It's absolutely OK if there's not many threads. :)
WeakHashMap sometimes causes OutOfMemoryError because its underlying
table (array) doesn't shrink even if the WeakReference is clared out.
For example, you can have an array whose length is huge and filled
with null. But it's not your case. :)
Trustin
--
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP Key ID: 0x0255ECA6
Re: per thread state
Posted by Greg Duffy <gd...@gmail.com>.
Per ThreadLocal JavaDoc, "Each thread holds an implicit reference to its
copy of a thread-local variable as long as the thread is alive and the
ThreadLocal instance is accessible; after a thread goes away, all of its
copies of thread-local instances are subject to garbage collection (unless
other references to these copies exist)."
AFAIK, ThreadLocal<T> is implemented with WeakHashMap<Thread, T>, so as long
as there isn't a reference to the Thread in T, T will be available for
garbage collection after thread death.
Also, I should clarify: it's not that the resource couldn't be created for
every session, it just has a somewhat expensive initialization time and can
be shared non-concurrently ... so it's nice to associate it with a thread.
I've also passed values between IoFilters with ThreadLocals. Maybe there's a
better way to do these things that I am just missing.
Of course, please correct me if my understanding is faulty, because I'll
need to go fix some code ASAP! :)
-Greg
On 3/27/07, Trustin Lee <tr...@gmail.com> wrote:
>
> On 3/28/07, Greg Duffy <gd...@gmail.com> wrote:
> > Trustin,
> >
> > I've used ThreadLocals to store references to certain resources that can
> be
> > reused for many sessions but cannot be used concurrently by multiple
> > threads. However, with this method I didn't find any limitations that
> MINA
> > needed to address.
>
> Then when do you remove the allocated local values? There might be
> memory leak unless ThreadLocal.remove () is invoked.
>
> Trustin
> --
> what we call human nature is actually human habit
> --
> http://gleamynode.net/
> --
> PGP Key ID: 0x0255ECA6
>
Re: per thread state
Posted by Trustin Lee <tr...@gmail.com>.
On 3/28/07, Greg Duffy <gd...@gmail.com> wrote:
> Trustin,
>
> I've used ThreadLocals to store references to certain resources that can be
> reused for many sessions but cannot be used concurrently by multiple
> threads. However, with this method I didn't find any limitations that MINA
> needed to address.
Then when do you remove the allocated local values? There might be
memory leak unless ThreadLocal.remove() is invoked.
Trustin
--
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP Key ID: 0x0255ECA6
Re: per thread state
Posted by Greg Duffy <gd...@gmail.com>.
Trustin,
I've used ThreadLocals to store references to certain resources that can be
reused for many sessions but cannot be used concurrently by multiple
threads. However, with this method I didn't find any limitations that MINA
needed to address.
-Greg
On 3/27/07, Trustin Lee <tr...@gmail.com> wrote:
>
> Hi Keith,
>
> On 3/28/07, Keith McNeill <mc...@streambase.com> wrote:
> > I need to store some state that can be shared on a per thread basis (not
> > per session). What pattern does mina use for this? Does mina have a
> > spot for data like this or should one use ThreadLocal to acomplish this?
>
> MINA doesn't provide any particular utility to store shared data on a
> per-thread basis because I don't find any use case for it. As you
> know, once thread pool is employed, the assigned thread can be changed
> whenever. Session attributes are the only reliable storage IMHO. I
> could give you better answer if more detailed use case is described.
>
> HTH,
> Trustin
> --
> what we call human nature is actually human habit
> --
> http://gleamynode.net/
> --
> PGP Key ID: 0x0255ECA6
>
Re: per thread state
Posted by Trustin Lee <tr...@gmail.com>.
Hi Keith,
On 3/28/07, Keith McNeill <mc...@streambase.com> wrote:
> I need to store some state that can be shared on a per thread basis (not
> per session). What pattern does mina use for this? Does mina have a
> spot for data like this or should one use ThreadLocal to acomplish this?
MINA doesn't provide any particular utility to store shared data on a
per-thread basis because I don't find any use case for it. As you
know, once thread pool is employed, the assigned thread can be changed
whenever. Session attributes are the only reliable storage IMHO. I
could give you better answer if more detailed use case is described.
HTH,
Trustin
--
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP Key ID: 0x0255ECA6
Re: per thread state
Posted by Greg Duffy <gd...@gmail.com>.
I've done this with ThreadLocals in both IoFilters and IoHandlers. Is
ThreadLocal missing some functionality that you require?
-Greg
On 3/27/07, Keith McNeill <mc...@streambase.com> wrote:
>
> I need to store some state that can be shared on a per thread basis (not
> per session). What pattern does mina use for this? Does mina have a
> spot for data like this or should one use ThreadLocal to acomplish this?
>
> Thanks,
>
> Keith
>