Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronization?  RSS feed

 
Neeraja Rajan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could one of the experts please explain SYNCHRONIZATION to me.
Pretty confused.
Thank you
Neeraja
 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you create an object like this
a first=new a();
a second=new a();
while a is declared as
class a{
int i;
public void p(){
}
}
please take note of the fact that in memory only one copy of the class exists , Thus there is only one method by the name p() in memory. while as many copies of the instance variables as Objects created exist in memory. When one thread accesses the object first and the another accesses the object second then there is no concurruncy problem as the instance variables are different.
However when two or more threads access the same object then there is a chance that a concurrency problem may occur. This also holds true for static methods.
let us say the p method is defined as
public void p(){
i=5; // line 1 , i not local
/// some complex operation and the result is of i is 10
System.out.println(i);
}
the problem occurs when for example one thread is accessing method p. It initializes i to 5 and undertakes processing. when it is about to print out the result another thread enters p and initializes i to 5. what happens??. the value of 5 is printed out by the first thread while 10 was expected to be printed out.
Thus in such cases synchronization is needed.
for more idea on synchronization check out these articles on javaWorld
here
Regds.
Rahul P. Mahindrakar

[This message has been edited by Rahul Mahindrakar (edited August 26, 2000).]
 
Neeraja Rajan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the simple explanation- understood it well..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!