File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Mock Question. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread Mock Question." Watch "Thread Mock Question." New topic
Author

Thread Mock Question.

Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
This is from Whizlab

Given the following code, which statements are true about the

objects referenced through the fields i, j and k, given that any

thread may call the methods a(), b() and c() at any time?




i.v is guranteed always to be 0 or 1
j.v is guranteed always to be 0 or 1
k.v is guranteed always to be 0 or 1

j.v will always be greater than or equal to k.v at any give time
k.v will always be greater than or equal to k.v at any give time





I donot know right the answer and cannot make head or tails out of this question.
Can some one help me understand this and how to approach such questions.
[ September 08, 2008: Message edited by: Nabila Mohammad ]

The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Nikos Pougounias
Ranch Hand

Joined: Jan 16, 2008
Posts: 110
hi Mohammad,

Firstly you can see that the Counter class provides synchronization for both methods that modify its state. That's good.

Another note is that if you call the method inc() first and the method dec() afterwards, then the value of v does not change.

i appears only inside a() and b() and it's methods inc() and dec() are always called as a pair.

So, if it was only one thread, v's value would always be 0 or 1:
0 before calling i.inc(),
1 after the call to i.inc() but before calling i.dec(), and
0 after the call to i.dec()

The important thing is that both a() and b() are synchronized on the same instance.
This means that even if we have many threads running, only one may execute a() or b() at any given moment.

So the statement "i.v is guaranteed always to be 0 or 1" is true.

Please continue to find the rest.
[ September 08, 2008: Message edited by: Nikos Pugunias ]

Nikos' Java blog
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Hi Nikos,

Thanks for the reply..

I got some part of it.. but i still have a hazy picture of what's going on.
And that's probably because i am not really good with threads.

Could you please elaborate your answer.
Why it couldn't be -

j.v is guranteed always to be 0 or 1 OR
k.v is guranteed always to be 0 or 1
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

I think the answer is i.v and j.v are guaranteed to be either 0 or 1.

Well you will have to understand the question as this

there are suppose three threads. first thread(thread1) invokes b(). it increases the value of i.v so it becomes 1. now another thread(thread2) calls a(), the thread will have to wait as both a() and b() are synchronized. then thread1 increases j.v, then k.v so both become 1, now third thread(thread3) invokes c(). the call will be made immediately since c() is not synchronized. it will increate k.v so it will be 2.

So as you can see that i.v will be increased by one and then decreased by 1 before any other thread can increase or decrease it(as a() and b() are synchronized).

Similarly j.v will be increased by one and then decreased by 1 before any other thread can increase or decrease it(b() is synchronized).

but k.v can be increased by one more than one time concurrently as c() is not synchronized...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Thanks a ton Ankit,
That helped alot.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Mock Question.
 
Similar Threads
synchronized methods + Threads
One more on threads
Thread Question
Mughal rasmussen question
Synchronization Methods