享元模式&ThreadLocal结合实例

 享元模式

定义:运用共享技术来有効地支持大量细粒度对象的复用。它通过共享已经存在的对象来大幅度减少需要创建的对象数量、避免大量
相似类的开销,从而提高系统资源的利用率。

 

public abstract class Log {

	/*
	 * 内部状态(类似于数据库主键,不会发生频繁变更)
	 * 同一线程中,记录日志时,用户名字、用户性别、用户角色
	 */
	//用户名字
	private String username;
	//用户性别
	private String sex;
	//用户角色
	private String role;

	/*
	 * 外部状态(类似于不同数据,频繁迭代,快速递增)
	 * 同一个线程中,记录日志时,每次访问的不同方法和参数不一样
	 */
	//操作方法
	private String methodName;
	//信息
	private String message;

	/*
	 * 业务操作,补充和完善methodName,args参数
	 */
	public abstract void supplementLogContent(String... args);

	public String getMethodName() {
		return methodName;
	}

	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Log(String username, String sex, String role) {
		this.username = username;
		this.sex = sex;
		this.role = role;
	}

	@Override
	public String toString() {
		return "Log{" + "username='" + username + '\'' + ", sex='" + sex + '\'' + ", role='" + role + '\'' + ", methodName='" + methodName + '\'' + ", message='" + message + '\'' + '}';
	}
}
public class SpecificOperationLog extends Log{

	/****
	 * 业务逻辑,完善不同方法的日志记录
	 * @param args 长度为2,第1个是方法名字,第2个是方日志信息
	 */
	@Override
	public void supplementLogContent(String... args) {
		super.setMethodName(args[0]);
		super.setMessage(args[1]);
	}

	public SpecificOperationLog(String username, String sex, String role) {
		super(username, sex, role);
	}
}
public class ThreadUserLogFactory {

	/*
	* ThreadLocal的作用
	* 线程私有,线程安全
	* 允许一个线程以及该线程创建所有线程可以访问已经保存的值
	 *
	 * */

	//存储线程对应的用户名日志信息
	private static ThreadLocal<Log> userRecode = new ThreadLocal<>();

	/****
	 * 添加用户信息记录
	 */
	public void add(Log log) {
		userRecode.set(log);
	}

	/***
	 * 记录方法名和参数
	 * @param args
	 */
	public String reload(String... args) {
		//获取对象
		Log logComponent = userRecode.get();
		//设置数据
		logComponent.supplementLogContent(args);
		System.out.println("logComponent:"+logComponent.toString());
		return logComponent.toString();
	}

	/****
	 * 获取LogComponent
	 */
	public Log get(){
		return userRecode.get();
	}
	/****
	 * 移除
	 */
	public void remove(){
		userRecode.remove();
	}
}
public class OperationDemo {

	public static void main(String[] args) {
		/*
		* 模拟controller访问,前端传过来的用户信息
		* 第一个ThreadLocal变量线程保存一个值
		* */
		ThreadUserLogFactory userLogFactory=new ThreadUserLogFactory();
		Log log = new SpecificOperationLog("Tom", "男", "cat");
		userLogFactory.add(log);
		/*
		*  模拟AOP切面调用方法,记录日志
		*  第二个ThreadLocal变量线程在第一个变量线程ThreadLocal基础上继续操作,
		*  存放对应的值
		* */
		ThreadUserLogFactory userLogFactory2=new ThreadUserLogFactory();
		userLogFactory2.reload("com.hikktn.flyweight.service.impl.addUser","name:Tom","sex:男","role:cat");

	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
ThreadLocal是Java中的一个线程局部变量,它可以为每个线程提供独立的变量副本,使得每个线程都可以独立地修改自己所拥有的变量副本,而不会影响其他线程的副本。DBUtils是一个开源的数据库操作工具类库,它封装了JDBC的操作细节,简化了数据库操作的代码。 当ThreadLocal与DBUtils结合使用时,可以实现在多线程环境下,每个线程都拥有独立的数据库连接,避免了线程间的资源竞争和并发访问的问题。 下面是一个ThreadLocal与DBUtils结合使用的示例: ```java import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; public class DBUtilsExample { private static ThreadLocal<Connection> connectionHolder = new ThreadLocal<>(); public static void main(String[] args) { // 初始化数据源 DataSource dataSource = ...; // 创建查询器 QueryRunner queryRunner = new QueryRunner(dataSource); try { // 获取数据库连接 Connection connection = dataSource.getConnection(); // 将连接保存到ThreadLocal中 connectionHolder.set(connection); // 在当前线程中执行数据库操作 User user = queryRunner.query("SELECT * FROM user WHERE id = ?", new BeanHandler<>(User.class), 1); System.out.println(user); } catch (SQLException e) { e.printStackTrace(); } finally { // 关闭数据库连接 Connection connection = connectionHolder.get(); if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } // 清除ThreadLocal中的连接 connectionHolder.remove(); } } } ``` 在上述示例中,我们通过ThreadLocal将数据库连接保存在每个线程的独立副本中。这样,在每个线程中执行数据库操作时,都可以从ThreadLocal中获取到独立的数据库连接,而不会受到其他线程的影响。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hikktn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值