Reputation: 294
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:
Upvotes: 0
Views: 47
Reputation: 4597
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.
Upvotes: 1