`
onray
  • 浏览: 6292 次
  • 来自: ...
最近访客 更多访客>>
社区版块
存档分类
最新评论

Object Locking

阅读更多
Object Locking
As mentioned in earlier chapters, some of the Java virtual machine's runtime data areas are shared by all threads, others are private to individual threads. Because the heap and method area are shared by all threads, Java programs need to coordinate multi-threaded access to two kinds of data: o instance variables, which are stored on the heap o class variables, which are stored in the method area Programs never need to coordinate access to local variables, which reside on Java stacks, because data on the Java stack is private to the thread to which the Java stack belongs.

In the Java virtual machine, every object and class is logically associated with a monitor. For objects, the associated monitor protects the object's instance variables. For classes, the monitor protects the class's class variables. If an object has no instance variables, or a class has no class variables, the associated monitor protects no data.

To implement the mutual exclusion capability of monitors, the Java virtual machine associates a lock (sometimes called a mutex) with each object and class. A lock is like a privilege that only one thread can "own" at any one time. Threads need not obtain a lock to access instance or class variables. If a thread does obtain a lock, however, no other thread can obtain a lock on the same data until the thread that owns the lock releases it. (To "lock an object" is to acquire the monitor associated with that object.)

Class locks are actually implemented as object locks. As mentioned in earlier chapters, when the Java virtual machine loads a class file, it creates an instance of class java.lang.Class. When you lock a class, you are actually locking that class's Class object.

A single thread is allowed to lock the same object multiple times. For each object, the Java virtual machine maintains a count of the number of times the object has been locked. An unlocked object has a count of zero. When a thread acquires the lock for the first time, the count is again incremented to one. Each time the thread acquires a lock on the same object, the count is again incremented. (Only the thread that already owns an object's lock is allowed to lock it again. As mentioned previously, no other thread can lock the object until the owning thread releases the lock.) Each time the thread releases the lock, the count is decremented. When the count reaches zero, the lock is released and made available to other threads.

A thread in the Java virtual machine requests a lock when it arrives at the beginning of a monitor region. In Java, there are two kinds of monitor regions: synchronized statements and synchronized methods. (These are described in detail later in this chapter.) Each monitor region in a Java program is associated with an object reference. When a thread arrives at the first instruction in a monitor region, the thread must obtain a lock on the referenced object. The thread is not allowed to execute the code until it obtains the lock. Once it has obtained the lock, the thread enters the block of protected code. When the thread leaves the block, no matter how it leaves the block, it releases the lock on the associated object.

Note that as a Java programmer, you never explicitly lock an object. Object locks are internal to the Java virtual machine. In your Java programs, you identify the monitor regions of your program by writing synchronized statements and methods. As the Java virtual machine runs your program, it automatically locks an object or class every time it encounters a monitor region.

分享到:
评论

相关推荐

    H7 HAL驱动概要DM00392525_ENV2.pdf

    • Object locking mechanism: safe hardware access to prevent multiple spurious accesses to shared resources. • Timeout used for all blocking processes: the timeout can be a simple counter or a time...

    informatica PowerCenter培训文档

    同时,PowerCenter提供了一套完整的安全性框架,包括Repository Privileges、Folder Permissions、Object Locking以及User ID和password管理,确保数据的安全访问和权限控制。 5. 用户管理与权限 用户管理涉及User...

    深入java虚拟机(inside the java virtual machine)

    Object Locking Synchronization Support in the Instruction Set Synchronized Statements Synchronized Methods Coordination Support in Class Object On the CD-ROM The Resources Page Appendix A. ...

    Oracle Locking Survival Guide

    5. **监控和诊断**:定期检查锁定统计信息,使用`V$SESSION_WAIT`、`V$LOCKED_OBJECT`等动态性能视图来识别和解决问题。 总的来说,《Oracle Locking Survival Guide》是一份全面的参考资料,涵盖了Oracle数据库中...

    SQL2008中SQL应用之-锁定(locking) 应用分析

    锁定(Locking)是为了防止在数据库系统中同时进行的多个事务对相同数据进行并发更新,从而避免数据不一致和死锁的情况。SQL Server通过动态管理锁定来实现这一目标,但理解Transact-SQL查询如何影响锁定策略对于...

    微软内部资料-SQL性能优化3

     Analyze the output of blocking scripts and Microsoft® SQL Server™ Profiler to troubleshoot locking and blocking issues.  Formulate hypothesis to resolve locking and blocking issues. ...

    PLSQL.Developer v11.0.4.1774 主程序+ v11中文包+keygen

    Command Window addition for batch locking/unlocking of report files: REPORT LOCKFORALL <path> <password> [R] REPORT LOCKFOROTHERS <path> <password> [R] REPORT UNLOCK <path> <password> [R] REPORT LIST ...

    New NHibernate + Sample Source Part2

    * Support for "conversations" - NHibernate supports long-lived persistence contexts, detach/reattach of objects, and takes care of optimistic locking automatically * Free/open source - NHibernate ...

    New NHibernate + Sample Source Part1

    * Support for "conversations" - NHibernate supports long-lived persistence contexts, detach/reattach of objects, and takes care of optimistic locking automatically * Free/open source - NHibernate ...

    NHibernate 2.0.1 + Movies + Sample Source Part2

    * Support for "conversations" - NHibernate supports long-lived persistence contexts, detach/reattach of objects, and takes care of optimistic locking automatically * Free/open source - NHibernate ...

    oracle解锁

    Oracle中的锁定主要包括行级锁定(Row-Level Locking)、表级锁定(Table-Level Locking)等不同级别。其中,非升级行级锁定(Non-Escalating Row-Level Locking)是一种常用的锁定方式,它可以在不影响其他用户对...

    C#单例模式

    这种双重检查锁定(Double-Check Locking)策略既保证了线程安全性,也避免了不必要的锁操作,提高了效率。 #### 客户端代码示例 客户端可以通过调用`Singleton.GetInstance()`方法获取单例对象,如下所示: ```...

    org.springframework.orm.hibernate4.HibernateOptimisticLockingFailureException

    这个异常通常在乐观锁(Optimistic Locking)机制失败时抛出,意味着在数据库操作过程中,数据的版本信息(version)与预期不符,即在读取和更新之间,数据已被其他事务修改。 乐观锁是一种并发控制策略,它假设在...

    java轻量级ORM实现-jorm (Just ORM)

    This project is based on Java, is a lightweight ORM model. Only concerned about the Object-Relationl Mapping, therefore more ...Support for optimistic locking; A rich set of tools for SQL statements;

    单例设计模式五种例子

    3. 双重检查锁定(Double-Check Locking): 这种模式在懒汉式的基础上优化了性能,减少了同步的范围。代码如下: ```csharp public class Singleton { private static volatile Singleton instance; private ...

    JAVA面试设计模式.ppt

    为解决线程安全问题,可以使用双重检查锁定(Double-Check Locking)或静态内部类实现。 ```java public class Singleton { private static volatile Singleton instance = null; private Singleton() {} ...

    c#单例3种实现

    双重检查锁定(Double-Check Locking) 这种实现方式结合了懒汉式的延迟初始化和饿汉式的线程安全性。 ```csharp public sealed class Singleton { private static volatile Singleton instance; private ...

    resources_text

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成,广泛应用于Web服务和客户端应用中。 结合以上分析,我们可以探讨以下几个IT知识领域: 1. **数据...

    .net 架构师设计模式特训付费版源码一

    3. **双检锁/双重校验锁(DCL,Double-Checked Locking)**:结合了懒汉式和饿汉式的优点,既延迟初始化又保证了线程安全,但实现起来较为复杂。 ```csharp public sealed class Singleton { private static ...

Global site tag (gtag.js) - Google Analytics