多线程程序设计学习(12)Thread-soecific storage pattern

Thread-Specific-Storage[线程保管箱]

一:Thread-Specific Storage的参与者
--->记录日志的线程(ClientThread)
--->负责获取不同线程记录日志(Log)
--->负责将日志写入文件的类(TsLog)

       

二:Thread-Specific Storage模式什么时候使用
--->当每个线程必须有自己的独有信息时,可以将该信息放入线程保管箱ThreadLocal


三:Thread-Specific Storage思考
--->放置线程特有信息的地方
        (1)线程外--->线程的保管箱ThreadLocal
           (2)    线程内-->线程体的局部变量

--->多线程处理共有数据,存在共享互斥
--->共享互斥会降低性能,所以要尽量将共享互斥的范围缩小
--->线程的性能在于线程代码的实现

四进阶说明        
--->

记录日志的行为类

 1 package com.yeepay.sxf.thread11;
 2 
 3 import java.io.FileWriter;
 4 import java.io.PrintWriter;
 5 
 6 /**
 7  * 日志类
 8  * @author sxf
 9  *
10  */
11 public class TsLog {
12     //写日志对象
13     private PrintWriter printWriter=null;
14     
15     //构造器
16     public TsLog(String fileName){
17         try {
18             printWriter=new PrintWriter(new FileWriter(fileName));
19         } catch (Exception e) {
20             // TODO Auto-generated catch block
21             e.printStackTrace();
22         }
23     }
24     
25     //添加一条日志
26     public void addLogStr(String logstr){
27         printWriter.print(logstr);
28     }
29     
30     //关闭输出流
31     public void closeLog(){
32         printWriter.close();
33     }
34 }
View Code

代理不同线程的记录日志的对象

 1 package com.yeepay.sxf.thread11;
 2 
 3 
 4 /**
 5  * 不同线程分发不同的日志实例
 6  * @author sxf
 7  *
 8  */
 9 public class Log {
10     //线程的保管箱集合
11     private static final ThreadLocal tsLongConteint=new ThreadLocal();
12 
13     public static void printLogStr(String logStr){
14         TsLog log=getTslog();
15         log.addLogStr(logStr);
16     }
17     //获取当前线程的保管箱
18     public static TsLog getTslog(){
19         //从线程保管箱中拿去当前线程的TsLog
20         TsLog lg=(TsLog) tsLongConteint.get();
21         //如果不存在,创建新的TsLog
22         if(lg==null){
23             lg=new TsLog("/usr/war/"+Thread.currentThread().getName()+"-log.txt");
24             tsLongConteint.set(lg);
25         }
26         return lg;
27     }
28     //关闭log对象的流
29     public static void  closeTsLog(){
30         getTslog().closeLog();
31     }
32 }
View Code

记录日志的线程类

 1 package com.yeepay.sxf.thread11;
 2 /**
 3  * 记录日志的线程
 4  * @author sxf
 5  *
 6  */
 7 public class ClientThreaad  implements Runnable{
 8     
 9     public ClientThreaad() {
10 
11     }
12     
13     
14     @Override
15     public void run() {
16         for (int i = 0; i <10; i++) {
17             Log.printLogStr(Thread.currentThread().getName()+i);
18             System.out.println("ClientThreaad.run()==>"+Thread.currentThread().getName()+i);
19         }
20         Log.closeTsLog();
21     }
22     
23 
24 }
View Code

测试类

 1 package com.yeepay.sxf.thread11;
 2 /**
 3  * 测试类
 4  * @author sxf
 5  *
 6  */
 7 public class Test {
 8     
 9     
10     public static void main(String[] args) {
11         //开启三个线程,产生3个文件,三个线程自己记录自己的日志
12         new Thread(new ClientThreaad()).start();
13         new Thread(new ClientThreaad()).start();
14         new Thread(new ClientThreaad()).start();
15     }
16 
17 }
View Code
原文地址:https://www.cnblogs.com/shangxiaofei/p/4695629.html