File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Synchronization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Synchronization" Watch "Synchronization" New topic
Author

Synchronization

meera kanekal
Ranch Hand

Joined: Feb 13, 2005
Posts: 75
Hi,
Could anyone out there clarify what KS&BB mean by static and non-static synchronized methods accessing a non-static field on page 713?
The rule is : Static methods can access only static variables and instance methods can access both static and non-static variables.
In that case how can a static synchronized method access a non-static field using an instance?
Thanks,
Meera
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

Hi Meera. welcome to javaranch.

As per your question, I think you must post a code to what you don't understand. If you create a new instance inside the static method of the class in which it is, then the method can access all the non-static members of the class and no synchronization will interfere as it has it's own instance.
This is what I am referring to


But I don't think this is what you are referring to. So please tell your exact doubt...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Tag for later. I was also completely confused with static synchronization.


SCJA
~Currently preparing for SCJP6
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 417
    
    2

I think the authors question isn't about synchronization as such ...

If you change the rule to Static methods can access only static variables (assuming they don't have an instance) does that help .... ie static methods don't normally have an instance (no this) unless you pass one as a parameter as demonstrated above .

The fact the synchronized static method can access non static methods (in your second statement) is because it has an instance not because its synchronized.

ie you two statements don't disagree, its just your rule implies you have no access to an instance and your second example explicitly tells you have.


Apologies if this was a question about class vs object locks but I suspect its not ;-)


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
meera kanekal
Ranch Hand

Joined: Feb 13, 2005
Posts: 75
public class StaticThreadDemo {

int nonStatvar = 10 ;

/*
* when a static method access a non static field using an
* instance
*/
public static synchronized int calculateInt() {
StaticThreadDemo demo = new StaticThreadDemo();
demo.nonStatvar+=200;
return demo.nonStatvar;
}


/*
* nonstatic method also access variable nonStaVar
*/
public synchronized int calcInt() {
nonStatvar+=300;
return nonStatvar;
}



public static void main(String[] args) {

StaticThreadDemo stdem = new StaticThreadDemo();

System.out.println(" Non Static "+stdem.calcInt());
System.out.println(" Static "+calculateInt());
System.out.println(" nonstatvar"+stdem.nonStatvar);
}

}
The output is
Non Static 310
Static 210
nonstatvar310
So here both the static and non-static methods are accessing a non-static variable. The static method has a lock on the Class object and the non-static method has a lock on "this". So they are both running on seperate threads. Is that why we see different data for the same instance variable in the output?
Ankit I am clear on how a static method can access an instance variable. Thanks for your reply. But I am trying to understand what happens to an instance variable when it is manipulated by a static synchronized method and a non-static synchronized method.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

The static method has a lock on the Class object and the non-static method has a lock on "this". So they are both running on seperate threads.


Meera this is why I posted what I did. You are confused here. Although the instance variable is the same, but static method and the non-static method have a different copy of the field nonStatVar. There is no question of synchronization here. Both the methods are running in the same thread.
meera kanekal
Ranch Hand

Joined: Feb 13, 2005
Posts: 75
Thanks Ankit for your reply. The example code I posted is not right. There there are 2 different objects and both the static and non-static synchronized methods have their own copy.
The statement that I am trying to understand from K&B is -----
On pg713 they state while discussing how even synchronization may not prevent data from being corrupted in cases of , and I quote them here "However - what if you have a non-static method that accesses a static field? Or a static method accesses a non-static field (using an instance)?"
I would be very grateful if any of you could give me an explanation.
Thanks,
Meera
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

Meera I think you have 5.0 book. As the page number that you have given doesn't contain what you have said in my 6.0 book.

First you have to know how synchronization works when static and non-static methods access a resource.

Suppose there is a static field count. Now one synchronized static and one synchronized non-static method accesses it. Then they can access it concurrently



Now in this code you cannot be sure that every value of count will be displayed. As one of the method may increase the value of count and before it can display the value of count, the other method may increase the value of count again(it can be even worse as nonStaticMethod may be called on more than one instance of ThreadTry concurrently but let's forget that for the moment). This is possible as static methods put lock on the class and non-static method put lock an instance which are independent of each other.

The solution to this problem is this



The opposite may also be true. More than one method may manipulate the value of an instance field. This is more complicated than the previous one. Here's example of how this can happen



Now again the field count is not safe from concurrent access as both the methods can modify the value of count concurrently.

The solution to this problem is this

meera kanekal
Ranch Hand

Joined: Feb 13, 2005
Posts: 75
Thank you Ankit for your explanation. It is very clear now. sorry I forgot to mention that I was quoting from the SCJP 5 K&B book. Thanks everybody for your input.

Meera
 
GeeCON Prague 2014
 
subject: Synchronization