Solved

Should calls to new() be protected by synchronized block?

Posted on 2006-06-12
13
216 Views
Last Modified: 2010-03-31
As we know, each thread is awarded a Java stack, which contains data no other thread can access, including the local variables, parameters, and return values of each method the thread has invoked. What about the object references?

Consider  the following method:

Myclass1 func(Myclass2 obj2)
{
 Myclass1 obj1 = new Myclass1(obj2);
 return obj1;
}

Assume that this method is being called by many threads simultaneously, each passing its own value for obj2, should I protect th execution of line Myclass1 obj1 = new Myclass1(obj2) through a synchronized block?
0
Comment
Question by:vi_sharma
  • 6
  • 5
13 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 20 total points
ID: 16883972
no, each thread woill have its own copy of local vars
0
 

Author Comment

by:vi_sharma
ID: 16884001
True, obj1 will be local variable for each thread, but the object it refers to,  which is created using new MyClass(obj2) won't be. Each thread may pass a different value for obj2, so I am wondering if it will lead to some inconsistencies?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16884004
See any book on Principles of Programming languages to know how a new activation record is created for each function call having its own copy of local variables, etc
0
 

Author Comment

by:vi_sharma
ID: 16884018
Mayank, this is little more for me to grasp. Could you please give a dumbed-down explanation of how a new activation record relates to my query on objects on heap being shared by threads?

Thanks.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16884026
Whenever a function/ method is called, a new record for it is created on the stack. This is the activation record of the function that stores its state - in terms of parameter-values, values of local variables, return-address, etc. It is separate for each function 'call', so even if the same function is called 3 times from 3 different places, they will have 3 separate records. The references could refer to same objects, though, in the heap, if they are assigned that way to a shared object.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16884030
In your case, since obj2 is referring to a different object in every 'call', it is fine.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:vi_sharma
ID: 16884044
Mayank, thanks. But I still didn't get this - would a new MyClass() executed by thread 1 would result in return reference value to be falsely used by thread 2? If so,  I need to use synchronized block to prevent that.
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 30 total points
ID: 16884053
No - they are 2 different function-calls, 2 different activation records, 2 different objects and 2 different references to those objects, so it should be fine.
0
 

Author Comment

by:vi_sharma
ID: 16884098
Thanks! clear now.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16884107
Glad to help - you should've split because objects' comment was also correct. I'll ask Venabili (our Page Editor).
0
 

Author Comment

by:vi_sharma
ID: 16884145
Cool. That's fine - you can ask her(him?). I would prefer 30-20 split (30 for you as you spent more time on this with me :-))
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16884155
Done - asked her.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 84
mapAB Challlenge 35 124
Groovy:unable to resolve class error 2 66
object oriented programming comparison 5 54
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

911 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

17 Experts available now in Live!

Get 1:1 Help Now