我正在使用现有的基于Web的应用程序,该应用程序使用静态地图存储特定于应用程序的数据。Java:使用静态变量将应用程序规格数据存储在基于Web的应用程序内
这是我的代码低于它负责存储数据的内部ConcurrentHashMap
如下所示。
public class MyClass
// Class variable
private static Map<String, UserThread> usermap = new ConcurrentHashMap<String, UserThread>();
// Inside a Method
public void userData()
{
UserThread userThread= usermap.get(getLoginId());
if (userThread == null) {
userThread = new UserThread();
userThread.start();
usermap.put(getLoginId(), userThread);
}
}
应用程序工作正常,在这里我的问题是,这是一个有效的代码,因为我们可以存储静态变量中的数据? (此处的静态ConcurrentHashMap
包含特定于应用程序数据)
我不是说这是最好的主意,但为什么你不能?你怕什么呢?你有没有想过将它存储在一个servlet上下文属性呢? –
你是什么意思“但你为什么不能”? – Pawan
你问你是否可以做到上述,尽管你已经做到了,而且工作正常。那么,为什么你关心这个代码呢?为什么你担心这可能不是一个好主意? –