008-ThreadLocal
一、基本用法
ThreadLocal是一个容器,用于存放线程的局部变量。如果ThreadLocalVariable(线程局部变量)更加好理解。
在Jdk 1.2 java.lang.ThreadLocal开始使用,他是为解决多线程并发设计的.
示例序列号生成,保证每个线程生成唯一序列号
编写Sequence接口
package com.lhx.chapter4.threadlocaltest;
public interface Sequence {
int getNumber();
}
编写线程类
package com.lhx.chapter4.threadlocaltest;
public class ClientThread extends Thread {
private Sequence sequence;
public ClientThread(Sequence sequence){
this.sequence=sequence;
}
@Override
public void run() {
for(int i=0;i<3;i++){
System.out.println(Thread.currentThread().getName()+"=>"+sequence.getNumber());
}
}
}
实现方式一
package com.lhx.chapter4.threadlocaltest;
public class SequenceA implements Sequence {
private static int number = 0;
@Override
public int getNumber() {
number=number+1;
return number;
}
public static void main(String[] args) {
Sequence sequence = new SequenceA();
ClientThread clientThread1 = new ClientThread(sequence);
ClientThread clientThread2 = new ClientThread(sequence);
ClientThread clientThread3 = new ClientThread(sequence);
clientThread1.start();
clientThread2.start();
clientThread3.start();
}
}
Thread-0=>1
Thread-1=>2
Thread-0=>3
Thread-1=>4
Thread-0=>5
Thread-1=>6
Thread-2=>7
Thread-2=>8
Thread-2=>9
由输出可以线程间共享一个Static变量,无法保证线程安全。
示例2,
package com.lhx.chapter4.threadlocaltest;
public class SequenceB implements Sequence {
private static ThreadLocal<Integer> numberContainer = new ThreadLocal<Integer>(){
@Override
protected Integer initialValue() {
return 0;
}
};
@Override
public int getNumber() {
numberContainer.set(numberContainer.get()+1);
return numberContainer.get();
}
public static void main(String[] args) {
Sequence sequence = new SequenceB();
ClientThread clientThread1 = new ClientThread(sequence);
ClientThread clientThread2 = new ClientThread(sequence);
ClientThread clientThread3 = new ClientThread(sequence);
clientThread1.start();
clientThread2.start();
clientThread3.start();
}
}
输出值:
Thread-0=>1
Thread-0=>2
Thread-0=>3
Thread-1=>1
Thread-1=>2
Thread-1=>3
Thread-2=>1
Thread-2=>2
Thread-2=>3
可以看出每个线程变量独立开来,也就是说ThreadLocal为每个线程提供了一个副本。
ThreadLocal的API,
public void set(T value)将值放入线程局部变量中
public T get() 从线程局部变量中获取值
public void remove()从线程局部变量中移除
protected T initialValue()返回线程局部变量的初始值(默认为null),必须默认实现。
自定义实现ThreadLocal
package com.lhx.chapter4.threadlocaltest; import java.util.Collections;
import java.util.HashMap;
import java.util.Map; public class MyThreadLocal<T> {
private Map<Thread,T> container=Collections.synchronizedMap(new HashMap<Thread,T>());
public void set(T value){
container.put(Thread.currentThread(),value);
}
public T get(){
Thread thread = Thread.currentThread();
T value = container.get(thread);
if(value==null&&!container.containsKey(thread)){
value=initialValue();
container.put(thread,value);
}
return value;
}
public void remove(){
container.remove(Thread.currentThread());
} protected T initialValue(){
return null;
} }
用法一致
二、使用案例
通过ThreadLocal存放Jdbc Connection,以达到事务控制能力。
示例,修改价格,记录日志
update product set price=? where id=?
insert into log(crerated,description)values(?,?)
编写工具类DBUtil
package com.lhx.test.dbutil1; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil {
private static final String driver = "com.mysql.jdbc.Driver";
private static final String url = "jdbc:mysql://localhost:3306/demo";
private static final String username = "root";
private static final String password = "root"; private static Connection conn = null; public static Connection getConnection() {
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
} public static void colseConnection() {
try {
if(conn!=null){
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
编写ProductService
package com.lhx.test.dbutil1;
public interface ProductService {
void updateProductPrice(long productId,int price);
}
编写ProductService实现类编写ProductServiceImpl
package com.lhx.test.dbutil1; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
import java.util.Date; public class ProductServiceImpl implements ProductService { private static final String Update_Product_sql = "update product set price=? where id=?";
private static final String Insert_Log_sql = "insert into log(crerated,description)values(?,?)"; @Override
public void updateProductPrice(long productId, int price) {
try {
Connection connection = DBUtil.getConnection();
connection.setAutoCommit(false);
updateProduct(connection,Update_Product_sql,productId,price);
insertLog(connection,Update_Product_sql,"Create Product");
connection.commit();
} catch (Exception e) {
e.printStackTrace();
}finally {
DBUtil.colseConnection();
}
}
private void updateProduct(Connection connection,String sql,long productId,int price) throws Exception{
PreparedStatement pstmt = connection.prepareStatement(sql);
pstmt.setInt(1,price);
pstmt.setLong(2,productId);
int rows = pstmt.executeUpdate();
if(rows!=0){
System.out.println("update product success");
}
}
private void insertLog(Connection connection,String sql,String msg) throws Exception{
PreparedStatement pstmt = connection.prepareStatement(sql);
pstmt.setString(1,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.sss").format(new Date()));
pstmt.setString(2,msg);
int rows = pstmt.executeUpdate();
if(rows!=0){
System.out.println("insert log success");
}
}
}
个人测试
public static void main(String[] args) {
ProductService productService = new ProductServiceImpl();
productService.updateProductPrice(1,3000);
}
并发测试
package com.lhx.test.dbutil1;
import com.lhx.test.dbutil1.ProductService;
public class ClientThread extends Thread {
private ProductService productService;
public ClientThread(ProductService productService) {
this.productService = productService;
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
productService.updateProductPrice(1, 3000);
}
}
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
ProductService productService = new ProductServiceImpl();
ClientThread clientThread = new ClientThread(productService);
clientThread.start();
}
}
使用ThreadLocal编写工具类
package com.lhx.test.dbutil2; import java.sql.Connection;
import java.sql.DriverManager; public class DBUtil {
private static final String driver = "com.mysql.jdbc.Driver";
private static final String url = "jdbc:mysql://localhost:3306/demo";
private static final String username = "root";
private static final String password = "root"; private static ThreadLocal<Connection> conContainer = new ThreadLocal<>(); public static Connection getConnection() {
Connection conn = conContainer.get();
try {
if (conn == null) {
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
conContainer.set(conn);
}
return conn;
} public static void colseConnection() {
Connection conn = conContainer.get();
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
conContainer.remove();
}
}
}
008-ThreadLocal的更多相关文章
- ThreadLocal简单理解
在java开源项目的代码中看到一个类里ThreadLocal的属性: private static ThreadLocal<Boolean> clientMode = new Thread ...
- Android线程管理之ThreadLocal理解及应用场景
前言: 最近在学习总结Android的动画效果,当学到Android属性动画的时候大致看了下源代码,里面的AnimationHandler存取使用了ThreadLocal,激起了我很大的好奇心以及兴趣 ...
- Threadlocal使用Case
Threadlocal能够为每个线程分配一份单独的副本,使的线程与线程之间能够独立的访问各自副本.Threadlocal 内部维护一个Map,key为线程的名字,value为对应操作的副本. /** ...
- 多线程映射工具——ThreadLocal
ThreadLocal相当于一个Map<Thread, T>,各线程使用自己的线程对象Thread.currentThread()作为键存取数据,但ThreadLocal实际上是一个包装了 ...
- ThreadLocal 工作原理、部分源码分析
1.大概去哪里看 ThreadLocal 其根本实现方法,是在Thread里面,有一个ThreadLocal.ThreadLocalMap属性 ThreadLocal.ThreadLocalMap t ...
- ThreadLocal<T>的是否有设计问题
一.吐槽 ThreadLocal<T>明显是.NET从JAVA中来的一个概念,但是这种设计是否出现了问题. 很明显,在JAVA中threadLocal直接是Thread的成员,当然随着th ...
- 理解ThreadLocal —— 一个map的key
作用: 当工作于多线程中的对象使用ThreadLocal维护变量时,threadLocal为每个使用该变量的线程分配一个独立的变量副本. 接口方法: protected T initialValue( ...
- JavaSe:ThreadLocal
JDK中有一个ThreadLocal类,使用很方便,但是却很容易出现问题.究其原因, 就是对ThreadLocal理解不到位.最近项目中,出现了内存泄漏的问题.其中就有同事在使用ThreadLocal ...
- 0041 Java学习笔记-多线程-线程池、ForkJoinPool、ThreadLocal
什么是线程池 创建线程,因为涉及到跟操作系统交互,比较耗费资源.如果要创建大量的线程,而每个线程的生存期又很短,这时候就应该使用线程池了,就像数据库的连接池一样,预先开启一定数量的线程,有任务了就将任 ...
- ThreadLocal 源码剖析
ThreadLocal是Java语言提供的用于支持线程局部变量的类.所谓的线程局部变量,就是仅仅只能被本线程访问,不能在线程之间进行共享访问的变量(每个线程一个拷贝).在各个Java web的各种框架 ...
随机推荐
- Atitit.协议的转换smb2http 原理
Atitit.协议的转换smb2http 原理 1. 协议的转换原理 stream的转换.. 2. 常用协议转换的情形 android通过SMB访问局域网共享文件 作者:: ★(attilax)&g ...
- Java中有几种类型的流?以及常见的实现类都有哪些?
Java中有几种类型的流?以及常见的实现类都有哪些? 首先应该从两个角度来看: 从输入输出方面来讲: Java中有输入流和输出流 从流的编码方式上来讲: Java中有字节流和字符流 ...
- 使用spring-boot-maven-plugin打包
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> ...
- 做自己生活的导演:华为CameraKit为你加持大师光环
今年最流行的娱乐方式,无疑是短视频,抖音等短视频平台,越来越多的消费者沉浸其中.除了看别人拍的短视频用以丰富生活乐趣之外,也有不少跃跃欲试的消费者加入到短视频拍摄的行列中.随着拍摄者的增加,对拍摄设备 ...
- 【ASP.NET】——AdRotator控件
AdRotator控件即广告控件. 广告,是站点不可缺少的一部分.也是站点获取收益的最主要途径,但最初制作广告非常麻烦.asp.net就将该方法封装成了一个控件.为我们节省了非常多时间.这也是ASP. ...
- Oracle基础学习登陆SQLPLUS(一)
SQLPLUS是ORACLE公司开发的非常简洁的管理工具,SQLPLUS是最好的,最核心的ORACLE管理工具.SQLPLUS简洁而高效,舍弃浮华,反璞归真.使用sqlplus,进入sqlplus并进 ...
- WPF-数据转换
有时我们展现的数据,需要进行转换,比如如果一个学生的成绩过了60,我们显示一个Pass的图片. XAML: <Window x:Class="DeepXAML.MainWindow&q ...
- Laravel5.1 响应
上篇笔记刚刚记录完请求 这节就来说说响应,一般来说啊 一个请求对应一个响应,用户都请求咱了 咱必须做一些逻辑后给人家反馈是不是,这就是响应. 1 基本的响应 我们前几篇笔记已经用过很多响应了,其中包括 ...
- react 近期
ECMAScript 6 入门:http://es6.ruanyifeng.com/#docs/destructuring#%E6%95%B0%E7%BB%84%E7%9A%84%E8%A7%A3%E ...
- 绝对定位position: absolute;
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...