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
Post a Comment