您的位置:首页 > 编程语言 > Java开发

The Java™ Tutorials — Concurrency :Synchronization 同步

2016-02-11 17:16 477 查看


The Java™ Tutorials — Concurrency :Synchronization 同步

原文地址:https://docs.oracle.com/javase/tutorial/essential/concurrency/sync.html


关键点

同步目的:防止线程冲突以及内存一致性错误
线程冲突的两种形式:饥饿与死锁
内存一致性错误:对共享数据的不一致操作


全文翻译

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. Starvation
and livelock are forms of thread contention. See the section Liveness for more information.

然而,同步可能导致线程冲突。当两个以及两个以上的线程同时尝试获取同种资源时,就会发生线程冲突。同步会引起Java运行时更加缓慢地执行多个线程,甚至会引起执行的阻塞。饥饿和死锁是线程冲突的两种形式。等多信息请参阅《活跃度》章节。

This section covers the following topics:
Thread Interference describes how errors are introduced when multiple threads access shared data.
Memory Consistency Errors describes errors that result from inconsistent views of shared memory.
Synchronized Methods describes a simple idiom that can effectively prevent thread interference and memory consistency errors.
Implicit Locks and Synchronization describes a more general synchronization idiom, and describes how synchronization is based on implicit locks.
Atomic Access talks about the general idea of operations that can’t be interfered with by other threads.

本章节讨论了下面几个话题:
《线程冲突》描述了多个线程获取共享数据时,错误是怎样发送的
《内存一致性错误》描述了对共享数据的不一致访问所引发的后果
《同步方法》描述了一种可以阻止线程冲突和内存一致性错误的傻瓜式方法
《隐式锁和同步》描述了一种更加常见的同步手段,并描述了同步是如何基于隐式锁工作的
《原子操作》描述了对无法被其他线程中断的操作的概述。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息