【发布时间】:2012-10-16 02:27:41
【问题描述】:
MainThread 有一个 HashTable,它保存了从 customId 到 SubThread 对象的映射,并将任务放到映射中。 SubThread 从地图中删除任务。 如何避免这个问题?
线程 1:
public void start()
{
subThreadMap = new Hashtable<Integer, SubThread>();
while(true)
{
List<CloudPusherTaskInfo> taskInfos = TestDao.getTasks();
for (CloudPusherTaskInfo task : taskInfos)
{
distribute(task);
}
}
}
private void distribute(CloudPusherTaskInfo task)
{
SubThread subThread = null;
if(subThreadMap.containsKey(task.getCustomerId()))
{
/*
* if subThread exist, add a task to it
*/
subThread = subThreadMap.get(task.getCustomerId());
/* -----at this point, the other subThread maybe end, and return null--------*/
subThread.add(task);
}
else
{
/*
* if subThread is not exist, create a new subthread, then add a task and run it
*/
SubThread newThread = createNewSubThread(task.getCustomerId());
subThread = subThreadMap.put(task.getCustomerId(), newThread);
newThread.add(task);
new Thread(newThread).start();
}
}
【问题讨论】:
-
你的问题到底是什么???你想避免哪个问题?
标签: java multithreading hashtable