-2

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.

Ansar Samad
  • 729
  • 2
  • 8
  • 15

1 Answers1

0

You have two different objects configured to write to two different files.

From what you are showing here there is nothing that should prevent them from running in parallel using the two threads.

You only need to synchronize threads when they are supposed to access the same data.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • so the same instances means the instance value of the WebAppProperty object should be same , right ? and if two same threads(WebAppProperty) means they accessing same data will have to wait right ? if they are different object means access different data , they can execute the method without waiting? – Ansar Samad Jul 15 '17 at 14:56
  • I have updated the question , what am trying to know is if two users try to access same resource can i block them using syncronized method ?will that above code works – Ansar Samad Jul 15 '17 at 15:04
  • 1
    The very first question is : did you study that duplicated question in depth? – GhostCat Jul 15 '17 at 15:17
  • sorry , that duplicate message confused me , the different instances and different threads statements on that answer confused me. could you please help me to clarify this – Ansar Samad Jul 15 '17 at 15:37