这是我的课:
public class OnlineDataProcessor implements Runnable
{
private final Map<String, VehicleData> recentDataMapping = new HashMap<String,VehicleData>();
public void run()
{
//Here i collect data from database and create objects VehicleData and put them in recentDataMapping.
}
public String toXML(String vehicleId)
{
//Here i take VehicleData object from recentDataMapping and work with it.
}
}
然后在系统启动时我有这个:
OnlineDataProcessor onlineDataProcessor = new OnlineDataProcessor();
Thread a = new Thread(onlineDataProcessor);
a.start();
然后基于servlet请求我有这个代码:
String vehicleId = request.getParameter("vehicleId");
String str = onlineDataProcessor.toXML(vehicleId);
所以问题是...
我是否需要同步对run() 和 toXML() 方法中的recentDataMapping和VehicleData对象的访问?