File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Mock Question.

 
Nabila Mohammad
Ranch Hand
Posts: 661
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Nikos Pougounias
Ranch Hand
Posts: 110
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Nabila Mohammad
Ranch Hand
Posts: 661
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Nabila Mohammad
Ranch Hand
Posts: 661
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a ton Ankit,
That helped alot.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic