• 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

Toughie from Khalid's mock exam (Q7)

 
Ranch Hand
Posts: 158
  • 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 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?
<pre>
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();
}
}
</pre>

select valid answer
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
Beats me. Any ideas anyone ?
rgds
Sahir
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say that its important to be able to solve this kind of problem, I just had a bad experience in threads, with 53% in this section.
And I considered myself very strong in threads.
 
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me give it a shot, I would pick 1, 2. Because it seems to me that the only guarantee of the synchronization is j and i. Since k can be changed in a non-synchrozied method.
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Adrian, I think it is 1 and 2.
Reasoning:
If you create an object and have 3 threads running, when ever they call method a or b, they get locked, so i.v and j.v will always get incremented and then decremented by 1. Leaving them at 0 and 1. Since it is synchronized, you will never get them to be greater than or less than 1 and 0 since they get locked.
However method c is not synchronized, so one thread can go in and start c and increment k.v and then another thread can take control if it is a higher thread or becuase of timeslicing and run c incrementing k.v again so it is now 2.
As for the last two, neither of those can be guaranteed becuase we know that j.v will always be 0 or 1, but k.v can be 2 as in the above example, but you don't know when it will happen and there could be times that j get incremented to 1 and k is still at 0 and other times where k is at 1 and j is at 0.
These are my thoughts, any comments from anyone else. Does anyone have the answer from Khalid's exam?
Bill
reply
    Bookmark Topic Watch Topic
  • New Topic