Java Concurrency Learning nine--using local thread variables

Source: Internet
Author: User

This article is a summary of the study of the article on the network, thank you for your selfless sharing.

1 , if you create a class object, implement the Runnable interface, and then multiple Thread object uses the same Runnable object, all threads share the same property. This means that if you change an attribute in a line thread, all threads will be affected by this change. If you want the properties of the brother thread in the program to not be shared,theJava concurrency API provides a clear mechanism called local thread variables.

2,JavaConcurrencyAPIincludeinheritableThreadLocalclass provides the hereditary value of thread-creation threads. If the threadAhas a local thread variable, and then it creates another threadB, then the threadBwill have withAthe same local thread variable value. You can overwriteChildvaluemethod to initialize the value of the local thread variable for the child thread. It receives the local thread variable as a parameter for the thread being paid.


Let's start with a code that doesn't use local thread variables.

Package Chapter;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.printf ("Starting Thread:%s:%s\n", Thread.CurrentThread (). GetId (), startdate); try { TimeUnit.SECONDS.sleep ((int) math.rint (math.random () *10)); catch (Interruptedexception e) {e.printstacktrace ();} System.out.printf ("Thread finished:%s:%s\n", Thread.CurrentThread (). GetId (), StartDate);}}

Package Chapter;import Java.util.concurrent.timeunit;public class Main9 {/** * <p> * </p> * @author Zhangjunsh Uai * @date 2014-8-22 pm 4:30:20 * @param args */public static void main (string[] args) {unsafetask task = new Unsafetask () ; for (int i = 0; i<10;i++) {Thread thread = new Thread (Task); Thread.Start (); try {TimeUnit.SECONDS.sleep (2);} catch (Inte Rruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} /*safetask task = new Safetask (); for (int i = 0; i<10;i++) {Thread thread = new Thread (Task); Thread.Start (); try {Timeunit . Seconds.sleep (2);} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} */}}




Using local thread variables

Package Chapter;import Java.util.date;import Java.util.concurrent.timeunit;public class Safetask implements Runnable{ private static threadlocal<date> StartDate = new threadlocal<date> () {protected Date InitialValue () {return New Date ();}}; @Overridepublic void Run () {System.out.printf ("Starting thread:%s:%s\n", Thread.CurrentThread (). GetId (), Startdate.get ()); try {TimeUnit.SECONDS.sleep (int) math.rint (math.random () *10)} catch (Interruptedexception e) {//TODO Auto-generated catch Blocke.printstacktrace ();}  System.out.printf ("Thread finished:%s:%s\n", Thread.CurrentThread (). GetId (), Startdate.get ());}}

Package Chapter;import Java.util.concurrent.timeunit;public class Main9 {/** * <p> * </p> * @author Zhangjunsh Uai * @date 2014-8-22 pm 4:30:20 * @param args */public static void main (string[] args) {/*unsafetask task = new Unsafetask (); for (int i = 0; i<10;i++) {Thread thread = new Thread (Task), Thread.Start (); try {TimeUnit.SECONDS.sleep (2);} catch (in Terruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} */safetask task = new Safetask (); for (int i = 0; i<10;i++) {Thread thread = new Thread (Task); Thread.Start (); try {Timeunit . Seconds.sleep (2);} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}


Java Concurrency Learning nine--using local thread variables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.