Win a copy of Design for the Mind this week in the Design forum!

# Threads question from K&B Practice Exams

Piyush Joshi
Ranch Hand
Posts: 207
Hi I have a doubt about question number 25 in Practice Exam no.3:

question is :

Which are true?
A. Output could be P Q R S
B. Output could be P R S Q
C. Output could be P R Q S
D. Output could be P Q P Q
E. the program could cause a deadlock
F. Compilation Fails

"A,B and C are correct. Since pick() is static and release() is non-static, there are two locks.
If pick() was non-static, only A would be correct.
D is incorrect because line 6 swaps the value of id between 0 and 1. There is no chance for the same
method to be executed twice. E and F are incorrect based on the above."

I understand why A, B and C are correct.

But I think D could also be correct. I think that the statement id = 1 - id; is not-atomic. There are two atomic operations here
1. read the value of id
2. change the value of id
So there can be a possibility that two threads might read the same initial value of id i.e. 1 and then both calculate value of id to be 0.
after this both can call pick() method one by one which will print P Q P Q.

Thanks.

Leandro Coutinho
Ranch Hand
Posts: 423
there's no way to both threads have id equals 0 in this part: if(id == 0) { ...
the first thread changes the value to zero, so the second will change it to -1

Maybe it can print R and S twice, but never P and Q twice.

Piyush Joshi
Ranch Hand
Posts: 207
Leandro Coutinho wrote:there's no way to both threads have id equals 0 in this part: if(id == 0) { ...

my doubt is that: for example, increment operation is not atomic (See this link)
on similar terms:
id = 1 - id; is also not atomic, first read the value of id, then calculate expression, and then write value of id. (am I wrong?)
Thread1 calculates the expression (1-1) and assigns id=0;
Thread2 calculates the expression (1-1) and assigns id=0;

so now both threads see value of id=0;

Please tell me if I am wrong.

Thanks

Leandro Coutinho
Ranch Hand
Posts: 423
Leandro Coutinho wrote:there's no way to both threads have id equals 0 in this part: if(id == 0) { ...

Let I correct myself. All threads always have the same id because it is static
Now, even if this expression (id = 1 - id;) is not atomic, the second thread that finishes this expression, will change the value of id to 1 (not -1 as I told before xD).
Hence, option d can NOT be correct.

Leandro Coutinho
Ranch Hand
Posts: 423
Thread1 calculates the expression (1-1) and assigns id=0;
Ok
Piyush Joshi wrote:Thread2 calculates the expression (1-1) and assigns id=0;

No. Only if there's a bug in the memory management. The expression should be (1 - 0)

Piyush Joshi
Ranch Hand
Posts: 207
Piyush Joshi wrote:
Thread1 calculates the expression (1-1) and assigns id=0;
Thread2 calculates the expression (1-1) and assigns id=0;

so now both threads see value of id=0;

Let me clarify myself:
I think id = 1 - id; will be calculated by making a temporary variable in memory and storing value of id:
somewhere in memory: so id = 1 - id; will be broken down to this:
int temp = id;
id = 1 - temp;

so what I said before read it like this:

So first Thread1 reads value of id =1 and stores it in a temporary int temp1 = 1;.
then Thread2 also reads id = 1 and stores it in a temporary int temp2 = 1;.
Thread1 calculates the expression (1-temp1) and assigns id=0;
Thread2 calculates the expression (1-temp2) and assigns id=0;

This is what I actually wanted to ask. Sorry for misunderstanding....

is there something wrong in my analysis?

Leandro Coutinho
Ranch Hand
Posts: 423
Piyush Joshi wrote:is there something wrong in my analysis?
Yes, there is. So, static variables belongs to the class, not objects or threads. The purpose of a static variable is that its value is the same and shared with all instances, no matter if it is in the same thread or not. So there's nothing like temp1 or temp2.

But Java threads cannot arbitrarily access each other's data objects: they need permission to access the objects, and one thread needs to pass the object reference to the other thread. Static variables are the big exception to this analogy: they are automatically shared between all threads in a Java program. http://oreilly.com/catalog/jthreads/excerpt/ch01.html
I understand what you mean, but don't care about it man. There're more important things for the exam. Let this thread open and maybe someone else has another thing to share.