`
poethelasi
  • 浏览: 6074 次
文章分类
社区版块
存档分类
最新评论

Java语言规范 第十七章 线程和锁(一)

阅读更多

 

While most of the discussion in the preceding chapters is concerned only with the behavior of code as executed a single statement or expression at a time, that is, by a single thread, the Java Virtual Machine can support many threads of execution at once. These threads independently execute code that operates on values and objects residing in a shared main memory. Threads may be supported by having many hardware processors, by time-slicing a single hardware processor, or by time-slicing many hardware processors. 
 
虽然前面章节大多数的讨论仅涉及到同一时间只有单个语句或表达式执行的行为,也就是说单线程(程序);但是,JVM(Java虚拟机)支持同一时间执行多个线程。这些线程单独地执行各自的代码、操作分配在共享主内存中的值(values)和对象(objects)。多线程可以被多个硬件处理器,或者单硬件处理器时间碎片,或者多硬件处理器时间碎片支持。
 
Threads are represented by the Thread class. The only way for a user to create a thread is to create an object of this class; each thread is associated with such an object. A thread will start when the start() method is invoked on the corresponding Thread object.
 
线程是通过Thread类来表示。对用户来说,创建一个线程的唯一方式就是创建一个Thread的对象;每一个线程与一个Thread对象关联。当调用相应线程对象的start() 方法时,该线程将被启动。
 
The behavior of threads, particularly when not correctly synchronized, can be confusing and counterintuitive. This chapter describes the semantics of multithreaded programs; it includes rules for which values may be seen by a read of shared memory that is updated by multiple threads. As the specification is similar to the memory models for different hardware architectures, these semantics are known as the Java programming language memory model. When no confusion can arise, we will simply refer to these rules as "the memory model". 
 
多线程的行为,尤其是当没有正确同步,可能会造成混淆和有悖常理。本章介绍了多线程程序的语义;它包含的规则,保证对被多个线程更新的共享内存中的值的读操作是可见的。如同该规范与不同硬件架构体系的内存模型类似一样,这些语义以Java语言内存模型的概念而被熟知。在不引起混淆的情况下,我们简单的将这些规则称作“内存模型”。
 
These semantics do not prescribe how a multithreaded program should be executed. Rather, they describe the behaviors that multithreaded programs are allowed to exhibit. Any execution strategy that generates only allowed behaviors is an acceptable execution strategy. 
 
这些语义没有规定多线程程序应该如何被执行。相反,它们描述了多线程程序被允许表现的行为。不管什么执行策略,只有生成被允许行为的策略才是一个可接受的执行策略。
 

17.1. Synchronization 同步

The Java programming language provides multiple mechanisms for communicating between threads. The most basic of these methods is synchronization, which is implemented using monitors. Each object in Java is associated with a monitor, which a thread can lock or unlock. Only one thread at a time may hold a lock on a monitor. Any other threads attempting to lock that monitor are blocked until they can obtain a lock on that monitor. A thread t may lock a particular monitor multiple times; each unlock reverses the effect of one lock operation.
 
Java 编程语言为线程间通信提供了多种机制。这些方法最基本的同步,它是通过监视器来实现的。Java 中的任何一个对象与一个监视器关联,线程可以对该监视器进行加锁和解锁操作。同一时间在同一个监视器上只有一个线程可以持有一个锁。任何其他想获得该监视器锁的线程将一直被阻塞,直到它们获得了该监视器的一个锁。一个线程t可能锁定一个特殊的监视器多次,每一次解锁反转一个锁定操作的效果。
 
The synchronized statement (§14.19) computes a reference to an object; it then attempts to perform a lock action on that object's monitor and does not proceed further until the lock action has successfully completed. After the lock action has been performed, the body of the synchronized statement is executed. If execution of the body is ever completed, either normally or abruptly, an unlock action is automatically performed on that same monitor. 
 
该 synchronized语句在一个对象上计算一次引用;然后在该对象的监视器上尝试执行一个加锁操作,这时线程不会继续执行,直到锁操作成功完成。锁操作执行后,同步语句块将被执行。如果执行块完成,不管正常地或是意外地,在该监视器上一个解锁操作都会自动执行。
 
A synchronized method (§8.4.3.6) automatically performs a lock action when it is invoked; its body is not executed until the lock action has successfully completed. If the method is an instance method, it locks the monitor associated with the instance for which it was invoked (that is, the object that will be known as this during execution of the body of the method). If the method is static, it locks the monitor associated with the Class object that represents the class in which the method is defined. If execution of the method's body is ever completed, either normally or abruptly, an unlock action is automatically performed on that same monitor. 
 
一个同步方法自动执行一个锁操作当它被调用时;方法体不会被执行,直到锁操作已经成功完成。如果是实例同步方法,那么它将锁定与监视器关联的实例,该实例就是拥有该被调用的同步方法的实例。(也就是说,在方法体执行期间,该对象是以this表示而知道的)。如果该方法是一个静态实例方法,它锁定的值该监视器关联的Class对象,该Class对象表示该静态同步方法在其定义的class。如果方法体的执行完成,不管是正常地或异常地,一个解锁操作都将自动在该监视器上执行。
 
The Java programming language neither prevents nor requires detection of deadlock conditions. Programs where threads hold (directly or indirectly) locks on multiple objects should use conventional techniques for deadlock avoidance, creating higher-level locking primitives that do not deadlock, if necessary. 
 
Java 编程语言既不防止也不要求对死锁条件进行检测。有多个线程在多个对象上持有(直接或间接)锁的程序应该使用惯用的技术避免死锁,如果有必要,创建高级的不会死锁的锁原语。
 
Other mechanisms, such as reads and writes of volatile variables and the use of classes in the java.util.concurrent package, provide alternative ways of synchronization. 
 
其他机制提供了可选的同步方法,比如volatile变量的读写和java.util.concurrent包中类的使用。

 

分享到:
评论

相关推荐

    Java语言程序设计-原书第10版-所有课后答案及源代码(不分单双)

    《Java语言程序设计-原书第10版》是一本深度探讨Java编程的权威书籍,其涵盖的内容广泛且深入,适合初学者以及有一定基础的开发者。第10版的更新可能涉及了Java 10的新特性,使得学习者能够紧跟Java语言的发展步伐。...

    JAVA语言中文教程

    《JAVA语言中文教程》是一份详尽的编程学习资料,主要介绍了JAVA语言的基础知识与面向对象的编程概念,适合初学者以及有一定编程基础的学习者深入理解JAVA语言的特性和编程技巧。 ### 第一章:JAVA概述 #### JAVA...

    Java编程语言详细教程

    ★ 第一讲 Java语言概述 ◇课前索引 ◇1.1 java语言的发展史 ◇1.2 java的工作原理 ◇1.3 一切都是对象 ◇1.4 构建java程序 ◇1.5 java程序规范 ◇1.6 建立java开发环境 ◇本讲小结 ◇课后习题 ★ 第二讲...

    Java语言程序设计学习笔记

    Java语言程序设计学习笔记是为初学者和有一定基础的开发者准备的一份详尽教程,它涵盖了从基础到进阶的各个重要知识点。这份笔记以Markdown(md)文件的形式组织,便于阅读和检索,使得学习过程更为高效。 首先,...

    Java 内存模型

    该规范修订了Java语言规范第二版第17章和Java虚拟机规范第二版第8章中关于内存模型和线程的描述。最终JSR-133规范将直接纳入修订后的Java语言规范中。 Java内存模型的核心内容涵盖了锁、线程间的交互、内存可见性和...

    Thinking in Java 中文第四版+习题答案

    第17章 项目 17.1 文字处理 17.1.1 提取代码列表 17.1.2 检查大小写样式 17.2 方法查找工具 17.3 复杂性理论 17.4 总结 17.5 练习 附录A 使用非Java代码 A.1 Java固有接口 A.1.1 调用固有方法 A.1.2 访问JNI函数:...

    JAVA 教学课件(共12章-全)

    11. **第十一章:多线程** - Java支持多线程编程,这一章会讲解线程的创建、同步、通信以及死锁问题,这对于开发高效并发应用程序至关重要。 12. **第十二章**:由于未提供具体章节名,可能涉及反射、I/O流、网络...

    java经典教材笔记

    第十二章讲述了Java中的类集(Collection)框架,它是一组接口和类,用于表示和操作对象集合。类集框架中包括了List、Set、Map等接口,以及ArrayList、LinkedList、HashSet、TreeSet等实现了这些接口的类。 通过...

    清华大学JAVA教程

    通过本门课程的学习可以使学生掌握Java语言的基本语法和编程规范;尤其是掌握用Java语言进行网络编程的技巧;同时Java语言是一门面向对象的语言,通过学习可以掌握用面向对象进行编程的思想和实践,使学生成为一名...

    深入Java虚拟机(原书第2版).pdf【附光盘内容】

    第17章 异常 17.1 异常的抛出与捕获 17.2 异常表 17.3 一个模拟:“play ball!” 17.4 随书光盘 17.5 资源页 第18章 finally子句 18.1 微型子例程 18.2 不对称的调用和返回 18.3 一个模拟:“hop ...

    JAVA清华大学教程

    清华大学java课程学习使用教程 ★ 第一讲 Java语言概述 ◇课前索引 ◇1.1 java语言的发展史 ◇1.2 java的工作原理 ◇1.3 一切都是对象 ◇1.4 构建java程序 ◇1.5 java程序规范 ◇1.6 建立java开发环境 ◇本...

    Java 7 Pocket Guide 2nd Edition

    第二部分“平台”包含第9章到第17章,着重介绍了Java平台的组件及相关主题,这帮助读者理解Java平台的内部构造和工作机制。最后,附录部分涵盖了第三方工具以及统一建模语言(UML)。 在Java编程语言方面,本书首先...

    JAVA 清华大学 教程

    ★ 第一讲 Java语言概述 ◇课前索引 ◇1.1 java语言的发展史 ◇1.2 java的工作原理 ◇1.3 一切都是对象 ◇1.4 构建java程序 ◇1.5 java程序规范 ◇1.6 建立java开发环境 ◇本讲小结 ◇课后习题 ★ 第二讲...

    JAVA基础课程讲义

    第十一章 **JAVA多媒体编程** JAVA支持多媒体处理,如字体、颜色、图形绘制和图像操作。JMF(Java Media Framework)用于音频和视频的处理。 第十二章 **GUI编程之AWT** GUI(图形用户界面)提供了与用户的交互...

    Java核心技术习题答案.doc

    本文件"Java核心技术习题答案.doc"涵盖了Java语言的多个核心知识点,包括语言概述、语法基础、面向对象编程、数组、高级类特性、GUI编程、集合框架、IO流、JDBC数据库访问、线程和网络编程等。 **第一章 Java语言...

Global site tag (gtag.js) - Google Analytics