• 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

Mughal, review question 9.6, p.282 (Synchronization)

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey javaranchers,
given this code, which one statement is true:
public class Myclass extends Thread{
static Object lock1 = new Object();
static Object lock2 = new Object();
static volatile int i1,i2,j1,j2,k1,k2;
public void run(){while(true) {doit(); check();}}
void doit(){
synchronized(lock1){i1++;} //1
j1++;
synchronized(lock2){k1++; k2++;} //2
j2++;
synchronized(lock1){i2++;} //3
}
void check(){
if (i1 != i2) System.out.println("i");
if (j1 != j2) System.out.println("j");
if (k1 != k2) System.out.println("k");
}
public static void main(String args[]){
new MyClass().start();//Thread A
new MyClass().start();//Thread B
}
}
a) The program will fail to compile
b) One can't be certain whether any of the letters i,j, and k will be printed during execution
c) One can be certain that i,j,k won't be printed during execution
d) One can be certain that i,k will never be printed during execution
e) One can be certain that k will never be printed during execution

my answer was c, however the right answer is b.
Does this mean that if Thread A gets to 1 first,
Thread B can go ahead and execute 2 or 3(and not wait in 1)
then return to 1 later
Thanks
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I see it, Thread A can run 1,2 & 3 and then pause. This will bring i1 & i2 to the same value. Thread B then runs 1 and then pause, bringing i1 to 2 and keaping i2 at 1. Thread A then continues with Check(), and voila i1 and i2 is not the same value, so I is printed. Likevise for j. Since 2 only puts a lock on lock2, lock1 is still free to do as it pleases (and it might be in the check() when lock2 is executing this).
Thus b is the correct answer.
Hope this helped
/Mike
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you get condition where k1 and k2 are different?
i thought answer is e
 
Mikael Jonasson
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happends if one of the lock2 thread pauses between the ++ statements of 2, and the other then starts up, in the middle of Check()?
/Mike
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was also having troubles with this question. Any explanations out there?
 
Alex Black
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ey javagurus common! are you just gona let this thread travel into oblivion??
 
Mikael Jonasson
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry, did my last statement sound like a question. It was meant to mkake to consider the posebility. That situation is the one that causes k to print.
/Mike
 
Alex Black
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Mikael,
sorry I missed out on that clarification...
anyway got your point, thanks so much.
 
There's a way to do it better - find it. -Edison. A better tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic