[Java concurrent programming] 12. ThreadLocal solves SimpleDateFormat's non-thread security and threadlocal concurrency.
Tim Cull encounters a serious performance problem caused by SimpleDateFormat. This problem is mainly caused by SimpleDateFormat. it is expensive to create a SimpleDateFormat instance, when parsing strings, instances with a short life cycle are frequently created, resulting in poor performance. Even if SimpleDateFormat is defined as a static class variable, it seems to be able to solve this problem, but SimpleDateFormat is non-thread-safe, and there are also problems. If we use 'synchronized' for thread synchronization, synchronization causes performance degradation (obtaining SimpleDateFormat instances serialized between threads ).
As early as JDK 1.2, java. lang. ThreadLocal is provided. ThreadLocal provides a new way of thinking to solve the concurrent problem of multi-threaded programs. Using this tool class, you can easily compile beautiful multi-threaded programs.
ThreadLocal is easy to understand and take for granted as a "local thread ". In fact, ThreadLocal is not a Thread, but a local variable of the Thread. It may be easier to understand to name it ThreadLocalVariable.
When ThreadLocal is used to maintain a variable, ThreadLocal provides an independent copy of the variable for each thread that uses the variable. Therefore, each thread can change its own copy independently, it does not affect the copies corresponding to other threads.
From the thread perspective, the target variable is like the Local variable of the thread, which is also the meaning of "Local" in the class name.
Tim Cull uses Threadlocal to solve this problem. For each thread SimpleDateFormat, there is no status that affects the collaboration between them. Create a copy or a copy of the SimpleDateFormat variable for each thread. The Code is as follows:
Import java. text. dateFormat; import java. text. parseException; import java. text. simpleDateFormat; import java. util. date;/*** use ThreadLocal to change the space for time to solve the SimpleDateFormat thread security problem. */Public class DateUtil {private static final String DATE_FORMAT = "yyyy-MM-dd HH: mm: ss"; @ SuppressWarnings ("rawtypes ") private static ThreadLocal threadLocal = new ThreadLocal () {protected synchronized Object initialValue () {return new SimpleDateFormat (DATE_FORMAT) ;}; public static DateFormat getDateFormat () {return (DateFormat) threadLocal. get ();} public static Date parse (String textDate) throws ParseException {return getDateFormat (). parse (textDate );}}
Create a ThreadLocal variable. Here, an anonymous class is used during the creation, covering the initialValue method. The main function is to initialize the instance during the creation. You can also use the following method to create an instance;
Import java. text. dateFormat; import java. text. simpleDateFormat;/*** use ThreadLocal to solve SimpleDateFormat thread security issues with space for time */public class DateUtil {private static final String DATE_FORMAT = "yyyy-MM-dd HH: mm: ss "; private static ThreadLocal threadLocal = new ThreadLocal (); // The first call to get Will return null // get the variable copy of the thread. If initialValue is not overwritten, the first get will return null, // Therefore, A SimpleDateFormat must be initialized and set to public static DateFormat getDateFormat () {DateFormat df = (DateFormat) threadLocal in threadLocal. get (); if (df = null) {df = new SimpleDateFormat (DATE_FORMAT); threadLocal. set (df) ;}return df ;}}
Of course, you can also use:
The DateFormatUtils or FastDateFormat Implementation of the apache commons-lang package ensures thread security and is more efficient.