Solved

synchronized with subclass

Posted on 2013-01-21
2
292 Views
Last Modified: 2013-01-21
hi guys

Object2 extends Object1

public class MyClass{
...
Object1 object1 = new Object1();
Object2 object2 = new Object2();
.....
....
synchronized( object1){
  // code 1
}

//code 2
synchronized( object 2){
  // code 3
}
}

When thread t1 executes code1, can thread t2 execute code3 at the same time?

thanks.
0
Comment
Question by:royjayd
  • 2
2 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 375 total points
ID: 38803889
Yes, object1 and object2 are two distinct objects are so there are two distinct 'locks' that are operated on here. The class heirarchy has no impact on this at all. This would even behave in the same way...

Object object1 = new Object();
Object object2 = new Object();

Again, because they are two different objects (even though they are the same class).
0
 
LVL 35

Expert Comment

by:mccarl
ID: 38803894
The below, however, would result in only one code block being able to be executed at a time... (just to point out that it needs to be the same actual object that the synchronization works on, even this one object has to variables the refer to it)

Object2 object2 = new Object2();
Object1 object1 = object2;
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question