• 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

Synchronizing question

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Example from Enthuware mock test :

The answer is :
One can be certain that none of the letters i, j and k will ever be printed during execution.

My question is how do you decide what object to synchronize on ? In the above example they synchronized on what seems to me to be an object that has got nothing to do with the rest of the code.
In the above example, could I make an instance of any class I have, lets say for example class Animal and then synchronize on that instead of Object to do the above code ?

I guess I'm a bit confused when to synchronize on an object (and what object) and when to synchronize on 'this'.

Is there some concept I'm not getting here ?
 
Author
Posts: 3473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that the lock1 and lock2 are static.
 
Gert Tomas
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, but I dont get it. Whats the significance of the fact that they are static ?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is there some concept I'm not getting here ?



The concept is simple. When two threads are synchronizing on the *same* object, they will block on each other. Which leads us back to...

Sorry, but I dont get it. Whats the significance of the fact that they are static ?



Based on the concept just mentioned, what's the significance? What does the fact that the variable being static do?

Henry
 
Gert Tomas
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mmmm, ok. In the main method there are two instances of TestClass made, so they 'share' the instances of Object that are made because the Objects created here are static, if they were not static then each instance would have its own seperate Objects and they could'nt block each other ?

If I'm correct, then just as a matter of interest, could I use any object instead of an instance of class Object like they did in the example ?
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure of the answer?
well, it seems possible to me that while thread 2 is doing the comparison, thread1 could have increased only 1 of the variables, thereby printing something.

And sure enough, when i tested the code, it printed an endless stream of j's.
 
Gert Tomas
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whoops, the correct answer is :
One cannot be certain whether any of the letters i, j and k will be printed during execution.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mmmm, ok. In the main method there are two instances of TestClass made, so they 'share' the instances of Object that are made because the Objects created here are static, if they were not static then each instance would have its own seperate Objects and they could'nt block each other ?



Correct.

Are you sure of the answer?
well, it seems possible to me that while thread 2 is doing the comparison, thread1 could have increased only 1 of the variables, thereby printing something.



This is also correct.

Synchronization is cooperative. By having the locks as static, the different threads that increment the variables do block on each other. However, the method that does the check and print does not, and hence, may see intermediate values.

Henry
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
--------------------------------------------------------------------------------
In the main method there are two instances of TestClass made,
so they 'share' the instances of Object that are made because the Objects
created here are static, if they were not static then each instance would
have its own seperate Objects and they could'nt block each other ?



Correct.
--------------------------------------------------------------------------------

I ran the above program without the static modifier for the 2 objects
Object lock1 = new Object();
Object lock2 = new Object();

The output was an endless stream of K's. If being 'static' was the reason the 2 threads blocked each other , why is this happening?
Thanks,
Meera
 
reply
    Bookmark Topic Watch Topic
  • New Topic