为什么“synchronized”对于变量是非法的?

It is a very easy question but I cant get the idea behind it:

Why synchronized cannot be assigned to Java variables?

It can be assigned to methods and blocks, only. I personally think this would be a very neat addition to the Java programming language.

#0

they have the volatile keyword for that.

#1

A synchronized block protects the complete code block. A synchronized on a method protects the complete code of the method. In both cases synchronized refers to an object it uses as the monitor - either this (or the current class object) or the specified object.

First: The requirement for a monitor object means that a synchronized field could not be a primitive type.

Second: What should a synchronized on a field do?

There are two possible answers:

  • protect access to the reference itself XOR
  • protect access to the object behind the reference.

Option one would be a hen-egg problem: In order to protect the access the field must be first accessed. Try to do this atomically on a multiprocessor might be fun. Furthermore: If the access is a write (i.e. the object changes) then the monitor would be on the first object while the second one is installed ... madness is on this road.

Option two: See this piece of code:

public class Foo {
     public synchronized StringBuilder sb = ...;
     public void doSomething1(){
         StringBuilder sb = this.sb;
         sb.append("foo");
     }

     public void doSomething2(){
         this.sb.append("foo");
     }
}

Since only the access would be protected, both methods do the same. The first version just makes it a little clearer, that the append call itself is not protected.

Oh, I forgot: In both cases you could use only the mutual exclusion of synchronized - the wait or notify stuff would be unavailable - because you cannot attach any piece of code to the variable.

What is left:

Perhaps just a shortcut for AtomicReference?

This is such a rare usecase, that changing the language itself is not a good option.

#2

Wouldn't help you. You use synchronized to make a block of code atomic. So most of the time, you have more than one line of code you want to synchronize.

#3

If you're looking for fields that act atomically, try the java.util.concurrent.atomic classes. For instance, an AtomicInteger lets you add numbers to it atomically. If object allocation is a concern and you're sure you need it, AtomicIntegerFieldUpdater<T> (and its ilk) can give you atomic-like access to volatile fields -- but you have to be careful with that, because it's easy to break your code with that approach.

推荐文章

in(std::cin):这是什么意思?

in(std::cin):这是什么意思?

推荐文章

代表与可可触摸事件

代表与可可触摸事件

推荐文章

是否可以将默认的wpf设计器用于不从ContentControl继承的自定义“控件”?

是否可以将默认的wpf设计器用于不从ContentControl继承的自定义“控件”?

推荐文章

字符串格式的字符串变量的参数列表

字符串格式的字符串变量的参数列表

推荐文章

如何让一个图像在Wordpress模板中循环显示?

如何让一个图像在Wordpress模板中循环显示?

推荐文章

如何从Jackson中的数组开始反序列化JSON文件?

如何从Jackson中的数组开始反序列化JSON文件?

推荐文章

在sql server中转换带时间的数据时间到不带时间的datetime?

在sql server中转换带时间的数据时间到不带时间的datetime?

推荐文章

为什么WinForm Designer显示消息“Value cannot be null。参数名称:context“?

为什么WinForm Designer显示消息“Value cannot be null。参数名称:context“?

推荐文章

如何在UITextField中显示来自UIDataPicker的值而不带按钮

如何在UITextField中显示来自UIDataPicker的值而不带按钮

推荐文章

如何在页面上导航后在页面上保存内容

如何在页面上导航后在页面上保存内容

推荐文章

iPhone背面按钮自定义图像

iPhone背面按钮自定义图像

推荐文章

Android SQLite插入错误

Android SQLite插入错误

推荐文章

在ExtJs中更改滚动条

在ExtJs中更改滚动条

推荐文章

从设计的角度看,CouchDB的修订应该如何处理?

从设计的角度看,CouchDB的修订应该如何处理?

推荐文章

在R中产生意外结果的合并

在R中产生意外结果的合并

推荐文章

R指挥官圆木窗高度

R指挥官圆木窗高度