What is ThreadLocal class? How can it be used?

Ans:Below are some key points about ThreadLocal variables
  • A thread-local variable effectively provides a separate copy of its value for each thread that uses it.
  • ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread
  • In case when multiple threads access a ThreadLocal instance, separate copy of Threadlocal variable is maintained for each thread.
  • Common use is seen in DAO pattern where the DAO class can be singleton but the Database connection can be maintained separately for each thread. (Per Thread Singleton) ThreadLocal variable are difficult to understand and I have found below reference links very useful in getting better understanding on them

Q:When InvalidMonitorStateException is thrown? Why?
Ans: This exception is thrown when you try to call wait()/notify()/notifyAll() any of these methods for an Object from a point in your program where u are NOT having a lock on that object.(i.e. u r not executing any synchronized block/method of that object and still trying to call wait()/notify()/notifyAll()) wait(), notify() and notifyAll() all throw IllegalMonitorStateException. since This exception is a subclass of RuntimeException so we r not bound to catch it (although u may if u want to). and being a RuntimeException this exception is not mentioned in the signature of wait(), notify(), notifyAll() methods.

People who read this post also read :


In this manner my pal Wesley Virgin's biography starts in this SHOCKING AND CONTROVERSIAL video.

Wesley was in the military-and shortly after leaving-he unveiled hidden, "MIND CONTROL" tactics that the government and others used to get everything they want.

As it turns out, these are the same tactics tons of celebrities (especially those who "became famous out of nowhere") and the greatest business people used to become rich and successful.

You probably know that you only use 10% of your brain.

That's really because most of your brain's power is UNCONSCIOUS.

Perhaps this conversation has even occurred INSIDE your very own head... as it did in my good friend Wesley Virgin's head around seven years back, while riding an unregistered, garbage bucket of a car without a license and in his bank account.

"I'm very fed up with living check to check! When will I get my big break?"

You've taken part in those types of thoughts, right?

Your success story is waiting to happen. You need to start believing in YOURSELF.

Learn How To Become A MILLIONAIRE Fast

Post a Comment


Twitter Delicious Facebook Digg Stumbleupon Favorites More