Ansar Samad
Ansar Samad

Reputation: 789

How to Synchronize File operation using java? Synchronized or Lock?

I created a application for reading and writing to a remote location property file using JSCH library.

I want to synchronize the write operation .

This is my code ,

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) {

        }
    }

}

This is what i want

Do we need to synchronize both read and write operations ?

How to implement Lock on this scenario?

Note : i had seen different questions regarding concurrency , but those are not cleared my doubts that's why am asking new question, please help me to get clarifications on this

Upvotes: 0

Views: 1510

Answers (1)

Avneet Paul
Avneet Paul

Reputation: 303

Write the Webapp class as follows:

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;
}    

}

Write your webappProperty class as follows:

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) {

    }
}

}

You don't need to use synchronized if you use this approach. Both the classes are immutable classes. Immutable references can be shared freely without any synchronization in a multithreaded environment.This is so because once an immutable is created it is frozen for the life of the object. Any changes that you need on the object can only happen by creating new objects.

Note that if you don't create immutable classes as described above you'll have to use synchronized in all the methods in the webapp class because you will need to protect shared mutable state in whichever method you touch it. Ditto for the second class.

Upvotes: 0

Related Questions