java - When and how should I use a ThreadLocal variable? -


When should I use a threadlocal variable?

How it is used A potential (and common) use occurs when you have something that is not thread-safe, but you want to avoid access to that object (I see you I'm here). Instead, give each thread your own example of the object.

For example:

  Public square fu {// SimpleDateFormat thread is not secure, so give each one a thread threshold for the personal static last thread & lt; Simplified Format & gt; Formatter = new ThreadLocal & lt; Simplified Format & gt; () {@ Override Protected Simple Data Formatting Value () {Return New SimpleDateform ("YYMEMMDHHMM"); }}; Public string format (date of the date) {return formatter.get (). Format (date); }}  

.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -