Threadlocal and inheritablethreadlocal

Source: Internet
Author: User
Threadlocal
A local variable can be implemented in the thread and can be accessed anywhere in the thread. It can reduce the passing of the parameter package JP. co. realseed;
Public class threadlocaltest ...{
Private Static threadlocal tl_1 = new threadlocal ()...{
// Return the thread name, variable 1
Protected object initialvalue ()...{
Return "thread name 1:" + thread. currentthread (). getname ();
}
};
Private Static threadlocal tl_2 = new threadlocal ()...{
// Return the thread name, variable 2
Protected object initialvalue ()...{
Return "thread name 2:" + thread. currentthread (). getname ();
}
};
Public static void main (string [] ARGs )...{
For (INT I = 0; I <10; I ++ )...{
New thread (New runnable ()...{
Public void run ()...{
New threadlocaltest (). PRT ();
}
}). Start ();
}
}
// Print the thread name
Public void PRT ()...{
System. Out. println (tl_1.get ());
System. Out. println (tl_2.get ());
}
}

Inheritablethreadlocal
The instance can be shared between the child thread and the parent thread, or to reduce the passing of the parameter package JP. co. realseed;
Public class inheritablethreadlocaltest ...{
Private Static inheritablethreadlocal ITL = new inheritablethreadlocal ()...{
Protected object initialvalue ()...{
Return new stringbuffer ("hello ");
}
};
Public static void main (string [] ARGs )...{
System. Out. println (thread. currentthread (). getname () + ":" + ITL. Get ());
New thread (New runnable ()...{
Public void run ()...{
System. Out. println (thread. currentthread (). getname () + ":" + ITL. Get ());
New thread (New runnable ()...{
Public void run ()...{
System. Out. println (thread. currentthread (). getname () + ":" + ITL. Get ());
(Stringbuffer) ITL. Get (). append (", wqf ");
System. Out. println (thread. currentthread (). getname () + ":" + ITL. Get ());
}
}). Start ();
Try ...{
Thread. Sleep (1000 );
} Catch (interruptedexception ex )...{
Ex. printstacktrace ();
}
System. Out. println (thread. currentthread (). getname () + ":" + ITL. Get ());
}). Start ();
Try ...{
Thread. Sleep (1000 );
} Catch (interruptedexception ex )...{
Ex. printstacktrace ();
}
System. Out. println (thread. currentthread (). getname () + ":" + ITL. Get ());
}

}

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.