`
Amur
  • 浏览: 13394 次
  • 性别: Icon_minigender_1
  • 来自: 火星
最近访客 更多访客>>
社区版块
存档分类
最新评论

JVM note1

    博客分类:
  • Java
 
阅读更多

摘自http://www.javaworld.com/javaworld/jw-07-1997/jw-07-hood.html?page=1

虽然一篇古老的文章,但是受益匪浅。

 

Each thread is allocated its own stack.
Inside the Java virtual machine, each thread is awarded a Java stack, which contains data no other thread can access, including the local variables, parameters, and return values of each method the thread has invoked. The data on the stack is limited to primitive types and object references . In the JVM, it is not possible to place the image of an actual object on the stack. All objects reside on the heap.

 

There is only one heap inside the JVM , and all threads share it. The heap contains nothing but objects. There is no way to place a solitary primitive type or object reference on the heap -- these things must be part of an object. Arrays reside on the heap, including arrays of primitive types, but in Java, arrays are objects too.

 

 

Besides the Java stack and the heap, the other place data may reside in the JVM is the method area, which contains all the class (or static) variables used by the program. The method area is similar to the stack in that it contains only primitive types and object references. Unlike the stack, however, the class variables in the method area are shared by all threads.

As described above, two memory areas in the Java virtual machine contain data shared by all threads. These are:

    * The heap, which contains all objects
    * The method area, which contains all class variables

 

正因为method area是共享的,所以引出了synchronized 的概念。JVM为了能够正确的管理synchronization又引入了lock的概念。所以,一环扣一环,所有概念的出现有它出现的必然性。顺着这种思路看书,也能更好的理解。

 

If multiple threads need to use the same objects or class variables concurrently, their access to the data must be properly managed. Otherwise, the program will have unpredictable behavior.

 

To coordinate shared data access among multiple threads, the Java virtual machine associates a lock with each object and class. A lock is like a privilege that only one thread can "possess" at any one time. If a thread wants to lock a particular object or class, it asks the JVM. At some point after the thread asks the JVM for a lock -- maybe very soon, maybe later, possibly never -- the JVM gives the lock to the thread. When the thread no longer needs the lock, it returns it to the JVM. If another thread has requested the same lock, the JVM passes the lock to that thread.

Class locks are actually implemented as object locks. When the JVM 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.

 

Threads need not obtain a lock to access instance or class variables. If a thread does obtain a lock, however, no other thread can access the locked data until the thread that owns the lock releases it.

 

A catch clause ensures the locked object will be unlocked even if an exception is thrown from within the synchronized block. No matter how the synchronized block is exited, the object lock acquired when the thread entered the block definitely will be released.

分享到:
评论

相关推荐

    jvm_learn_note.pdf

    在Java虚拟机(JVM)的学习中,了解内存管理和垃圾收集机制是非常关键的部分。这里我们将深入探讨这些主题。 首先,Java内存主要分为以下几个区域: 1. **方法区**:线程共享,存储已加载的类信息、常量、静态变量...

    尚硅谷JVM全套教程,百万播放,全网巅峰(宋红康详解java虚拟机)学习笔记_NOTE_JVM.zip

    尚硅谷JVM全套教程,百万播放,全网巅峰(宋红康详解java虚拟机)学习笔记_NOTE_JVM

    wanglijun93#AndroidNote#JVM类加载器1

    第一阶段要验证字节流是否符合Class文件格式的规范,并且能被当前版本的虚拟机处理 第二阶段是对字节码描述的信息进行语义分析,以保证其描述的信息符合Java语言

    Android代码-Kotlin-Reflect-Tools-For-JVM

    Note: This tools only availible for JVM not fit for android platform ,If you use on Android platform, Please use Kotlin-Reflect-Tools-For-Android Usage Add jcenter repository in your moduel build ...

    My-Note-Utils-Learn:学习笔记,类库。ActiveMQ消息队列,Layui常用公共类库,设计模式编码,多线程,JVM等

    My-Note-Utils-Learn xiaoshaDestiny:学习笔记,类库,面试QA learn-activemq 消息队列 ActiveMQ的使用,Spring整合ActiveMQ learn-data-tructure 数据结构 learn-design-pattern 设计模式 编码详解 learn-jedis ...

    leetcode分类-studyNote:mini-jvm团队学习笔记

    studyNote study note of mini-jvm team INBOX @java @inside @jvm How much memory does a Java object take? @memory @inside @cpu 关于程序局部性原理,如何写出性能比较高的代码。 @algorithm @leetcode 手把手撕...

    java_note_day02.pdf

    1. **JVM、JRE和JDK**: - JVM(Java Virtual Machine)是Java程序运行的核心,它负责解析和执行字节码文件(.class)。 - JRE(Java Runtime Environment)包含JVM和必要的库函数,使得Java程序能够在操作系统上...

    Java-Note:Java-Note,包含Java基础、并发编程、数据库、JVM、中间件、系统设计、开发框架、分布式等思维导图与笔记

    Java-Note Java基础 并发编程 数据库 基于Redis实现注册中心 Redis看门狗机制 Redission加锁流程 Redission解锁流程 JVM JVM 中间件 MQ MQ消息丢失解决方案 系统设计 秒杀系统设计 开发框架 Spring 分布式 分布式锁 ...

    JavaNote-main

    以上只是JavaNote-main可能涉及的部分内容,实际压缩包可能还包含更多如设计模式、数据库连接、JVM优化、反射、注解等进阶主题。学习Java时,理解并实践这些知识点将有助于成为一名熟练的Java开发者。

    Hpsyche#note#1.Java解释执行编译执行1

    (1)解析:.class文件经过JVM内嵌的解析器解析执行 (2)编译:存在JIT编译器(Just In Time Compile 即时编译器)把经常运行的代码

    butte-java-note编程文档

    1. **JVM**:Java虚拟机是Java程序运行的基础,文档可能讲解了JVM的工作原理、内存模型、类加载机制、垃圾回收以及性能优化等内容,帮助开发者理解如何编写更高效、更稳定的Java代码。 2. **Java基础**:这部分内容...

    Notes:This is a learning note | Java基础,JVM,源码,大数据,面经

    jvm垃圾收集机制与内存分配策略 jvm类加载机制 Java的内存模型 锁优化 Think In Java Java容器 Java并发 Java Concurrency in Practice 对象的共享 对象的组合 基础构建模块 JavaGC监控与优化 垃圾回收机制 垃圾回收...

    butte-java-note-编程文档

    1. **JVM(Java虚拟机)**:JVM是Java程序运行的核心,它负责解释和执行字节码,提供了跨平台的运行环境。了解JVM的工作原理,包括类加载机制、内存管理(堆、栈、方法区等)、垃圾收集机制以及性能优化策略,对于...

    JVM、Java基础、设计模式、结构算法、架构设计、Spring框架、中间件、大数据、数据库、Linux、数据服务.zip

    在这个"java学习"的压缩包中,"butte-java-note-master"可能是一个包含Java相关学习资料的项目,可能涵盖了上述所有知识点的详细教程或笔记。通过深入学习和实践,开发者可以全面提升自己的Java技能,从基础知识到...

    java_note笔记

    这份"java_note笔记"涵盖了Java语言的多个重要方面,包括基础语法、面向对象特性、图形用户界面(GUI)、集合框架、输入/输出(I/O)以及网络编程等。下面将详细解释其中的关键知识点。 1. **继承关系判断**:在...

    Java Concurrency In Practice Learning Note

    博客文章“Java Concurrency In Practice Learning Note”则是一位开发者在阅读本书后的心得体会,通过链接(已给出)我们可以获取更多实践中的经验分享。 并发编程的核心在于如何高效地利用多核处理器资源,同时...

    javaSE-note teacher

    1. **跨平台性**:Java的跨平台特性主要得益于其虚拟机(JVM)技术,即Java源代码被编译成字节码(.class文件),这种字节码可以被任何平台上的JVM解释执行,从而实现了一次编写,到处运行的目标。 2. **简单性**:...

    note11111.txt

    ### 1. JVM优化与内存管理 - **线程栈大小调整**:通过 `-Xss=N` 参数(例如 `-Xss=256k`)来设置线程栈大小。对于32位JVM来说,减小线程栈可以在一定程度上增加堆内存的可用空间,但过小可能会导致 `StackOverflow...

    java_note_day03.pdf

    1. **JVM (Java Virtual Machine)**:Java虚拟机是Java程序运行的核心,负责解释执行字节码文件(.class)。Java源代码首先由Java编译器(javac)编译成字节码,然后JVM负责解释执行这些字节码。 2. **JRE (Java ...

    JavaNote:java es番石榴jdk jdk8 jsonLombokmd线程jvm spring mvc vertx设计模式风暴

    请叫我大师兄 Java Note Project 项目结构目录 bean 统一存放一些测试使用的model的bean,enum... 设计模式 简要说明 设计模式是一套被反复使用的、多数人知晓的、经过分类编目的、代码设计经验的总结。 使用设计...

Global site tag (gtag.js) - Google Analytics