2012-09-20 31 views
-1

我正在使用Strust2和Hibernate。我必須找出貨幣匯率(美元兌INR)。我需要在多個地方使用這些信息。爲此,我正在使用ThreadLocal來達到此目的。如何使用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類,兩種方法屬於服務層類。

我的示例代碼

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

在上面的611和529中指出了上述呼叫的線路號碼(呼叫者線路號碼)。 – Daya

+0

您的要求不明確。 –

+0

@DaveNewton我需要threadLocalRate變量多個方法(近4種方法)。它將有匯率(美元 - >印度盧比)。用於查明匯率的代碼應只執行一次。剩餘3次它不應該得到執行。所以我在這裏使用了ThreadLocale概念。但它不工作。我更新了這個問題。 – Daya

回答

2

我覺得ThreadLocal的是不恰當的位置。我從來沒有使用過Struts 2,只有struts 1,但據我所知這是一個建立在servelts之上的web框架。這些內容運行在Web容器內部,並由Web容器決定何時應該打開線程,並且作爲開發人員應該干預此決定。

現在,線程本地只提供鍵值映射,以便可以爲不同線程中的相同鍵維護不同的值。

舉例來說,如果你有兩個線程A和B,並希望保持鍵值對

  • 「名」 - >「約翰」的線程A和
  • 「名」 - >「 Fred'用於線程B

你可以使用線程本地進行。

您真正需要的是一種應用程序上下文 - 一種用於存儲由容器中的所有線程共享的數據的位置。另一種可能性是單身人士在這裏。

這在技術上可以實現嗎?

您可能會感興趣的閱讀thisthis

希望這有助於

1

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

0

我沒有在我的編碼錯誤。我沒有把這個變量當作靜態和最終的。

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

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