Java TheadLocal multithreading resource usage

Source: Internet
Author: User

Java TheadLocal multithreading resource usage

Thread processing resources:

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 ("Haha, yes") ;}} public void print () {if (RegisterManager. getRegisterManager (). getRegister () = null) {System. out. println ("no ");}}}


Multi-threaded Resource Management:

package org.shefron.fc.thread.threadlocal;import java.util.HashMap;public class RegisterManager {private ThreadLocal
 
   localRegister = new ThreadLocal
  
   ();private static RegisterManager manager = new RegisterManager();private RegisterManager(){}public static RegisterManager getRegisterManager(){return manager;}public void startRegister(HashMap infoMap){this.localRegister.set(infoMap);}public HashMap getRegister(){return this.localRegister.get();}}
  
 


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 in the main thread handle. print () ;}} System. out. println (RegisterManager. getRegisterManager (). getRegister ());}}


Java TheadLocal multithreading 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.