簡體   English   中英

如何使用ThreadLocal存儲數據

[英]How to store the data using ThreadLocal

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

我需要從四種不同的方法中調用上述方法。 當我從不同的方法調用上面的方法時,上面的總代碼正在執行。 我的要求是只有一個時間的總方法得到執行。 剩下的三倍的總方法不應該執行。 應該返回存儲在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 layer類。

我的示例代碼

 //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
}  }

我認為ThreadLocal在這里不合適。 我從未使用過Struts 2,僅使用過Struts 1,但據我所知,這是一個基於Servlts構建的Web框架。 它們在Web容器內部運行,並在Web容器內部運行,以決定何時應打開線程,並且作為開發人員,您應干預此決定。

現在,線程本地僅提供鍵值映射,以便可以在不同線程中為同一鍵維護不同的值。

例如,如果您有兩個線程A和B,並且想要維護鍵值對

  • “名稱”->'John'代表線程A和
  • 線程B的“名稱”->'Fred'

您可以使用本地線程執行此操作。

您真正要尋找的是一種應用程序上下文-一個存儲容器中所有線程共享的數據的地方。 另一種可能性是此處的Singleton。

在技​​術上如何實現?

你可能有興趣在閱讀這個這個

希望這可以幫助

以下代碼對您有幫助嗎? 我已經閱讀了網站上的一篇文章,並修改了代碼以使其可以在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用作靜態變量。

我沒有錯誤的編碼。 我還沒有將變量設置為static和final。

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

現在,線程語言環境概念將起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM