Multithreaded Programming Learning (THREAD-SOECIFIC) storage pattern

Source: Internet
Author: User

thread-specific-storage[Thread Safety box]

One: Participants in the thread-specific storage
---> Logging thread (clientthread)
---> Responsible for getting different thread logging (log)
---> Class (Tslog) responsible for writing logs to a file



II: thread-specific storage mode when to use
---> When each thread must have its own unique information, you can put that information in the thread safe threadlocal


Three: thread-specific Storage thinking
---> Place line thread where information is available
(1) out-of-Thread---> Thread safe deposit box threadlocal
(2) Line range---thread body local variables

---> Multithreading common data with shared mutex
---> Shared mutexes degrade performance, so try to narrow the scope of the shared mutex as much as possible
---> Threading performance is the implementation of thread code

Four step instructions
--->

Behavior class for logging

1  PackageCom.yeepay.sxf.thread11;2 3 ImportJava.io.FileWriter;4 ImportJava.io.PrintWriter;5 6 /**7 * Log Class8  * @authorSXF9  *Ten  */ One  Public classTslog { A     //Write Log Object -     PrivatePrintWriter printwriter=NULL; -      the     //Constructors -      PublicTslog (String fileName) { -         Try { -Printwriter=NewPrintWriter (NewFileWriter (FileName)); +}Catch(Exception e) { -             //TODO auto-generated Catch block + e.printstacktrace (); A         } at     } -      -     //Add a log -      Public voidaddlogstr (String logstr) { - Printwriter.print (LOGSTR); -     } in      -     //turn off the output stream to      Public voidCloselog () { + printwriter.close (); -     } the}
View Code

Objects that represent logging for different threads

1  PackageCom.yeepay.sxf.thread11;2 3 4 /**5 * Different thread distribution of different log instances6  * @authorSXF7  *8  */9  Public classLog {Ten     //the thread's collection of safe boxes One     Private Static FinalThreadLocal tslongconteint=NewThreadLocal (); A  -      Public Static voidprintlogstr (String logstr) { -Tslog log=Gettslog (); the log.addlogstr (LOGSTR); -     } -     //gets the current thread's safe deposit box -      Public StaticTslog Gettslog () { +         //take the Tslog from the thread vault as the front-line. -Tslog lg=(Tslog) tslongconteint.get (); +         //If it does not exist, create a new Tslog A         if(lg==NULL){ atlg=NewTslog ("/usr/war/" +thread.currentthread (). GetName () + "-log.txt"); - Tslongconteint.set (LG); -         } -         returnLG; -     } -     //Close the stream of the log object in      Public Static voidClosetslog () { - Gettslog (). Closelog (); to     } +}
View Code

Thread class for logging

1  PackageCom.yeepay.sxf.thread11;2 /**3 * Log thread is logged4  * @authorSXF5  *6  */7  Public classClientthreaadImplementsrunnable{8     9      PublicClientthreaad () {Ten  One     } A      -      - @Override the      Public voidrun () { -          for(inti = 0; I <10; i++) { -Log.printlogstr (Thread.CurrentThread (). GetName () +i); -System.out.println ("Clientthreaad.run () ==>" +thread.currentthread (). GetName () +i); +         } - Log.closetslog (); +     } A      at  -}
View Code

Test class

1  PackageCom.yeepay.sxf.thread11;2 /**3 * Test Class4  * @authorSXF5  *6  */7  Public classTest {8     9     Ten      Public Static voidMain (string[] args) { One         //Open three threads, generate 3 files, three threads record their own logs A         NewThread (NewClientthreaad ()). Start (); -         NewThread (NewClientthreaad ()). Start (); -         NewThread (NewClientthreaad ()). Start (); the     } -  -}
View Code

Multithreaded Programming Learning (THREAD-SOECIFIC) storage pattern

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.