【问题标题】:How to store the data using ThreadLocal如何使用 ThreadLocal 存储数据
【发布时间】:2012-09-20 05:00:49
【问题描述】:

我正在使用 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 ;
}
}

我需要从四种不同的方法中调用上述方法。当我从不同的方法调用上述方法时,上述总代码正在执行。 我的要求只是一次整个方法必须执行。剩下的 3 次 整个方法不应该被执行。应该返回存储在 ThreadLocal 对象中的值。

这是我的日志报告,显示上述总方法已执行。

[ 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

请提出我做错了什么。上述方法将从四个方法中调用。 两个方法属于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
}  }

【问题讨论】:

  • 上面的611和529表示调用上面的行号(主叫行号)。
  • 您的要求不明确。
  • @DaveNewton 我需要不止一种方法的 threadLocalRate 变量(几乎有 4 种方法)。它将具有汇率(USD -> INR)。用于查找汇率的代码应该只执行一次。剩下 3 次它不应该得到 .execute。所以我在这里使用了 ThreadLocale 概念。但它不起作用。我更新了问题。
  • 好吧,您当然不需要在操作中使用它,因为操作是按请求创建的。为什么不直接将其传递给其他方法?
  • 如果基于用户详细信息,这似乎很冒险,这意味着每个用户的价值可能不同。

标签: java multithreading struts2 thread-local


【解决方案1】:

我认为 ThreadLocal 在这里不合适。我从未使用过 Struts 2,只使用过 struts 1,但据我所知,这是一个建立在 servelts 之上的 Web 框架。它们在 Web 容器内运行,由 Web 容器决定何时应该打开线程,而您作为开发人员应该干预这个决定。

现在thread local只提供了一个key-value的映射,这样就可以在不同的线程中为同一个key维护不同的值。

比如你有两个线程A和B,想维护键值对

  • "name" -> 'John' 用于线程 A 和
  • "name" -> 线程 B 的 'Fred'

您可以使用本地线程来做到这一点。

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

这在技术上如何实现?

您可能有兴趣阅读thisthis

希望对你有帮助

【讨论】:

    【解决方案2】:

    以下代码对您有帮助吗?我在一个网站上阅读了一篇文章,修改了代码,使其在JDK7.0中免费编译。我使用的资源是

    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用作静态变量。

    【讨论】:

      【解决方案3】:

      我的编码没有错误。我没有将变量设置为静态和最终的。

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

      现在线程语言环境概念将起作用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-26
        • 2017-09-01
        • 2021-07-19
        • 2023-03-04
        • 2011-07-12
        • 1970-01-01
        • 2014-03-29
        • 2015-11-22
        相关资源
        最近更新 更多