• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

class Counter {
int v = 0;
synchronized void inc() { v++; }
synchronized void dec() { v--; }
}

public class Q7ed5 {
Counter i;
Counter j;
Counter k;
public synchronized void a() {
i.inc();
System.out.println("a");
i.dec();
}

public synchronized void b() {
i.inc(); j.inc(); k.inc();
System.out.println("b");
i.dec(); j.dec(); k.dec();
}

public void c() {
k.inc();
System.out.println("c");
k.dec();
}
}

Select the two correct answers.

1)i.v is guaranteed always to be 0 or 1.

2)j.v is guaranteed always to be 0 or 1.

3)k.v is guaranteed always to be 0 or 1

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

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

1 and 2 are correct, I agree with that.
I think 5 answer is also right.
What you ppl think.
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think 3 is also correct because counter methods are sychronized do any method calling method c will wait on lock of "k".
Correct me if I am wrong.

why do u think 5 is correct?
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only choices 1 & 2 are correct.

Choice 3 is incorrect because many threads can access the method c() as its not synchronized(i.e. two threads might have incremented the k.v but none has decremented k.v, thus k.v > 1);
Choice 4 is incorrect since when the method c() alone is called then k.v > j.v;
Choice 5 is incorrect because in method b(), j.inc() is called before k.inc() so in this condition j.v > k.v

[ May 10, 2006: Message edited by: Shaliey Gowtham ]
 
Girish Nagaraj
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes You are right.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where did you get this code?

The only correct answer on a test would be "runtime error", because i, j and k are never initialised.

That aside, the only correct answer I can see is 2:

- 1 is not true: say Thread 1 calls a(). i.inc() is executed. Then Thread 2 calls b() BEFORE THREAD 1 EVER REACHES i.dec(). Thread 2 also executes i.inc(). Then i == 2
- 2 is true because j can only be incremented and decremented in the b() method. This method is synchronized, so j.dec() will be called before any other thread can re-execute the b() method
- 3 is not true for the same reason as 1
- 4 is obviously not true: say c() gets called once before any other method, then j = 0 and k = 1 (after the 1st statement)
- 5 is not true: say a thread executes method b(): after the 2nd statement j will be 1 and k 0
 
Shaliey Gowtham
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mathias De Groof,
Welcome to JavaRanch.
Nice to point out the variables uninitialized.
But choice 1 is correct as there can be only one thread executing one synchronized block to the fullest on an instance(Object has only one lock);

i.e when one thread has entered a synchronized method in a class for a particular object no other thread can execute any other synchronized methods for that particular object in reference to non-static methods

Hope that's clear.
[ May 10, 2006: Message edited by: Shaliey Gowtham ]
 
mmg
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right, I forgot the synchronization happens on the object. Thanks for pointing that out!
 
vivekkumar sharma
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
good point Mathians regarding unintailised variables .

I am not getting following point

Isn't for threads excuting methods a and b will have to obtain locks on all three objects 1,j and k before method exceution can begin.

if this is TRUE then if any of the methods a() or b() are already excuting by some other thread, Thread wanting to exceute method c() will have to wait. because if method c is not syncronised, But to exceute k.dec() and k.inc() functions this method has not obtain lock on object "k".

Please help me to build my understanding on this.
TA
Vivek
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When a thread calls an object's synchronized method, the WHOLE object is locked (in your case, the instance of Q7ed5 class is the locked object.). This means that if another thread tries to call ANY synchronized method of the same object, the call will block until the lock is released (which happens when the original call finishes).
 
vivekkumar sharma
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks wise owen ,
so that means locks on objects i,j,k and held and relased as inc and dec functions are invoved on to them, and they are not held for whole life time of execution of method a,b

TA
Vivek
 
reply
    Bookmark Topic Watch Topic
  • New Topic