如何使用ThreadLocal存储数据

问题描述 投票:0回答:3

我正在使用 Strust2 和 Hibernate。我必须找出货币汇率(美元到印度卢比)。我需要在多个地方使用此信息。为此,我使用 ThreadLocal 来实现此目的。

public class GetExchangeRate{
private ThreadLocal<Double> threadLocalRate = new ThreadLocal<Double>();
public double getCurrencyRate(UserDet userDet){
    LOG.info("Thread id is ---------------->"+Thread.currentThread().getId());
    Double currencyRate = (Double) threadLocalRate.get();
    if(currencyRate == null){
        LOG.info("Object does not exist");
     ---//my code which is used to find USD --> INR exchange rate
   threadLocalRate.set(currencyRate);    
}
return currencyRate ;
}
}

我需要从 four 不同的方法中调用上述方法。当我从不同的方法调用上述方法时,上面的总代码将被执行。 我的要求只是一次整个方法必须得到执行。剩下的“三次”总方法不应该被执行。应返回存储在 ThreadLocal 对象中的值。 这是我的日志报告,显示上面的total方法被执行了。

[ INFO] 2012-09-20 10:20:04,611 [CommonFormats] (CommonFormats.java:getCurrencyRate:159) Thread id is ---------------------------->54 [ INFO] 2012-09-20 10:20:04,611 [CommonFormats] (CommonFormats.java:getCurrencyRate:163) Object does not exist [ INFO] 2012-09-20 10:20:49,529 [CommonFormats] (CommonFormats.java:getCurrencyRate:159) Thread id is ---------------------------->54 [ INFO] 2012-09-20 10:20:49,529 [CommonFormats] (CommonFormats.java:getCurrencyRate:163) Object does not exist

请指出我做错了什么。上述方法将从 
four

方法中调用。 两个方法属于Action类,两个方法属于Service层类。 我的示例代码

//Action class public class StrutsAction1{ public String method1(){ // my code CommonFormats commonFormats= new CommonFormats(); System.out.println(commonFormats.getCurrencyRate()); // my code } public String method2(){ // my code CommonFormats commonFormats= new CommonFormats(); System.out.println(commonFormats.getCurrencyRate()); // my code } } //Business class public class BussinessLogic{ public String method1(){ // my code CommonFormats commonFormats= new CommonFormats(); System.out.println(commonFormats.getCurrencyRate()); // my code } public String method2(){ // my code CommonFormats commonFormats= new CommonFormats(); System.out.println(commonFormats.getCurrencyRate()); // my code } }


java multithreading struts2 thread-local
3个回答
2
投票

现在线程本地仅提供键值映射,以便可以在不同线程中为同一个键维护不同的值。

例如,如果你有两个线程A和B并且想要维护键值对

“name”-> 线程 A 的“John”和
  • “名称”-> 线程 B 的“Fred”
  • 您可以使用本地线程来做到这一点。

您真正需要的是一种应用程序上下文 - 一个存储容器中所有线程共享的数据的地方。另一种可能性是这里的单例。

这在技术上如何实现?

您可能有兴趣阅读

this

this 希望这有帮助


1
投票

http://javapapers.com/core-java/threadlocal/

package com.javapapers; import java.text.SimpleDateFormat; import java.util.Date; public class ThreadLocalExample { private static final ThreadLocal<SimpleDateFormat> formatter = new ThreadLocal<SimpleDateFormat>() { protected SimpleDateFormat initialValue() { return new SimpleDateFormat("yyyyMMdd HHmm"); } }; public String formatIt(Date date) { return formatter.get().format(date); } public static void main(String[] args) { ThreadLocalExample example = new ThreadLocalExample(); System.out.println(example.formatIt(new Date())); } }

这里ThreadLocal被用作静态变量。


0
投票

private static final ThreadLocal<Double> threadLocalRate = new ThreadLocal<Double>();

现在线程区域设置概念正在发挥作用。

© www.soinside.com 2019 - 2024. All rights reserved.