Solved

synchronized with subclass

Posted on 2013-01-21
2
293 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

828 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