多线程程序设计学习(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(被警戒的对象) --->该模式的角色:模拟修改警戒对象的线程, ...
随机推荐
- C#网络编程(1)
1.Purpose 1.什么是网络编程 2.TCP/IP协议 3.什么是套接字 4.多线程深入理解 二.Basic Concept 1.网络编程:主要实现进程(线程)相互之间的通信和基本的网络应用原理 ...
- oracle行转列、列转行
一.行转列 需要将如下格式 转换为: 这就是最常见的行转列,主要原理是利用decode函数.聚集函数(sum),结合group by分组实现的 create table test( id varcha ...
- 五、mysql存储引擎
show variable like 'table_type'; 显示系统默认存储引擎 show engine\G 显示系统支持存储殷勤 =============================== ...
- 判断webpart类型 How can I tell what type a web part is?
using(new SPSite("http://mysite/myweb").OpenWeb()){ //give relative path of the webpartpag ...
- Sharepoint delegate control
<?xml version="1.0" encoding="utf-8"?> <Elements xmlns="http://sch ...
- csu 1303 Decimal (数论题)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1303 1303: Decimal Time Limit: 1 Sec Memory Limit: ...
- mybatis include标签
使用mybatis 的include标签达到SQL片段达到代码复用的目的 示例: xml文件 <sql id="paysql"> payid,p.oid,p.bdate ...
- C++ static、const和static const 以及它们的初始化
转自C++ static.const和static const 以及它们的初始化 const定义的常量在超出其作用域之后其空间会被释放,而static定义的静态常量在函数执行后不会释放其存储空间. s ...
- 移动web开发入门级
http://www.infoq.com/cn/articles/development-of-the-mobile-web-deep-concept/
- Docker 监控实战
如今,越来越多的公司开始使用 Docker 了,现在来给大家看几组数据: 2 / 3 的公司在尝试了 Docker 后最终使用了它 也就是说 Docker 的转化率达到了 67%,而转化市场也控制在 ...