Java theadlocal multithreaded Resource usage

Source: Internet
Author: User

Thread-Handling Resource classes:

Package Org.shefron.fc.thread.threadlocal;import Java.util.hashmap;public class Handlethead extends thread {private HashMap map = Null;public Handlethead (HashMap HashMap) {map = HashMap;} @Overridepublic void Run () {super.run (); Registermanager.getregistermanager (). Startregister (map), if (Registermanager.getregistermanager (). GetRegister ()! = null) {System.out.println ("Oh, yes");}} public void print () {if (Registermanager.getregistermanager (). Getregister () = = null) {System.out.println ("no");}}}


Multithreaded Resource Management classes:

Package Org.shefron.fc.thread.threadlocal;import Java.util.hashmap;public class Registermanager {private threadlocal <HashMap> localregister = new threadlocal


Main method Test class:

Package Org.shefron.fc.thread.threadlocal;import Java.util.hashmap;public class Testthreadlocal {/** * @param args */ public static void Main (string[] args) {HashMap map = new HashMap (); Map.put ("hehe", "haha"); Registermanager.getregistermanager (). Startregister (map); Handlethead handle = null;for (int i=1;i<4;i++) {handle = new Handlethead (new HashMap ());//Sub-thread handle.start (); if ( Handle! = NULL) {//This method is located on the main thread Handle.print ();}} System.out.println (Registermanager.getregistermanager (). Getregister ());}}


Java theadlocal multithreaded Resource usage.

Java theadlocal multithreaded Resource usage

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.