Documentation

The Java™ Tutorials
Hide TOC
Synchronization同步
Trail: Essential Java Classes
Lesson: Concurrency

Synchronization同步

Threads communicate primarily by sharing access to fields and the objects reference fields refer to. 线程主要通过共享对字段和引用字段引用的对象的访问来进行通信。This form of communication is extremely efficient, but makes two kinds of errors possible: thread interference and memory consistency errors. 这种形式的通信非常有效,但可能导致两种错误:线程干扰内存一致性错误The tool needed to prevent these errors is synchronization.防止这些错误所需的工具是同步

However, synchronization can introduce thread contention, which occurs when two or more threads try to access the same resource simultaneously and cause the Java runtime to execute one or more threads more slowly, or even suspend their execution. 但是,同步可能会引入线程争用,当两个或多个线程试图同时访问同一资源时,会发生争用,并导致Java运行时执行一个或多个线程的速度变慢,甚至暂停它们的执行。Starvation and livelock are forms of thread contention. 饥饿和活锁是线程争用的两种形式。See the section Liveness for more information.有关更多信息,请参阅活性一节。

This section covers the following topics:本节涵盖以下主题:


Previous page: The SimpleThreads Example
Next page: Thread Interference