Am new to Java Thread , while reading i could see that same instances on thread object should wait until the current thread get finished execution . Consider I have two Objects , one is WebApp
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;
}
}
another one is WebAppProeprty
class WebAppProperty implements Runnable{
private WebApp webapp;
private String propertyFile;
private String keyValue;
public String getKeyValue() {
return keyValue;
}
public void setKeyValue(String keyValue) {
this.keyValue = 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 ){
this.webapp = webapp;
this.propertyFile = propertyFile;
}
private synchronized void writeToPropertyFile(){
try{
// code for write property into text file.
}catch (Exception e) {
}
}
}
so if i create two thread like , will the second object should wait for executing the syncronized method ? or it can execute the method parallel.
WebApp app1 = new WebApp("webapp1", "staging/folder", false);
WebAppProperty webappProp1 = new WebAppProperty(app1, "a.proeprties");
webappProp1.setKeyValue("keyvalue");
WebAppProperty webappProp2 = new WebAppProperty(app1, "a.proeprties");
webappProp2.setKeyValue("keyvalue");
Thread t1 = new Thread(webappProp1);
t1.start();
Thread t2 = new Thread(webappProp2);
t2.start();
Note: Updated thread accessing same resource
If two users try to access same resource will the above code block the second user ? if not please help me with the correct way to do it.