concept thread in category dependency injection

This is an excerpt from Manning's book Dependency Injection.
Table 8.2 Bad Lifestyle choices covered in this section (view table figure)
Subject Type Description Captive Dependencies Bug Keeps Dependencies referenced beyond their expected lifetime Leaky Abstractions Design issue Uses Leaky Abstractions, leaking Lifestyle choices to consumers Per-thread Lifestyle Bug Causes concurrency bugs by tying instances to the lifetime of a thread As table 8.2 states, Captive Dependencies and the per-thread Lifestyle can cause bugs in your application. More often than not, these bugs only appear after deploying the application to production, because they are concurrency related. When we start the application, as developers, we typically run it for a short period of time, one request at a time. The same holds true for testers that typically go through the application in an orderly fashion. This might hide such problems, which only pop up when multiple users access the application concurrently.
Listing 8.23 A Dependency’s lifetime tied to the lifetime of a thread
[ThreadStatic] #1 private static CommerceContext context; #1 static CurrencyParser CreateCurrencyParser( string connectionString) { if (context == null) #2 { #2 context = new CommerceContext( #2 connectionString); #2 } #2 return new CurrencyParser( #3 new SqlExchangeRateProvider(context), #3 context); #3 } #1 A static field is marked with the [ThreadStatic] attribute. The CLR ensures that such a field isn’t shared between threads, instead providing each executing thread a separate instance of the field. If the field is accessed on a different thread, it’ll contain a different value. #2 In case the thread on which the current code is executing doesn’t have an initialized CommerceContext yet, a new instance is created and stored in the corresponding thread-static field. #3 Injects the per-thread Dependency into a transient object graph

This is an excerpt from Manning's book Dependency Injection: Design Patterns Using Spring and Guice.
Finally, a configuration you can rely on! All dependencies are accounted for and the initialization hook is configured correctly. Or is it? Has everything been accounted for? Recall that objects in Spring are singletons by default. This means that other multiple threads are concurrently accessing unsafe5. While the injector is itself safe to concurrent accesses of this sort, UnsafeObject may not be. The Java Memory Model makes no guarantees about the visibility of non-final, non-volatile fields across threads. To fix this, you can remove the concept of shared visibility, so an instance is exposed to only a single thread:
It’s easy to see how this problem can creep into DI as well. Unless properly published, objects may appear incompletely constructed to participating threads, with unavailable or partially constructed dependencies. Consider the equivalent of the hashtable we just saw:
![]()
MoreUnsafePublication is a simple variant of the hashtable that reads an email address using its dependency EmailDatabase. This time the issue of publication is not with the hashtable values but with the EmailDatabase dependency itself. Threads that call method read() cannot rely on the fact that the dependency is available. Without sufficient synchronization, the thread creating the object does not safely publish its fields to other threads. The following could easily result in a NullPointerException:
In the non-atomic situation, intervening threads can easily alter count concurrently and corrupt its value. With the atomic incrementAndGet() this cannot happen.
Furthermore, if a thread is slow in incrementing its count (due to additional work that it’s doing, perhaps), this doesn’t block other threads from making progress in the meantime as synchronization would. This leads to real concurrency of threads and a substantial increase in throughput.