*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Threading conceptual doubt ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Threading conceptual doubt ..." Watch "Threading conceptual doubt ..." New topic
Author

Threading conceptual doubt ...

Shivaji Bhosale
Ranch Hand

Joined: Mar 12, 2001
Posts: 70
when I am synchronizing on some object,
see a snippet of code,
Object1 obj1;
Object2 obj2;
synchronized(obj1)
{
// Modifying obj1 as well obj2
}
My doubt is, threads wishing to access obj1 will wait until some other thread have acquired a lock over obj1. But what about obj2 ?
My conceptual doubt is ? In multithreaded scenario, synchronized bolck of objects of type, Object1, when applied, Synchronization applied to single object. So multiple locks are maintained for multiple objects. But within that Synchronized
block, if an object of type Object2 is modified within those different snippets, then, consistency with Object of type Object2 , is not maintained.
Now this is what I acquire from my knowledge of threading, I want to know, information I have is correct or wrong ?
If wrong whats wrong ?
If it is correct, then, to synchronize objects of type HashMap, will I have to put them in synchronized block, in order for them to stay consistent ?
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Shivaji Bhosale:
[...] threads wishing to access obj1 will wait until some other thread have acquired a lock over obj1. But what about obj2 ?
Threads wishing to access obj1 will only wait if the access takes place inside a block (or method) synchronized on obj1. Threads wishing to access obj2 will also wait provided the access takes place inside a block synchronized on obj1.
Although it is customary to do so, there is no rule that says that you have to synchronize on the very same object that needs to be protected from multi-threaded access. In fact, there are classes in the Java libraries that use a (private) semaphore object that is created purely so that code can synchronize on its monitor lock.
- Peter
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Just noticed that you also posted this to the Threads forum. Please don't - you'll only cause people to waste their time giving duplicate answers. Since I assume you're asking this in the context of the Developer assignment, I took the liberty of deleting the question from Threads.
- Peter
Shivaji Bhosale
Ranch Hand

Joined: Mar 12, 2001
Posts: 70
I must say objects which r not synchronized within Synchronized code block for some other object, gives consistent behaviour.
To prove this, let me explain what I did,
I made one test class, which toggles values between 5 & 4.
Its simple class, called from Synchronized block.
my synchronized block is like this
synchronized(n[i])
{
testClass_Obj.setValue();
}
This code obviously put at server, design is RMI.
From client side i passed different indices of i simultaneously.
It works fine !
Let me know if i am somewhere wrong ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threading conceptual doubt ...
 
Similar Threads
Reg. Some Thread basics
Avoid making a copy reference of an object
Thread synchronize question from JQ+ test
Doubt on GC
Basic thread doubt