Solved

synchronized with subclass

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now