【发布时间】:2013-09-19 12:06:23
【问题描述】:
我有一个类可以读取 xml 文件并将它们填充到私有静态数据结构(例如 HashMap)中。这个初始填充发生在一个静态块中。然后我有方法来获取给定键的值,实习生指的是静态 HashMap。考虑一下这种情况,当多个线程尝试获取给定键的值时,是否会影响性能?例如,当一个线程正在读取该静态对象时,其他线程必须等待。
public class Parser
{
private static HashMap resource = new HashMap();
static
{
parseResource();
}
private Parser()
{
}
private static parseResource()
{
//parses the resource and populates the resource object
}
public static Object getValue( String key)
{
//may be some check will be done here, but not any
//update/modification actions
return resource.get(key);
}
}
【问题讨论】:
-
只要读取过程没有定义为同步,线程就不会等待其他线程完成。
标签: java multithreading static-methods static-members