0

In my system aircraft move randomly and when a condition is meet they send a document to the central station, this means that it can happen that some aircraft send a document at the point in time.

class SingleCentralStation{
    public sendDocument(Document document){
        //do something
    }
}

class Spacecraft{
    private SingleCentralStation singleCentralStation;

    public Spacecraft(SingleCentralStation singleCentralStation){
        this.singleCentralStation = singleCentralStation;
    }

    public sendDocumentToCentralStation(Document document){
        singleCentralStation.sendDocument(document);
    }
}

class App{
    public static void main(String[] args) {
        SingleCentralStation centralStation = new SingleCentralStation(); // singleton

        Spacecraft spacecraft1 = new Spacecraft(centralStation);
        Spacecraft spacecraft2 = new Spacecraft(centralStation);
        Spacecraft spacecraft3 = new Spacecraft(centralStation);
        Spacecraft spacecraft4 = new Spacecraft(centralStation);
        Spacecraft spacecraft5 = new Spacecraft(centralStation);

        // let's imagine that spacecrafts decide to send a document to central station all at the same point in time
        spacecraft1.sendDocumentToCentralStation(new Document());
        spacecraft2.sendDocumentToCentralStation(new Document());
        spacecraft3.sendDocumentToCentralStation(new Document());
        spacecraft4.sendDocumentToCentralStation(new Document());
        spacecraft5.sendDocumentToCentralStation(new Document());
    }
}

Questions:

  • Multiple objects calling another method at the same time is possible?
  • if not, why not?

1 Answer 1

1

Yes its possible to call SingleCentralStation#sendDocument() method at the same time. The example you have given

        spacecraft1.sendDocumentToCentralStation(new Document());
        spacecraft2.sendDocumentToCentralStation(new Document());
        spacecraft3.sendDocumentToCentralStation(new Document());
        spacecraft4.sendDocumentToCentralStation(new Document());
        spacecraft5.sendDocumentToCentralStation(new Document());

These are actually sequential calls executed one after the another.

You will have to handle multithreading scenarios if are going to make SingleCentralStation#sendDocument() call concurrently.

P.S.: No need to handle concurrency if SingleCentralStation#sendDocument() is using all local variables and is not using any state changing class level variables.

Sign up to request clarification or add additional context in comments.

1 Comment

in the case that all aircraft send documents at the same point in time, there is no issue if the method contains only local variable. Right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.