aspose file tools*
The moose likes Java in General and the fly likes Restrict access to static variable from non static synchronized and static synchronized method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Restrict access to static variable from non static synchronized and static synchronized method" Watch "Restrict access to static variable from non static synchronized and static synchronized method" New topic
Author

Restrict access to static variable from non static synchronized and static synchronized method

Hariharan Thirumeni
Greenhorn

Joined: Jun 02, 2012
Posts: 3

I have 2 methods, one is static and another one is non static and both are synchronized. So both the methods can execute concurrently by two threads as one thread can use object lock to enter non static syn method and another one can use Class lock to enter static synchronized method. Suppose if i have a static variable, then both the thread can access this variable at the same time. So there will be a race condition. How can we restrict the access to this?


Thanks in advance
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3611
    
  14

If the field contains an object reference, you can let both methods lock on it explicitly. If it contains a primitive, you can just make the field volatile.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Use an explicit lock for this object:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3611
    
  14

And welcome to CodeRanch!
Hariharan Thirumeni
Greenhorn

Joined: Jun 02, 2012
Posts: 3

Thanks stephan.

Just assume that the static variable is an primitive int. Below is the program demonstrates the issue.
Sometimes the output is

Method1:Thread Name :Thread-0 Counter:1
Method2:Thread Name :Thread-0 Counter:3
Method1:Thread Name :Thread-1 Counter:3
Method2:Thread Name :Thread-1 Counter:4


I want the counter should increase sequentially as the variable is static and only one copy of it get stored per JVM....The output clearly shows that there is an conflict in accessing the variable. Volatile doesnt work as it is only for instance variables not for static.


Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3611
    
  14

volatile works fine with static, but I forgot it doesn't do much in the way of race conditions. Yes, the only way is to use a common lock:
Which reminds me. Rob Spoor's solutions shouldn't work in general, because subclasses may return a different class object when getClass() is being called. Static methods can't even use the getClass() method. Always use class literals (Classname.class).
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

You're very right of course.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Restrict access to static variable from non static synchronized and static synchronized method