多线程程序设计学习(12)Thread-soecific storage pattern
Thread-Specific-Storage[线程保管箱]
一:Thread-Specific Storage的参与者
--->记录日志的线程(ClientThread)
--->负责获取不同线程记录日志(Log)
--->负责将日志写入文件的类(TsLog)
二:Thread-Specific Storage模式什么时候使用
--->当每个线程必须有自己的独有信息时,可以将该信息放入线程保管箱ThreadLocal
三:Thread-Specific Storage思考
--->放置线程特有信息的地方
(1)线程外--->线程的保管箱ThreadLocal
(2) 线程内-->线程体的局部变量
--->多线程处理共有数据,存在共享互斥
--->共享互斥会降低性能,所以要尽量将共享互斥的范围缩小
--->线程的性能在于线程代码的实现
四进阶说明
--->
记录日志的行为类
package com.yeepay.sxf.thread11; import java.io.FileWriter;
import java.io.PrintWriter; /**
* 日志类
* @author sxf
*
*/
public class TsLog {
//写日志对象
private PrintWriter printWriter=null; //构造器
public TsLog(String fileName){
try {
printWriter=new PrintWriter(new FileWriter(fileName));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} //添加一条日志
public void addLogStr(String logstr){
printWriter.print(logstr);
} //关闭输出流
public void closeLog(){
printWriter.close();
}
}
代理不同线程的记录日志的对象
package com.yeepay.sxf.thread11; /**
* 不同线程分发不同的日志实例
* @author sxf
*
*/
public class Log {
//线程的保管箱集合
private static final ThreadLocal tsLongConteint=new ThreadLocal(); public static void printLogStr(String logStr){
TsLog log=getTslog();
log.addLogStr(logStr);
}
//获取当前线程的保管箱
public static TsLog getTslog(){
//从线程保管箱中拿去当前线程的TsLog
TsLog lg=(TsLog) tsLongConteint.get();
//如果不存在,创建新的TsLog
if(lg==null){
lg=new TsLog("/usr/war/"+Thread.currentThread().getName()+"-log.txt");
tsLongConteint.set(lg);
}
return lg;
}
//关闭log对象的流
public static void closeTsLog(){
getTslog().closeLog();
}
}
记录日志的线程类
package com.yeepay.sxf.thread11;
/**
* 记录日志的线程
* @author sxf
*
*/
public class ClientThreaad implements Runnable{ public ClientThreaad() { } @Override
public void run() {
for (int i = 0; i <10; i++) {
Log.printLogStr(Thread.currentThread().getName()+i);
System.out.println("ClientThreaad.run()==>"+Thread.currentThread().getName()+i);
}
Log.closeTsLog();
} }
测试类
package com.yeepay.sxf.thread11;
/**
* 测试类
* @author sxf
*
*/
public class Test { public static void main(String[] args) {
//开启三个线程,产生3个文件,三个线程自己记录自己的日志
new Thread(new ClientThreaad()).start();
new Thread(new ClientThreaad()).start();
new Thread(new ClientThreaad()).start();
} }
多线程程序设计学习(12)Thread-soecific storage pattern的更多相关文章
- 多线程程序设计学习(3)immutable pattern模式
Immutable pattern[坚不可摧模式] 一:immutable pattern的参与者--->immutable(不变的)参与者 1.1:immutable参与者是一个 ...
- 多线程程序设计学习(10)Future pattern
Future pattern[订单取货模式] 一:Future pattern的参与者--->Client(客户需求)--->Host(蛋糕门店)--->Data(票据和蛋糕的接口) ...
- 多线程程序设计学习(11)Two-phapse-Termination pattern
Two-phapse-Termination[A终止B线程] 一:Two-phapse-Termination的参与者--->A线程--->B线程 二:Two-phapse-Termina ...
- 多线程程序设计学习(9)worker pattern模式
Worker pattern[工作模式]一:Worker pattern的参与者--->Client(委托人线程)--->Channel(通道,里边有,存放请求的队列)--->Req ...
- 多线程程序设计学习(2)之single threaded execution pattern
Single Threaded Execution Pattern[独木桥模式] 一:single threaded execution pattern的参与者--->SharedResourc ...
- 多线程程序设计学习(7)read-write lock pattern
Read-Write Lock Pattern[读写]一:Read-Write Lock Pattern的参与者--->读写锁--->数据(共享资源)--->读线程--->写线 ...
- 多线程程序设计学习(13)Active Object pattern
Active Object[接收异步消息的对象] 一:Active Object的参与者--->客户端线程(发起某种操作请求处理)--->代理角色(工头)--->实际执行者(工人)- ...
- 多线程程序设计学习(6)Producer-Consumer模式
Producer-Consumer[生产消费者模式]一:Producer-Consumer pattern的参与者--->产品(蛋糕)--->通道(传递蛋糕的桌子)--->生产者线程 ...
- 多线程程序设计学习(5)balking模式和timed模式
Balking[返回模式]timed[超时模式]一:balking pattern的参与者--->GuardedObject(被警戒的对象) --->该模式的角色:模拟修改警戒对象的线程, ...
随机推荐
- asp.net项目中通过Web.config配置文件及文件夹的访问权限---forms
十分全面的forms验证配置: http://blog.csdn.net/qingyun1029/article/details/6184723
- StatusStrip状态栏控件
1.ToolStripStatusLabel statusstrip1.Items[].Text="日期"+DateTime.Now.ToString(); Thread p = ...
- Matlab生成动态链接库供C#调用
1.首先在Matlab中编写一个或几个.m文件 2.然后在命令空间中输入命令:deploytool 3.修改工程名称,修改需要生成文件后缀 4.添加类,添加文件,然后点击生成.
- make问题:make[1] entering directory
执行make distclean命令.
- sybase convert 函数
1.从string到int的转换 convert(int,@string) select convert( int , '15') 2. 从int 到 decimal 的转换 convert(deci ...
- sql表连接left join,right join,inner join三者之间的区别
sql表连接left join,right join,inner join区别 left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 (以左表数据为基准,不足补为NULL) ...
- 2016 系统设计第一期 (档案一)MVC 相关控件整理
说明:前者是MVC,后者是boostrap 1.form 表单 @using (Html.BeginForm("Create", "User", FormMet ...
- 淘宝自己的前端框架KISSY(类似jquery) - 简易指南
KISSY 是由阿里集团前端工程师们发起创建的一个开源 JS 框架. 具备模块化.高扩展性.组件齐全,接口一致.自主开发.适合多种应用场景等特性. 在以下方面具有一定优势: A.拥有大量的中文文档: ...
- c# DirectoryInfo类 详解
DirectoryInfo类和Directory类之间的关系与FileInfo类和File类之间的关系十分类似.下面介绍一下DirectoryInfo类的常用属性. DirectoryInfo类的常用 ...
- MS Translator
在看白老师的书的时候看到的,现在MS已经开始转向服务,真对不同行业具有不同的服务,有些免费的,还是十分值得我们借用的,毕竟是大公司出来的产品,都会保证SLA的. 不多说了,直接上地址: https:/ ...