Solved

synchronized with subclass

Posted on 2013-01-21
2
294 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

752 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