0

I have some difficulty in understanding the use of ThreadLocal in the below declaration. Does it mean, even though the instance is declared as static each thread will have it's own instance of DomainEventPublisher class

private static final ThreadLocal<DomainEventPublisher> instance = new ThreadLocal<DomainEventPublisher>() {
        protected DomainEventPublisher initialValue() {
            return new DomainEventPublisher();
        }
};
Somasundaram Sekar
  • 5,244
  • 6
  • 43
  • 85

1 Answers1

1

Yes, that is exactly what ThreadLocal is for.

In your example, each thread will have a different instance of DomainEventPublisher

Jean Logeart
  • 52,687
  • 11
  • 83
  • 118