22. Multiple objects of MyClass (given below) are used in a program that uses
multiple Threadsto create new integer count. What will happen when other threads
use the following code?
Choices:
a. The code will give compilation error.
b. The code will give runtime error.
c. Each thread will get a unique number.
d. The uniqueness of the number among different Threads can't be guaranteed.
Answer:-
C is correct.
The use of synchronized ensures that the number generated will not be duplicated, no matter how many Threads are trying to create the number.
Thus D is incorrect.
A and B are incorrect as the above code will not give any compiletime or runtime error.
multiple Threadsto create new integer count. What will happen when other threads
use the following code?
Choices:
a. The code will give compilation error.
b. The code will give runtime error.
c. Each thread will get a unique number.
d. The uniqueness of the number among different Threads can't be guaranteed.
Answer:-
C is correct.
The use of synchronized ensures that the number generated will not be duplicated, no matter how many Threads are trying to create the number.
Thus D is incorrect.
A and B are incorrect as the above code will not give any compiletime or runtime error.
No comments:
Post a Comment