<span style= "FONT-SIZE:18PX; Font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > in multi-threaded development, often encountered in the run method calls a common property, because each start will create a thread, so </span>
<span style= "FONT-SIZE:18PX; Font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > </span><span style= "font-size:18px; Font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > Some threads share a property, and when any of them changes the value of this property, the property will be changed in the following use, which </span>
<span style= "FONT-SIZE:18PX; Font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > Homing </span><span style= "FONT-SIZE:18PX; Font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > causes a lot of unexpected things to happen, which is called thread insecurity. Let's start with an example to illustrate the problem. </span>
<span style= "FONT-SIZE:18PX; Font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " ></span>
Package Com.bird.concursey;import Java.util.date;import Java.util.concurrent.timeunit;public class UnsafeTask Implements Runnable{private date startdate; @Overridepublic void Run () {startdate = new date (); System.out.println ("Start thread" + Thread.CurrentThread (). GetId () + "" + StartDate); try {TimeUnit.SECONDS.sleep (int) Math.rint (Math.random () * 10));} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println ("Thread Finish" + Thread.CurrentThread (). GetId () + "" + StartDate);} public static void Main (string[] args) {unsafetask task = new Unsafetask (), for (int i = 0; i <; i++) {Thread thread = New Thread (Task); Thread.Start (); try {TimeUnit.SECONDS.sleep (2);} catch (Interruptedexception e) {e.printstacktrace () ;}}}}
Here we create 10 sub-threads in the main thread, and then each thread instantiates this date, and then you will be amazed to find that when all the threads
After running, their end times are the same and the results are as follows.
start thread 8 Tue Sep 20:21:50 CST 2014start thread 9 Tue Sep 20:21:52 CST 2014start thread ten Tue Sep 20:21:54 CST 2014start t Hread Tue Sep 20:21:56 CST 2014start thread Tue Sep 20:21:58 CST 2014Thread finish Tue Sep 20:21:58 CST 2014Thread Finish Tue Sep 20:21:58 CST 2014Thread finish 8 Tue Sep 20:21:58 CST 2014start thread Tue Sep 16 20 : 22:00 CST 2014Thread finish 9 Tue Sep 20:22:00 CST 2014start thread Tue Sep 20:22:02 CST 2014start thread Tue Sep 20:22:04 CST 2014Thread finish Tue Sep 20:22:04 CST 2014start thread Tue Sep 20:22:06 CST 2014Thread f Inish Tue Sep 20:22:06 CST 2014start thread Tue Sep 20:22:08 CST 2014Thread finish Tue Sep 20:22:08 CST 2014Thread Finish Tue Sep 20:22:08 CST 2014Thread finish Tue Sep 20:22:08 CST 2014Thread finish Tue Sep 16 20:22:08 CST
Because all the threads are common to one property, one thread has changed his value, resulting in the subsequent invocation of the value of a variable, we
The expectation is that each thread has its own property value, and everyone is independent, so what to do.
Package Com.bird.concursey;import Java.util.date;import Java.util.concurrent.timeunit;public class SafeTask Implements Runnable {private static threadlocal<date> StartDate = new threadlocal<date> () {protected Date ini Tialvalue () {return new Date ();};}; @Overridepublic void Run () {System.out.println ("Start thread" + Thread.CurrentThread (). GetId () + "" + Startdate.get ()); t ry {TimeUnit.SECONDS.sleep ((int) math.rint (Math.random ())),} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println ("Thread Finish" + Thread.CurrentThread (). GetId () + "" + Startdate.get ());} public static void Main (string[] args) {safetask task = new Safetask (), for (int i = 0; i <; i++) {Thread thread = NE W Thread (Task); Thread.Start (); try {TimeUnit.SECONDS.sleep (2);} catch (Interruptedexception e) {e.printstacktrace ()}}}}
This is the correct code, mainly to join the Threadlocal class, the main purpose is to ensure that every time the thread is opened
Will call his InitValue method to pay an initial value to this property, and then each thread will maintain it itself, each thread is independent,
We can get the corresponding value by the Get method, of course he also provided the set method to change his value, or remove and so on, anyway now is the line Shuo
All of it.
Introduction to Java Multi-thread--threadlocal