About Question enthuware.ocpjp.v17.2.3738 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Badem48
Posts: 26
Joined: Thu Aug 24, 2023 4:33 pm
Contact:

About Question enthuware.ocpjp.v17.2.3738 :

Post by Badem48 »

The code snippet provided creates two threads that repeatedly increment three pairs of variables and then check if those pairs are unequal.

Concerns with Provided Answer:
The answer marked as correct is option B, stating that one cannot be certain whether any of the letters i, j, and k will be printed. However, this appears to be incorrect based on the code's behavior:

1. Pair (i1, i2) and (k1, k2): These variables are incremented inside synchronized blocks. The synchronization guarantees that both variables in each pair are incremented atomically, ensuring that the values of i1 and i2, and k1 and k2, are always equal. Thus, the letters "i" and "k" will never be printed.

2. Pair (j1, j2): These variables are incremented outside synchronized blocks, creating a race condition. As a result, the values of j1 and j2 might be unequal, and the letter "j" may be printed.

Option D, "One can be certain that the letters i and k will never be printed during execution," is the correct answer, as it aligns with the code's behavior.

The provided explanation seems to misinterpret the effect of the synchronized blocks, suggesting that the monitors' acquisition does not protect the atomic nature of the operations. However, the synchronized blocks do indeed ensure atomicity for the variables they protect, contrary to what the explanation implies.

I kindly suggest reviewing the given question and its explanation to ensure alignment with the code's actual behavior. The correct answer appears to be option D, and the current marking may lead to confusion among readers seeking to understand these important concepts in multithreading.

admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v17.2.3738 :

Post by admin »

No, what you are saying is incorrect.

The issue is whether second thread can read the value of i1 and i2 while the first thread has already performed i1++ but has not yet performed i2++.

The second thread does not synchronize on the same lock that the first thread uses, so the second thread can read the value of i2 anytime irrespective of what the first thread is doing. It is not going to wait for the first thread to finish its synchronized block. So, the second thread may see i1 != i2 and print "i".
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 220 guests