ThreadLocal in One Click
October 30th, 2009 by Anna KozlovaMost of applications initially are single threaded, and IntelliJ IDEA was no different; though luckily, now it isn’t — but we had to adapt our code to use multiple threads. In this post I’m going to tell you how.
In our example we see SAXBuilder, which is too expensive to be created every time we need it, so it is stored in a static final field.

Because SAXBuilder is not thread safe, multiple calls to loadDocument from different threads cause a lot of interesting exceptions. This is why we need either to make access to this field synchronized, or to make the field ThreadLocal. In our case, we’re choosing the latter. We encapsulate the field, change its type and initializer, then fix a generated getter. Quite a lot of work, right? Luckily in Maia you can do it all in just one click. Just place caret on a field and press Alt+Enter to smoothly migrate it to ThreadLocal.

Tags: Code Insight, maia
October 30th, 2009 at 6:52 am
These “little” details makes idea the best IDE
October 30th, 2009 at 8:29 am
hi there,
cool!
how long before eclipse copies it ? (as it’s been doing for the last 8 years
)
BR,
~A
March 1st, 2010 at 8:06 am
Unfortunately, this wonderful little trick doesn’t work in the Community Edition =(