The use of the Java Threadlocal class

Source: Internet
Author: User

Java's use of the Threadlocal class, Threadlocal is a class that supports generics and is used in multi-threading to prevent concurrency conflicts.

For example, the following example is used to increase the thread by 1, but does not conflict with each other

Package Com.test.threadlocal;import Java.util.concurrent.executorservice;import java.util.concurrent.Executors; Class Local{private static threadlocal<integer> num=new threadlocal<integer> () {protected synchronized Integer InitialValue () {return 1;}}; public static void Add () {Num.set (Num.get () +1);} public static int Get () {return num.get ();}} Class Selfthread extends Thread{public void Run () {for (int i=0;i<1000;i++) {System.out.println (This.getname () + "  "+local.get ()); This.yield (); Local.add ();}}} public class Mythreadlocal {/** * @param args */public static void main (string[] args) {//TODO auto-generated method stub Executorservice Exec=executors.newcachedthreadpool (); for (int i=0;i<5;i++) {Exec.execute (New SelfThread ());} Exec.shutdown ();}}


Output results

Thread-3 973
Thread-1 808
Thread-3 974
Thread-1 809
Thread-3 975
THREAD-1 810
Thread-3 976
Thread-1 811
Thread-3 977
Thread-1 812
Thread-3 978
Thread-1 813
Thread-3 979
Thread-1 814
Thread-3 980
Thread-1 815
Thread-3 981
Thread-1 816
Thread-3 982
Thread-1 817
Thread-3 983
Thread-1 818
Thread-3 984
Thread-1 819
Thread-3 985
Thread-1 820
Thread-3 986
Thread-1 821
Thread-3 987
Thread-1 822
Thread-3 988
Thread-1 823
Thread-3 989
Thread-1 824
Thread-3 990
Thread-1 825
Thread-3 991
Thread-1 826
Thread-3 992
Thread-1 827
Thread-3 993
Thread-1 828
Thread-3 994
Thread-1 829
Thread-3 995
Thread-1 830
Thread-3 996
Thread-1 831
Thread-3 997
Thread-1 832
Thread-3 998
Thread-1 833
Thread-3 999
Thread-1 834
Thread-3 1000
Thread-1 835
Thread-1 836
Thread-1 837
Thread-1 838
Thread-1 839
Thread-1 840
Thread-1 841
Thread-1 842
Thread-1 843
Thread-1 844
Thread-1 845
Thread-1 846
Thread-1 847
Thread-1 848
THREAD-1 849
Thread-1 850
Thread-1 851
Thread-1 852
Thread-1 853
Thread-1 854
THREAD-1 855
Thread-1 856
Thread-1 857
Thread-1 858
Thread-1 859
Thread-1 860
Thread-1 861
Thread-1 862
THREAD-1 863
Thread-1 864
Thread-1 865
Thread-1 866
Thread-1 867
Thread-1 868
Thread-1 869
Thread-1 870
Thread-1 871
Thread-1 872
Thread-1 873
Thread-1 874



We found that the threads switched between each other, but each thread kept the order by 1, stating that there was no interaction between the threads.


The use of the Java Threadlocal class

Related Article

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.