Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can anyone please tell me the purpose of ThreadLocal in DomainEventPublisher? #6

Open
njzhxf opened this issue May 2, 2013 · 10 comments

Comments

@njzhxf
Copy link

njzhxf commented May 2, 2013

Can anyone please tell me the purpose of ThreadLocal in DomainEventPublisher? Does that mean every thread needs to maintain a list of its own subsriber list?

@TorbenRahbekKoch
Copy link

The DomainEventPublisher (here) is a static and synchronous component where events are published synchronously in the same thread - and subscribed to by the same thread. The ThreadLocal is used to have storage specific to each thread, so different threads can publish at the same time without conflicting.

@VaughnVernon
Copy link
Owner

I totally missed the original comment. The DomainEventPublisher has a static (class-level) interface for obtaining the instance that belongs to the current thread. However, there are also the instance methods publish(), subscribe(), reset(), that operate on the ThreadLocal instance. In any given thread there is likely only one or a few subscribers registered, so it's not a lot of overhead. Also note that at some point when a thread is assigned to an incoming server request, the subscriber list for the thread must be reset using DomainEventPublisher.instance().reset() so that subscriber(s) from the previous request using the same thread will not be left on the subscriber list.

@leonfs
Copy link

leonfs commented Oct 24, 2013

Looking at the implementation of the DomainEventPublisher I struggle to understand why do you have the publishing lock. Because you are running one instance per thread there are no potential concurrency issues. Why would you want to have a lock like that?

Probably I'm missing some fundamental concept. But it seems to me quite redundant based on the fact that you have one instance per Thread (thanks to ThreadLocal).

@VaughnVernon
Copy link
Owner

The DomainEventPublisher must protect itself from the subscribers. For
example, when a subscriber's handle() method has been called back on, the
handle() method could attempt to register another subscriber. Even though
it's all happening on a single thread, that would cause a
ConcurrentModificationException on the collection.

On Thu, Oct 24, 2013 at 3:40 AM, Leonardo Fernandez <
notifications@github.com> wrote:

Looking at the implementation of the DomainEventPublisher I struggle to
understand why do you have the publishing lock. Because you are running one
instance per thread there are no potential concurrency issues. Why would
you want to have a lock like that?

Probably I'm missing some fundamental concept. But it seems to me quite
redundant based on the fact that you have one instance per Thread (thanks
to ThreadLocal).


Reply to this email directly or view it on GitHubhttps://github.com//issues/6#issuecomment-26978779
.

@gaigeshen
Copy link

Yeah! I see.

@VaughnVernon
Copy link
Owner

Please see Chapter 8 of my book.

@gaigeshen
Copy link

Hum, I am reading this book at now, and thank you very much! It has been translated into Chinese.

@batmanhaha
Copy link

what if
thread_A subscribe(payEventSubscriber)
thread_B publish(payEvent)

it can`t work...

@VaughnVernon
Copy link
Owner

VaughnVernon commented May 9, 2018 via email

@batmanhaha
Copy link

i have known that after reading the code.
thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants