java 同步语句有什么用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1268016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:52:28  来源:igfitidea点击:

What is synchronized statement used for?

javasynchronization

提问by Johanna

What is the usage of synchronizedstatements?

synchronized语句的用法是什么?

回答by mk.

These are used for when you are building a program with many "threads". When main starts, it starts with one thread, which executes the steps in a sequence. You can start many more threads, which can then execute code at the same time. If you're executing the same code at the same time, things might behave in ways you don't want:

这些用于构建具有许多“线程”的程序。当 main 启动时,它从一个线程开始,该线程按顺序执行步骤。您可以启动更多线程,然后这些线程可以同时执行代码。如果您同时执行相同的代码,事情可能会以您不希望的方式运行:

y = x+20;
// at this moment, before the next instruction starts, some other thread performs 
// the above step, which sets 'y' (an object property) to something different.
int b = y+10; // this would not be x+20, as you might expect.

What you want to do is put a 'lock' over this block of code, to make sure that no other thread can start executing any code that is "synchronized on" the variable y.

您想要做的是在此代码块上放置一个“锁”,以确保没有其他线程可以开始执行与变量 y“同步”的任何代码。

synchronized (y) {
    y = x+20;
    int b = y+10;
} // lock gets released here

Now, all other threads have to wait for whichever thread got there first to exit the block and release the lock, at which point another thread grabs the lock, enters the block of code, executes it, and releases the lock. Note that yhas to be an object (Integer), not a primitive type.

现在,所有其他线程必须等待首先到达那里的线程退出块并释放锁,此时另一个线程获取锁,进入代码块,执行它,然后释放锁。请注意,y必须是对象(整数),而不是原始类型。

You can also add 'synchronized' to methods, which synchronizes on 'this' (the instance object), or the class in the case of a static method.

您还可以向方法添加“同步”,在“this”(实例对象)或静态方法中的类上同步。

Writing multi-threaded code is hard, because of problems like this. Synchronization is one tool, though it has one major problem - deadlocks. There is a lot of information online about deadlocks.

由于这样的问题,编写多线程代码很困难。同步是一种工具,但它有一个主要问题——死锁。网上有很多关于死锁的信息。

回答by Alex

It is a java built in form of mutual exclusion. This is used for multithreaded applications.

它是一个以互斥形式构建的java。这用于多线程应用程序。

Sun concurrency tutorial

Sun并发教程

This has a section about synchronized, but you should read the whole thing if you are trying to use multithreaded applications.

这有一个关于同步的部分,但是如果您尝试使用多线程应用程序,您应该阅读整篇文章。

Wiki mutex

维基互斥锁

回答by Lawrence Dol

It creates a section of code which, with respect to two or more threads, can (a) only be executed by one thread at a time, and (b) forms a memory barrier.

它创建一段代码,对于两个或多个线程,该代码段可以 (a) 一次只能由一个线程执行,并且 (b) 形成内存屏障

While understanding the concept of mutual-exclusion preventing concurrent execution of the code is quite easy, equally important is the memory barrier.

虽然理解互斥防止代码并发执行的概念很容易,但同样重要的是内存屏障。

A memory barrier forms a "happens before" relationship between two threads. Any changes to memory made by a thread before acquiring a lock is guaranteed to be observed by another thread after it acquires the same lock. Due to the effects of CPU caches and their interaction with main memory, this is critical to preventing observation and update of stale cached memory and preventing race conditions between threads.

内存屏障在两个线程之间形成“发生在之前”的关系。一个线程在获取锁之前对内存所做的任何更改都保证在另一个线程获取相同的锁之后被另一个线程观察到。由于 CPU 缓存的影响及其与主内存的交互,这对于防止观察和更新陈旧的缓存内存以及防止线程之间的竞争条件至关重要。

回答by Steve B.

Only 1 thread at a time can access a synchronized block.

一次只有 1 个线程可以访问同步块。

This is a basic language construct. If you're not at all familiar with it you'll need to review.

这是一个基本的语言结构。如果您根本不熟悉它,则需要进行复习。

回答by Allam

Invoking a synchronized instance method of an object acquires a lock on the object, and invoking a synchronized static method of a class acquires a lock on the class. A synchronized statement can be used to acquire a lock on any object, not just this object, when executing a block of the code in a method. This block is referred to as a synchronized block. The general form of a synchronized statement is as follows: ? synchronized (expr) { statements; }? The expression expr must evaluate to an object reference. If the object is already locked by another thread, the thread is blocked until the lock is released. When a lock is obtained on the object, the statements in the synchronized block are executed, and then the lock is released.

调用对象的同步实例方法获取对象锁,调用类的同步静态方法获取类锁。当执行方法中的代码块时,synchronized 语句可用于获取对任何对象的锁定,而不仅仅是此对象。该块称为同步块。同步语句的一般形式如下: synchronized (expr) { statements; }? 表达式 expr 必须评估为对象引用。如果该对象已被另一个线程锁定,则该线程将被阻塞,直到锁定被释放。当对象获得锁时,同步块中的语句被执行,然后锁被释放。