为什么“synchronized”对于变量是非法的?
【腾讯云】亏本大甩卖,服务器4核16G 1年370元(带宽12M,系统盘120GB SSD盘,月流量2000GB)!!!!!!
云产品 配置 价格
服务器 1核2G,带宽5M,系统盘50GB SSD盘,月流量500GB 38元/年
MySQL 1核1G 19元/年
服务器 16核32G,带宽18M,系统盘250GB SSD盘,月流量5000GB 1197元/年
点我进入腾讯云,查看更多详情

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.

推荐文章

快速搭建windows服务器的可视化运维环境

快速搭建windows服务器的可视化运维环境

推荐文章

从任何地方的用户断点

从任何地方的用户断点

推荐文章

如何终止挂起的APM操作

如何终止挂起的APM操作

推荐文章

Python3移植工作流?

Python3移植工作流?

推荐文章

Django字段属性和选项

Django字段属性和选项

推荐文章

Maven替代品?

Maven替代品?

推荐文章

基于原型的对象定向。好的,坏的,丑的?

基于原型的对象定向。好的,坏的,丑的?

推荐文章

蚂蚁金服庆涛:OceanBase支撑2135亿成交额背后的技术原理

蚂蚁金服庆涛:OceanBase支撑2135亿成交额背后的技术原理

推荐文章

在shell中获取程序执行时间

在shell中获取程序执行时间

推荐文章

学习可视化数据采集

学习可视化数据采集

推荐文章

PHP中的变量监视

PHP中的变量监视

推荐文章

理解伊格尔维

理解伊格尔维

推荐文章

10月全球数据库排名

10月全球数据库排名

推荐文章

Visual Studio中的PHP

Visual Studio中的PHP

推荐文章

SQL a编号列-MySQL

SQL a编号列-MySQL

推荐文章

WCF RESTful POST网站

WCF RESTful POST网站