Reputation: 789
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
If a user try to write into to a property file on web app say 'A', all other user who try to write into the same file of same webapp should wait , but another user can write into another files of same web app or file with same name on another webapp .
Will the synchronized method can do all these works ? how it identify the webapp and property file ?
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
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