“同步”是什么意思? 意思是?
我对 synchronized
关键字的用法和意义有一些疑问。
synchronized
关键字有何意义?- 方法何时应该
同步
? - 从程序上和逻辑上来说这意味着什么?
I have some questions regarding the usage and significance of the synchronized
keyword.
- What is the significance of the
synchronized
keyword? - When should methods be
synchronized
? - What does it mean programmatically and logically?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
synchronized
关键字是关于不同的线程读取和写入相同的变量、对象和资源。 这在 Java 中不是一个简单的话题,但这里引用了 Sun 的一句话:简而言之:当您有两个线程正在读取和写入同一“资源”(例如名为
foo
的变量)时,您需要确保这些线程以原子方式访问变量。 如果没有synchronized
关键字,线程 1 可能看不到线程 2 对foo
所做的更改,或者更糟糕的是,它可能只更改了一半。 这不是你逻辑上所期望的。再说一次,这在 Java 中是一个不平凡的话题。 要了解更多信息,请探索 SO 和 Interwebs 上的主题:
继续探索这些主题,直到名称“Brian Goetz” 在你的大脑中永久地与术语“并发”联系在一起。
The
synchronized
keyword is all about different threads reading and writing to the same variables, objects and resources. This is not a trivial topic in Java, but here is a quote from Sun:In a very, very small nutshell: When you have two threads that are reading and writing to the same 'resource', say a variable named
foo
, you need to ensure that these threads access the variable in an atomic way. Without thesynchronized
keyword, your thread 1 may not see the change thread 2 made tofoo
, or worse, it may only be half changed. This would not be what you logically expect.Again, this is a non-trivial topic in Java. To learn more, explore topics here on SO and the Interwebs about:
Keep exploring these topics until the name "Brian Goetz" becomes permanently associated with the term "concurrency" in your brain.
好吧,我认为我们已经有了足够的理论解释,所以考虑一下这段代码。
注意:只要前一个线程的执行尚未完成,
synchronized
就会阻止下一个线程对方法 test() 的调用。 线程一次只能访问此方法。 如果没有synchronized
,所有线程都可以同时访问此方法。当线程调用对象的同步方法“test”时(这里对象是“TheDemo”类的实例),它会获取该对象的锁,任何新线程都不能调用同一对象的任何同步方法,只要前一个线程获得锁的对象不会释放锁。
当调用类的任何静态同步方法时,都会发生类似的情况。 线程获取与该类关联的锁(在这种情况下,该类实例的任何非静态同步方法都可以由任何线程调用,因为该对象级锁仍然可用)。 只要当前持有锁的线程未释放类级别锁,任何其他线程都将无法调用该类的任何静态同步方法。
同步输出
不同步输出
Well, I think we had enough of theoretical explanations, so consider this code
Note:
synchronized
blocks the next thread's call to method test() as long as the previous thread's execution is not finished. Threads can access this method one at a time. Withoutsynchronized
all threads can access this method simultaneously.When a thread calls the synchronized method 'test' of the object (here object is an instance of 'TheDemo' class) it acquires the lock of that object, any new thread cannot call ANY synchronized method of the same object as long as previous thread which had acquired the lock does not release the lock.
Similar thing happens when any static synchronized method of the class is called. The thread acquires the lock associated with the class(in this case any non static synchronized method of an instance of that class can be called by any thread because that object level lock is still available). Any other thread will not be able to call any static synchronized method of the class as long as the class level lock is not released by the thread which currently holds the lock.
Output with synchronised
Output without synchronized
synchronized
关键字可防止多个线程同时访问代码块或对象。Hashtable
的所有方法都是同步的
,因此一次只有一个线程可以执行其中任何一个方法。当使用非
同步
构造(例如HashMap
)时,您必须在代码中构建线程安全功能以防止一致性错误。The
synchronized
keyword prevents concurrent access to a block of code or object by multiple threads. All the methods ofHashtable
aresynchronized
, so only one thread can execute any of them at a time.When using non-
synchronized
constructs likeHashMap
, you must build thread-safety features in your code to prevent consistency errors.synchronized
表示在多线程环境中,具有synchronized
方法/块的对象不允许两个线程访问synchronized
代码> 同时代码的方法/块。 这意味着一个线程在另一个线程更新它时无法读取它。第二个线程将等待,直到第一个线程完成其执行。 开销是速度,但优点是保证数据的一致性。
如果您的应用程序是单线程的,则
synchronized
块不会提供任何好处。synchronized
means that in a multi threaded environment, an object havingsynchronized
method(s)/block(s) does not let two threads to access thesynchronized
method(s)/block(s) of code at the same time. This means that one thread can't read while another thread updates it.The second thread will instead wait until the first thread completes its execution. The overhead is speed, but the advantage is guaranteed consistency of data.
If your application is single threaded though,
synchronized
blocks does not provide benefits.synchronized
关键字使线程在进入方法时获得锁,这样同一时间只有一个线程可以执行该方法(对于给定的对象实例,除非是静态方法)。这通常被称为使类线程安全,但我想说这是一种委婉的说法。 虽然同步确实可以保护 Vector 的内部状态不被损坏,但这通常对 Vector 的用户没有多大帮助。
考虑一下:
尽管所涉及的方法是同步的,但由于它们是单独锁定和解锁的,两个不幸的定时线程可以创建一个具有两个元素的向量。
因此,实际上,您还必须在应用程序代码中进行同步。
因为方法级同步 a) 当您不需要同步时成本高昂,b) 当您需要同步时不足,因此现在有非同步的替代方案(在 Vector 的情况下为 ArrayList)。
最近,并发包已经发布,其中有许多巧妙的实用程序可以解决多线程问题。
The
synchronized
keyword causes a thread to obtain a lock when entering the method, so that only one thread can execute the method at the same time (for the given object instance, unless it is a static method).This is frequently called making the class thread-safe, but I would say this is a euphemism. While it is true that synchronization protects the internal state of the Vector from getting corrupted, this does not usually help the user of Vector much.
Consider this:
Even though the methods involved are synchronized, because they are being locked and unlocked individually, two unfortunately timed threads can create a vector with two elements.
So in effect, you have to synchronize in your application code as well.
Because method-level synchronization is a) expensive when you don't need it and b) insufficient when you need synchronization, there are now un-synchronized replacements (ArrayList in the case of Vector).
More recently, the concurrency package has been released, with a number of clever utilities that take care of multi-threading issues.
概述
Java 中的 Synchronized 关键字与线程安全有关,即多个线程读取或写入同一个变量时。
这可以直接发生(通过访问相同的变量)或间接发生(通过使用一个类,该类使用另一个访问相同变量的类)。
synchronized 关键字用于定义一个代码块,其中多个线程可以以安全的方式访问同一个变量。
更深入的
语法方面,
synchronized
关键字采用一个Object
作为参数(称为锁定对象),然后后面跟着一个{代码块}
。当执行过程中遇到此关键字时,当前线程会尝试“锁定/获取/拥有”(随意选择)锁定对象,并在获取锁定后执行关联的代码块。
一次只有一个线程可以持有锁,在此期间所有其他尝试获取同一锁对象的线程都将等待(暂停执行)。 当执行退出同步代码块时,锁将被释放。
同步方法:
在方法定义中添加
synchronized
关键字等于将整个方法体包装在同步代码块中,锁定对象为this
(对于实例方法) 和ClassInQuestion.getClass()
(对于类方法)。- 实例方法是没有
static
关键字的方法。- 类方法是具有
static
关键字的方法。技术
如果没有同步,则无法保证读取和写入发生的顺序,可能会导致变量留下垃圾。< br>
(例如,一个变量可能最终有一半的位由一个线程写入,一半的位由另一个线程写入,使该变量处于两个线程都未尝试写入的状态,但混合在一起的混乱两者。)
在另一个线程读取之前(挂钟时间)在线程中完成写入操作是不够的,因为硬件可能已经缓存了变量的值,并且读取线程将看到缓存的值值而不是写入的内容。
结论
因此,在 Java 的情况下,您必须遵循 Java 内存模型以确保不会发生线程错误。
换句话说:使用同步、原子操作或在幕后为您使用它们的类。
Overview
Synchronized keyword in Java has to do with thread-safety, that is, when multiple threads read or write the same variable.
This can happen directly (by accessing the same variable) or indirectly (by using a class that uses another class that accesses the same variable).
The synchronized keyword is used to define a block of code where multiple threads can access the same variable in a safe way.
Deeper
Syntax-wise the
synchronized
keyword takes anObject
as it's parameter (called a lock object), which is then followed by a{ block of code }
.When execution encounters this keyword, the current thread tries to "lock/acquire/own" (take your pick) the lock object and execute the associated block of code after the lock has been acquired.
Any writes to variables inside the synchronized code block are guaranteed to be visible to every other thread that similarly executes code inside a synchronized code block using the same lock object.
Only one thread at a time can hold the lock, during which time all other threads trying to acquire the same lock object will wait (pause their execution). The lock will be released when execution exits the synchronized code block.
Synchronized methods:
Adding
synchronized
keyword to a method definition is equal to the entire method body being wrapped in a synchronized code block with the lock object beingthis
(for instance methods) andClassInQuestion.getClass()
(for class methods).- Instance method is a method which does not have
static
keyword.- Class method is a method which has
static
keyword.Technical
Without synchronization, it is not guaranteed in which order the reads and writes happen, possibly leaving the variable with garbage.
(For example a variable could end up with half of the bits written by one thread and half of the bits written by another thread, leaving the variable in a state that neither of the threads tried to write, but a combined mess of both.)
It is not enough to complete a write operation in a thread before (wall-clock time) another thread reads it, because hardware could have cached the value of the variable, and the reading thread would see the cached value instead of what was written to it.
Conclusion
Thus in Java's case, you have to follow the Java Memory Model to ensure that threading errors do not happen.
In other words: Use synchronization, atomic operations or classes that use them for you under the hoods.
可以把它想象成一种十字转门,就像你在足球场上看到的那样。 有平行的人流想要进去,但在十字转门处他们是“同步”的。 一次只有一个人可以通过。 所有想要通过的人都可以,但他们可能必须等到他们能够通过为止。
Think of it as a kind of turnstile like you might find at a football ground. There are parallel steams of people wanting to get in but at the turnstile they are 'synchronised'. Only one person at a time can get through. All those wanting to get through will do, but they may have to wait until they can go through.
1. 什么是synchronized关键字?
同步块或方法可以防止线程干扰并确保数据一致。 在任何时间点,只有一个线程可以通过获取锁来访问同步块或方法(临界区)。 其他线程将等待锁释放才能访问关键部分。
2. 方法何时同步?
当您将
synchronized
添加到方法定义或声明时,方法就会同步。 您还可以在方法中同步特定的代码块。3. 从编程和逻辑上讲这是什么意思?
这意味着只有一个线程可以通过获取锁来访问临界区。 除非该线程释放该锁,否则所有其他线程都必须等待获取锁。 如果不获取锁,他们就无法进入关键部分。
程序员有责任识别应用程序中的关键部分并相应地对其进行保护。 Java提供了一个框架来保护你的应用程序,但是保护哪些部分是程序员的责任。
使方法同步有两个效果:
这保证了对象状态的更改对所有线程都是可见的。
在以下位置寻找同步的其他替代方法:
避免在 Java 中同步(this)?
1. What is the synchronized keyword?
Synchronized blocks or methods prevents thread interference and make sure that data is consistent. At any point of time, only one thread can access a synchronized block or method (critical section) by acquiring a lock. Other thread(s) will wait for release of lock to access critical section.
2. When are methods synchronized?
Methods are synchronized when you add
synchronized
to method definition or declaration. You can also synchronize a particular block of code with-in a method.3. What does it mean pro grammatically and logically?
It means that only one thread can access critical section by acquiring a lock. Unless this thread release this lock, all other thread(s) will have to wait to acquire a lock. They don't have access to enter critical section with out acquiring lock.
It's programmer responsibility to identify critical section(s) in application and guard it accordingly. Java provides a framework to guard your application, but which sections to be guarded is the responsibility of programmer.
Making methods synchronized has two effects:
This guarantees that changes to the state of the object are visible to all threads.
Look for other alternatives to synchronization in :
Avoid synchronized(this) in Java?
同步普通方法
相当于Synchronized 语句
(使用此)Synchronized 静态方法
等价于Synchronized 语句
(使用类)Synchronized 语句(使用变量)
For
synchronized< /code>,我们有
同步方法
和同步语句
。 不过,同步方法
与同步语句
类似,因此我们只需要了解同步语句
即可。=> 基本上,我们认为
这 2 点有助于理解
synchronized
内在锁
。同步语句
时,它会自动获取该同步语句
对象的内在锁
,并在方法返回时释放它。 只要一个线程拥有内在锁
,没有其他线程就可以获取SAME锁=> 线程安全。=>
当
线程A
调用synchronized(this){//代码1}
=>; 所有具有synchronized(this)
的块代码(在类内部)和所有同步普通方法
(在类内部)都被锁定,因为 SAME 锁。 它将在线程 A
解锁后执行(“// code 1”完成)。此行为类似于
synchronized(a variable){// code 1}
或synchronized(class)
。相同的锁 => 锁(不依赖哪个方法?或者哪个语句?)
使用同步方法还是同步语句?
我更喜欢
同步语句
,因为它更具可扩展性。 例如,将来您只需要同步方法的一部分。 例如,您有 2 个同步方法,并且它们彼此没有任何相关,但是当线程运行一个方法时,它将阻塞另一个方法(它可以通过使用synchronized 来阻止(变量)
)。不过,apply Synchronized方法很简单,代码看起来也很简单。 对于某个类,只有1个同步方法,或者该类中的所有同步方法彼此相关=> 我们可以使用
synchronized方法
使代码更短且易于理解注意
(它与
synchronized
没有太大关系,它是对象和类或非静态和非静态之间的区别静止的)。synchronized
或普通方法或synchronized(this)
或synchronized(non-staticvariable)
时,它将基于每个对象实例进行同步。synchronized
或静态方法或synchronized(class)
或synchronized(static variable)
时,它将基于类参考
进行同步https://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html
https://docs.oracle.com/javase/tutorial/essential/concurrency /locksync.html
希望有帮助
Synchronized normal method
equivalent toSynchronized statement
(use this)Synchronized static method
equivalent toSynchronized statement
(use class)Synchronized statement (using variable)
For
synchronized
, we have bothSynchronized Methods
andSynchronized Statements
. However,Synchronized Methods
is similar toSynchronized Statements
so we just need to understandSynchronized Statements
.=> Basically, we will have
Here is 2 think that help understanding
synchronized
intrinsic lock
associated with it.synchronized statement
, it automatically acquires theintrinsic lock
for thatsynchronized statement's
object and releases it when the method returns. As long as a thread owns anintrinsic lock
, NO other thread can acquire the SAME lock => thread safe.=>
When a
thread A
invokessynchronized(this){// code 1}
=> all the block code (inside class) where havesynchronized(this)
and allsynchronized normal method
(inside class) is locked because SAME lock. It will execute afterthread A
unlock ("// code 1" finished).This behavior is similar to
synchronized(a variable){// code 1}
orsynchronized(class)
.SAME LOCK => lock (not depend on which method? or which statements?)
Use synchronized method or synchronized statements?
I prefer
synchronized statements
because it is more extendable. Example, in future, you only need synchronized a part of method. Example, you have 2 synchronized method and it don't have any relevant to each other, however when a thread run a method, it will block the other method (it can prevent by usesynchronized(a variable)
).However, apply synchronized method is simple and the code look simple. For some class, there only 1 synchronized method, or all synchronized methods in the class in relevant to each other => we can use
synchronized method
to make code shorter and easy to understandNote
(it not relevant to much to
synchronized
, it is the different between object and class or none-static and static).synchronized
or normal method orsynchronized(this)
orsynchronized(non-static variable)
it will synchronized base on each object instance.synchronized
or static method orsynchronized(class)
orsynchronized(static variable)
it will synchronized base on classReference
https://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html
https://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html
Hope it help
以下是来自Java 教程的解释。
考虑以下代码:
Here is an explanation from The Java Tutorials.
Consider the following code:
据我了解,同步基本上意味着编译器在您的方法周围编写一个monitor.enter和monitor.exit。 因此,它可能是线程安全的,具体取决于它的使用方式(我的意思是,您可以使用同步方法编写一个对象,但该对象不是线程安全的,具体取决于您的类的用途)。
To my understanding synchronized basically means that the compiler write a monitor.enter and monitor.exit around your method. As such it may be thread safe depending on how it is used (what I mean is you can write an object with synchronized methods that isn't threadsafe depending on what your class does).
其他答案缺少的是一个重要方面:内存屏障。 线程同步基本上由两个部分组成:序列化和可见性。 我建议大家去谷歌搜索“jvm内存屏障”,因为这是一个不平凡且极其重要的主题(如果您修改多个线程访问的共享数据)。 完成此操作后,我建议查看 java.util.concurrent 包的类,它们有助于避免使用显式同步,这反过来又有助于保持程序简单高效,甚至可能防止死锁。
其中一个示例是 ConcurrentLinkedDeque。 与命令模式一起,它允许通过将命令填充到并发队列中来创建高效的工作线程-- 不需要显式同步,不会出现死锁,不需要显式 sleep(),只需通过调用 take() 轮询队列即可。
简而言之:当您启动线程、线程结束、读取易失性变量、解锁监视器(留下同步块/函数)等时,“内存同步”会隐式发生。这种“同步”影响(在某种意义上“刷新”)所有在该特定操作之前完成的写入。 对于上述 ConcurrentLinkedDeque ,文档“说”:
这种隐式行为是一个有点有害的方面,因为大多数没有太多经验的 Java 程序员只会因此而采取很多既定的做法。 然后,在 Java 没有执行它在生产环境中“应该”执行的操作(其中存在不同的工作负载)之后,突然遇到了这个线程,而且测试并发问题非常困难。
What the other answers are missing is one important aspect: memory barriers. Thread synchronization basically consists of two parts: serialization and visibility. I advise everyone to google for "jvm memory barrier", as it is a non-trivial and extremely important topic (if you modify shared data accessed by multiple threads). Having done that, I advise looking at java.util.concurrent package's classes that help to avoid using explicit synchronization, which in turn helps keeping programs simple and efficient, maybe even preventing deadlocks.
One such example is ConcurrentLinkedDeque. Together with the command pattern it allows to create highly efficient worker threads by stuffing the commands into the concurrent queue -- no explicit synchronization needed, no deadlocks possible, no explicit sleep() necessary, just poll the queue by calling take().
In short: "memory synchronization" happens implicitly when you start a thread, a thread ends, you read a volatile variable, you unlock a monitor (leave a synchronized block/function) etc. This "synchronization" affects (in a sense "flushes") all writes done before that particular action. In the case of the aforementioned ConcurrentLinkedDeque, the documentation "says":
This implicit behavior is a somewhat pernicious aspect because most Java programmers without much experience will just take a lot as given because of it. And then suddenly stumble over this thread after Java isn't doing what it is "supposed" to do in production where there is a different work load -- and it's pretty hard to test concurrency issues.
同步仅仅意味着如果在特定对象上使用同步块,则与单个对象关联的多个线程可以防止脏读和脏写。 为了让您更清楚,让我们举个例子:
我们创建了两个 MyRunnable 类对象,runnable1 与线程 1 和线程 3 共享。 runnable2 仅与线程 2 共享。
现在,当 t1 和 t3 在没有使用同步的情况下启动时,PFB 输出表明线程 1 和 3 同时影响 var 值,其中对于线程 2 来说,var 有自己的内存。
使用 Synchronzied,线程 3 在所有情况下都会等待线程 1 完成。 获取了两个锁,一个在由线程 1 和线程 3 共享的 runnable1 上,另一个在仅由线程 2 共享的 runnable2 上。
Synchronized simply means that multiple threads if associated with single object can prevent dirty read and write if synchronized block is used on particular object. To give you more clarity , lets take an example :
We've created two MyRunnable class objects , runnable1 being shared with thread 1 and thread 3 & runnable2 being shared with thread 2 only.
Now when t1 and t3 starts without synchronized being used , PFB output which suggest that both threads 1 and 3 simultaneously affecting var value where for thread 2 , var has its own memory.
Using Synchronzied, thread 3 waiting for thread 1 to complete in all scenarios. There are two locks acquired , one on runnable1 shared by thread 1 and thread 3 and another on runnable2 shared by thread 2 only.
在java中为了防止多个线程操作共享变量,我们使用
synchronized
关键字。 让我们借助以下示例来理解它:在示例中,我定义了两个线程并将它们命名为增量和减量。 增量线程增加共享变量(
counter
)的值,增量与减量线程减少的量相同,即增加 5000 倍(结果为 5000 + 0 = 5000),减少 5000 倍(结果为 5000 + 0 = 5000)。结果为 5000 - 5000 = 0)。没有
synchronized
关键字的程序:如果我们运行上述程序,我们期望缓冲区的值相同,因为以相同的量递增和递减缓冲区将导致我们开始时的初始值正确的 ?。 让我们看看输出:
正如您所看到的,无论我们运行程序多少次,我们都会得到不同的结果,原因是每个线程同时操作
计数器
。 如果我们能够设法让一个线程首先递增共享变量,然后第二次递减它,反之亦然,那么我们将获得正确的结果,这正是使用synchronized
关键字可以完成的结果在Buffer
的inc
和dec
方法之前添加synchronized
关键字,如下所示:Program with
synchronized
关键字:和输出:
无论我们运行多少次,我们都会得到与 0 相同的输出
In java to prevent multiple threads manipulating a shared variable we use
synchronized
keyword. Lets understand it with help of the following example:In the example I have defined two threads and named them increment and decrement. Increment thread increases the value of shared variable (
counter
) by the same amount the decrement thread decreases it i.e 5000 times it is increased (which result in 5000 + 0 = 5000) and 5000 times we decrease (which result in 5000 - 5000 = 0).Program without
synchronized
keyword:If we run the above program we expect value of buffer to be same since incrementing and decrementing the buffer by same amount would result in initial value we started with right ?. Lets see the output:
As you can see no matter how many times we run the program we get a different result reason being each thread manipulated the
counter
at the same time. If we could manage to let the one thread to first increment the shared variable and then second to decrement it or vice versa we will then get the right result that is exactly what can be done withsynchronized
keyword by just addingsynchronized
keyword before theinc
anddec
methods ofBuffer
like this:Program with
synchronized
keyword:and the output:
no matter how many times we run it we get the same output as 0
Java同步
易失性
[关于] => Java中的synchronized
synchronized
块是多线程中的监视器。 具有相同对象/类的同步块只能由单个线程执行,所有其他线程都在等待。 它可以帮助解决竞争条件
[关于]情况。Java 5
通过支持happens-before
扩展了synchronized
[关于]下一步是java.util.concurrent
Java synchronized
volatile
[About] =>synchronized
synchronized
block in Java is a monitor in multithreading.synchronized
block with the same object/class can be executed by only single thread, all others are waiting. It can help withrace condition
[About] situation.Java 5
extendedsynchronized
by supportinghappens-before
[About]The next step is
java.util.concurrent
同步简单意味着没有两个线程可以同时访问块/方法。 当我们说类的任何块/方法是同步的时,这意味着一次只有一个线程可以访问它们。 在内部,尝试访问它的线程首先会对该对象加锁,只要该锁不可用,其他线程就无法访问该类实例的任何同步方法/块。
请注意,另一个线程可以访问未定义为同步的同一对象的方法。 线程可以通过调用来释放锁
synchronized simple means no two threads can access the block/method simultaneously. When we say any block/method of a class is synchronized it means only one thread can access them at a time. Internally the thread which tries to access it first take a lock on that object and as long as this lock is not available no other thread can access any of the synchronized methods/blocks of that instance of the class.
Note another thread can access a method of the same object which is not defined to be synchronized. A thread can release the lock by calling
“synchronized”关键字是为了实现线程安全。 它将同步块或方法的访问限制为一次只能有一个线程,有效地防止多线程程序中的数据不一致。
何时使用方法同步
在处理数据结构等共享资源时,方法同步就变得很有必要。 它可以防止数据损坏,特别是在一次只能运行一个线程的关键部分。 对于用于多线程使用的类来说,维护线程安全也至关重要。
理解编程和逻辑含义
从编程的角度来看,Java 的同步确保只有一个线程可以通过获取和释放锁来访问同步代码。 从逻辑上讲,它在多线程环境中强制执行数据一致性和完整性。
The "synchronized" keyword is for achieving thread safety. It restricts access to a synchronized block or method to just one thread at a time, effectively preventing data inconsistencies in multi-threaded programs.
When to Use Method Synchronization
Method synchronization becomes necessary when dealing with shared resources such as data structures. It safeguards against data corruption, especially in critical sections where only one thread should operate at a time. It's also vital in classes intended for multi-threaded use to maintain thread safety.
Understanding the Programmatic and Logical Implications
From a programmatic perspective, Java's synchronization ensures that only one thread can access synchronized code by acquiring and releasing locks. Logically, it enforces data consistency and integrity in multi-threaded environments.
Synchronized是Java中的一个关键字,用于在多线程环境中使关系发生在之前,以避免内存不一致和线程干扰错误。
synchronized is a keyword in Java which is used to make happens before relationship in multithreading environment to avoid memory inconsistency and thread interference error.