Oracle
1Z0-895 · Question #29
Given the following stateless session bean: How would you change the EJB to prevent multiple clients from simultaneously accessing the sayHello method of a single bean instance?
The correct answer is A. Convert sayHello into a synchronized method. It is not possible for two invocations of synchronized methods on the same object to interleave. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first…
High Availability
Question
Given the following stateless session bean: How would you change the EJB to prevent multiple clients from simultaneously accessing the sayHello method of a single bean instance?
Exhibit
Options
- AConvert sayHello into a synchronized method
- BExecute the call to generateLocalizedHello in a synchronized block
- CConvert generateLocalizehello into a synchronized method
- DConvert HelloWordBean into a singleton bean
- ENo changes are needed
How the community answered
(28 responses)- A79% (22)
- B11% (3)
- C7% (2)
- E4% (1)
Explanation
- It is not possible for two invocations of synchronized methods on the same object to interleave. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object. * When a synchronized method exits, it automatically establishes a happens- before relationship with any subsequent invocation of a synchronized method for the same object. This guarantees that changes to the state of the object are visible to all threads. Reference: The Java Tutorial, Synchronized Methods
Topics
#singleton bean#synchronized method#thread safety#concurrent access
Community Discussion
No community discussion yet for this question.
