Junior — Middle
Can you give an example of a situation where the system operates with synchronous component interaction?
sobes.tech AI
Answer from AI
Synchronous interaction between components means that one component calls another and waits for its completion before continuing execution. In Java, this often occurs when calling methods, where the execution thread is blocked until the result is received.
Example situation:
Suppose you have a service that requests data from a database. The method that makes the request is called synchronously — the program waits until the data is received, and only then continues working with it.
public String getUserName(int userId) {
// Synchronous call to the database
String name = database.queryUserNameById(userId);
return name;
}
// Call
String userName = getUserName(123); // thread waits for the result
System.out.println(userName);
In this example, the thread is blocked until it receives the user's name, which is an example of synchronous interaction.