我有一个
Java类,它启动2个独立的线程.第一个线程启动正常,所有变量都正确.
当我启动第二个线程时,来自线程1的全局变量更改为线程2中设置的值.
我已经尝试添加全局变量更新的同步块,但这不起作用.
有没有办法解决这个问题?我希望每个线程都启动并使用自己的值而不会干扰其他线程值.
编辑:
我的Thread类的片段:
public abstract class ConsumerIF implements Runnable {
public static Element root = null;
public static String name = null;
public static String type = null;
public static String location = null;
public final synchronized void reconfigure() throws FatalDistributionException {
Document doc = builder.build(new StringReader(xmlCollector));
root = doc.getRootElement();
Element nameElement = root.getChild("name");
Element typeElement = root.getChild("type");
Element locationElement = root.getChild("location");
Element scheduleElement = root.getChild("schedule");
if (nameElement != null && typeElement != null && locationElement != null){
name = nameElement.getTextTrim();
type = typeElement.getTextTrim();
location = locationElement.getTextTrim();
}
}
}