aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mind job question about threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mind job question about threads" Watch "Mind job question about threads" New topic
Author

Mind job question about threads

Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Given the following code, which statements concerning the objects referenced through the member variables i, j and k are true, given that any thread may call the methods a(), b() and c() at any time?

1. i.v is guranteed to be always 0 or 1
2. j.v is guranteed to be always 0 or 1
3. k.v is guranteed to be always 0 or 1
4. j.v will be greater than or equal to k.v at all times
5. k.v will be greater than or equal to j.v at all times
The given answer is 1, 2

This code is really baking my noodle for the following reasons:
1) How the instance i.inc() method is going to be called without even creating an instance of the class Counter?
2) Let say we�ve create an object and we have 3 threads running on this object, now whenever a() or b() is called, that thread will get the lock on this object (since both methods are synchronized). Let us say that the thread is executing on a(), what happens here is that the value of v.i gets incremented to 1 then decremented back to zero, so the final guaranteed value of v.i is always zero (it is not possible for v.i to have the value 1, making option 1 incorrect), right? The same question is applied to j.i
3) My third and last question, what prevents me of creating instances of I,j,k then calling inc() or dec() methods as many times as I want? Nothing, then it is valid to say that you can change �v� to any value you want � what I am trying to say here is that non of these options are correct.
If someone can shed a light on my doubts, I�ll be really appreciated.
[ February 09, 2004: Message edited by: Vicken Karaoghlanian ]

- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
Sriram Chintapalli
Ranch Hand

Joined: Dec 16, 2003
Posts: 59
Hi Vivken,
I would say the correct answer is 2. For a minute lets just think the variable v is private and i,j,k all reference the same instance of Counter(and also that we have created it in the first place).
Imagine our thread say main thread created an instance of Q7ed5 say Q and calls method a()and method b() in that order. This executes sequentially without any waiting time on the lock because when main thread enters the a()[calls i.inc() and increments v] it obtains the lock of Q and can simutaneously enter method b()[call i.inc() and increment v again] before i.dec() is executed in method a(). So now the value of i.v can actually be 2.
Consider a similar scenario with answer 3 also. but in option 2 j.v is being incremented and decremented only 1 in all the methods so it is guarenteed to be 1 or 0 at all times.
You should be able to weed out answers 4 and 5 using the same logik
So my answer is ONLY 2. Correct me if you think I am wrong.
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
First of all my name is Vicken and not Vivken
And secondly you can't just suppose that 'v' is private because it is simply NOT, the same goes for i,j,k.

Imagine our thread say main thread created an instance of Q7ed5 say Q and calls method a()and method b() in that order. This executes sequentially without any waiting time on the lock because when main thread enters the a()[calls i.inc() and increments v] it obtains the lock of Q and can simultaneously enter method b()[call i.inc() and increment v again] before i.dec() is executed in method a(). So now the value of i.v can actually be 2.

if thread 't' has the lock to execute method a() then that thread will continue the execution of a() to the end, then will start the execution of b().
a() can't start executing untill b() is finished and vise versa.
Option 3 is definitly wrong because the method c() is not synchronized, therfore it is possible to one or more thread to execute k.inc().
I totally agree that 3,4,5 are wrong, but 1 and 2 being correct is not convincing yet (for the reasons i provided in my first post).
Thanks for trying.
[ February 09, 2004: Message edited by: Vicken Karaoghlanian ]
Sriram Chintapalli
Ranch Hand

Joined: Dec 16, 2003
Posts: 59
Sorry Vicken for getting your name wrong the last time('v' is next to 'c' on the keyboard) and lets not even try to spell both our last names
my bad I thought a() and b() can be started by the same thread simultaneously. yep so a() has to finish and then b() has to start. That explains it so answers 1 and 2 are correct only if we assume v to be private and the same object is being referenced by i,j,k otherwise I would agree with you its a straight compiler error in the first place and v cannot be public. I did run through questions in mock exams where certain things have to be assumed to arrive at the correct answer which actually is not correct otherwise.
So it is entirely upto you to arrive at a conclusion.
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
The answer 1, 2 can't be correct, the way i understand it, 'guaranteed' means 'the value i.v can hold when the synchronized method exits', therefore the guaranteed value of i.v is ALWAYS 0 (i know that it becomes 1 then back to zero). If the question said "the value(s) i.v can hold" then yes, i would say that i.v can be 0 or 1.
Personally i think the question suffers from a wording problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mind job question about threads
 
Similar Threads
Toughie from Khalid's mock exam (Q7)
One more on threads
Synchronization Methods
Mughal rasmussen question
Thread Question