代码如下,其中pro是读写文件的操作,运行后发现并没有同步,该如何解决?谢谢public class MonitorThread extends Thread {
private final byte[] lock= new byte[0];
......
public void run() {
... ...
synchronized (lock) {
IniFileProcessor pro = new IniFileProcessor(path);
String currStation = "station" + getI().toString();
String currStat = "";
if (success.equals("1")) {
pro.updatePropettyValue(currStation, "stat", "0", null);
pro.updatePropettyValue(currStation, "mail", "0", null);
} else {
if (pro.getPropertyValue(currStation, "stat") != null) {
currStat = pro.getPropertyValue(currStation, "stat");
}
logger.info(RelativeDate.getDateString(new Date(), "yyyy-MM-dd HH:mm")+"....。");
currStat=Integer.toString(Integer.parseInt(currStat)+1);
pro.updatePropettyValue(currStation, "stat", currStat, null);
}
pro.saveFile();
}
... ...
private final byte[] lock= new byte[0];
......
public void run() {
... ...
synchronized (lock) {
IniFileProcessor pro = new IniFileProcessor(path);
String currStation = "station" + getI().toString();
String currStat = "";
if (success.equals("1")) {
pro.updatePropettyValue(currStation, "stat", "0", null);
pro.updatePropettyValue(currStation, "mail", "0", null);
} else {
if (pro.getPropertyValue(currStation, "stat") != null) {
currStat = pro.getPropertyValue(currStation, "stat");
}
logger.info(RelativeDate.getDateString(new Date(), "yyyy-MM-dd HH:mm")+"....。");
currStat=Integer.toString(Integer.parseInt(currStat)+1);
pro.updatePropettyValue(currStation, "stat", currStat, null);
}
pro.saveFile();
}
... ...
private final byte[] lock= new byte[0];
你可以把
private final byte[] lock= new byte[0];public static final byte[] lock= new byte[0];试试,应该就能同步。但是这支持试试而也,真真用同步的时候不一定就必须这样用,你要做的就是保证你的线程里用的lock是所有线程都能访问的且共用的一个lock就行。