Page 1 of 1

About Question enthuware.ocpjp.v17.2.3738 :

Posted: Thu Aug 24, 2023 4:39 pm
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.

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

Posted: Fri Aug 25, 2023 12:21 am
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".

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

Posted: Mon Feb 03, 2025 9:21 am
by raphaelzintec
This is not the correct answear: One cannot be certain whether any of the letters i, j and k will be printed during execution
-> We can, run as many times as you want you will only see j

So the correct answear is: One can be certain that the letters i and k will never be printed during execution.
-> Run as many time as you want you won't never see i and k

Because j is the only not being synchronized so it will always be true in if

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

Posted: Sun Feb 16, 2025 1:04 pm
by raphaelzintec
Hello ive spend more time analyzing this code, this topic comments, and so on.

The options are very ambiguous.

Options:
- One can be certain that the letter k will never be printed during execution.
= this is a correct option, k will never be printed
- One can be certain that the letters i and k will never be printed during execution.
= this is a correct option, i & k will never be printed

- One cannot be certain whether any of the letters i, j and k will be printed during execution.
= this option is not clear at 100%, id rather say: One can be certain that the letters i and k will never be printed during execution BUT j may be printed.

it's just my personal opinion

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

Posted: Sun Feb 16, 2025 6:15 pm
by admin
Yes, that might be more precise. More importantly, if you understand the behavior of the program correctly now, the question has served its purpose.

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

Posted: Sun Feb 16, 2025 10:33 pm
by raphaelzintec
yes thank you