0

我使用 JSCH 库创建了一个用于读取和写入远程位置属性文件的应用程序。

我想同步写操作。

这是我的代码,

class WebApp{
    private String webappName;
    private boolean isQA = false;
    private String path ;

    public WebApp(String name , String  path , boolean isQA){
        this.webappName = name;
        this.path = path;
        this.isQA  = isQA;
    }
    public String getWebappName() {
        return webappName;
    }
    public void setWebappName(String webappName) {
        this.webappName = webappName;
    }
    public boolean isQA() {
        return isQA;
    }
    public void setQA(boolean isQA) {
        this.isQA = isQA;
    }
    public String getPath() {
        return path;
    }
    public void setPath(String path) {
        this.path = path;
    }
}

class WebAppProperty implements Runnable{

    private WebApp webapp; // webapp-1
    private String propertyFile; // server.properties
    private String keyValue;

    public String getPropertyFile() {
        return propertyFile;
    }
    public void setPropertyFile(String propertyFile) {
        this.propertyFile = propertyFile;
    }

    @Override
    public void run(){
        writeToPropertyFile();
    }
    public WebAppProperty(WebApp webapp , String propertyFile,String keyValue){
        this.webapp  = webapp;
        this.propertyFile = propertyFile;
        this.keyValue = keyValue;

    }
    
    private void writeToPropertyFile(){
        try{
            // code  for writing key value pair into remote file
        }catch (Exception e) {

        }
    }

}

这就是我要的

  • 如果用户尝试写入 Web 应用程序上的属性文件说“A”,则尝试写入同一个 Web 应用程序的同一文件的所有其他用户应该等待,但另一个用户可以写入同一个 Web 应用程序或文件的另一个文件在另一个 webapp 上具有相同的名称。

    同步方法可以完成所有这些工作吗?它如何识别 webapp 和属性文件?

我们需要同步读写操作吗?

如何在这种情况下实现锁定?

注意:我看到了关于并发的不同问题,但这些问题并没有消除我的疑虑,这就是为什么要问新问题,请帮助我对此进行澄清

4

1 回答 1

0

编写Webapp类如下:

class final WebApp{
private final String webappName;
private final boolean isQA = false;
private final String path ;

public WebApp(String name , String  path , boolean isQA){
    this.webappName = name;
    this.path = path;
    this.isQA  = isQA;
}
public String getWebappName() {
    return webappName;
}

public boolean isQA() {
    return isQA;
}

public String getPath() {
    return path;
}    

}

编写您的 webappProperty 类,如下所示:

class final WebAppProperty implements Runnable{

private final WebApp webapp; // webapp-1
private  finalString propertyFile; // server.properties
private final String keyValue;

public String getPropertyFile() {
    return propertyFile;
}    

@Override
public void run(){
    writeToPropertyFile();
}
public WebAppProperty(WebApp webapp , String propertyFile,String keyValue){
    this.webapp  = webapp;
    this.propertyFile = propertyFile;
    this.keyValue = keyValue;

}

private void writeToPropertyFile(){
    try{
        // code  for writing key value pair into remote file
    }catch (Exception e) {

    }
}

}

如果您使用这种方法,则不需要使用同步。这两个类都是不可变的类。不可变引用可以在多线程环境中自由共享而无需任何同步。这是因为一旦创建了不可变引用,它就会在对象的生命周期内被冻结。您需要对对象进行的任何更改只能通过创建新对象来实现。

请注意,如果您不创建如上所述的不可变类,则必须在 webapp 类的所有方法中使用同步,因为您需要在任何方法中保护共享的可变状态。第二课同上。

于 2017-07-23T03:56:33.250 回答