• Post Reply Bookmark Topic Watch Topic
  • New Topic

synchronization

 
Allan Morris
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After reading the java tutorial :
"Synchronized methods enable a simple strategy for preventing thread interference and memory consistency errors: if an object is visible to more than one thread, all reads or writes to that object's variables are done through synchronized methods."


Since all objects have the potential to be visible to more than one thread hould I synchronize all mutative methods in all classes I write as a standard procedure. I'm thinking of changing all my setters to the form:
public synchronized void setSomething(int val);

I'm not sure if this is a good idea or is excessively cautious (can there be such a thing) and if this a possible source of performance problems for larger projects.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35709
408
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Allan,
Welcome to JavaRanch!

In that quote, they are discussing the callers of the getters and setters as opposed to the getters and setters themselves.

Suppose I write the following method:

This method is unsafe because I am setting the value using the getter's result from earlier. It may have changed since then. So I need a synchronized block to avoid this problem on method "unsafe." Adding one to the getter and setter wouldn't help as I could still write my method as is.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35709
408
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PM from Allan:
Hi,

Thanks for your welcome and the good example on the unsafe method call. I've still got one concern from the java tutorial code relating to inside an accessor method though.

( http://java.sun.com/docs/books/tutorial/essential/concurrency/interfere.html )

The tutorial says this specifically:
---------------------------
class Counter {
private int c = 0;

public void increment() {
c++;
}

public void decrement() {
c--;
}

public int value() {
return c;
}

}

It might not seem possible for operations on instances of Counter to interleave, since both operations on c are single, simple statements. However, even simple statements can translate to multiple steps by the virtual machine. We won't examine the specific steps the virtual machine takes � it is enough to know that the single expression c++ can be decomposed into three steps:

Retrieve the current value of c.
Increment the retrieved value by 1.
Store the incremented value back in c.
------------------------------------------------
and my concern is that the set method itself, in addition to making the unsafe method synchronized, is also a risk and should be synchronized too?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35709
408
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Allan,
That example is a little more complex as c++; maps to three statements. A single getter/setter maps to one. I'm not sure if a simple getter or setter require a synch block. Now that this is in the threads & synchronization forum, I'm sure someone around does know and I look forward to reading the answer.

My point above was that the code calling the class needs to be sychronized, not just the lower level class. It doesn't preclude the lower level class from being sychronized too of course.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!