什么 JVM ?
JVM 是可运行 Java 中假想的计算机,就是在真实的计算机上模拟计算机功能,包括一套字节指令,一套寄存器,一个栈,一个垃圾回收,堆,一个存储区域,JVM 是运行在操作系统之上,它与硬件没有直接的交互。
Java 源文件通过编译器,能够生产相应的 .class 文件,也就是字节码文件。而字节码又通过 Java 虚拟机中解释器,编译成特定机器上的机器码。
1. Java 源码文件 -----》 编译器 ------》字节码文件
2. 字节码文件 -----》JVM ----》 机器码
每种平台的解释器是不同的,但是实现的虚拟机是相同的,这也就是 Java 为什么能够跨平台的原因。当一个程序从开始运行,这是虚拟机开始实例化,多个程序启动就会存在多个虚拟机程序。程序退出或者关闭,虚拟机消亡,多个虚拟机实例之间数据不能共享。
Hotspot JVM 中的 Java 线程与 原生操作系统线程有直接的映射关系。当线程本地存储,缓冲区分配,同步对象,栈,程序计数器准备好之后,创建一个操作系统原生线程,Java 线程结束,原生线程将被回收。
Hotspot JVM 后台运行的系统线程主要有下面几个
列表参考
2020-01-27 18:04:11 Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.40-b25 mixed mode): "Service Thread" #10 daemon prio=9 os_prio=0 tid=0x00000000195ee800 nid=0x1bc runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C1 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x00000000195ec000 nid=0x4750 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C2 CompilerThread1" #8 daemon prio=9 os_prio=2 tid=0x0000000019593000 nid=0x51e0 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C2 CompilerThread0" #7 daemon prio=9 os_prio=2 tid=0x0000000019590800 nid=0x4704 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x000000001956f000 nid=0x2f7c runnable [0x0000000019e2e000] java.lang.Thread.State: RUNNABLE at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.socketRead(SocketInputStream.java:116) at java.net.SocketInputStream.read(SocketInputStream.java:170) at java.net.SocketInputStream.read(SocketInputStream.java:141) at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284) at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326) at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178) - locked <0x00000000d5fadb60> (a java.io.InputStreamReader) at java.io.InputStreamReader.read(InputStreamReader.java:184) at java.io.BufferedReader.fill(BufferedReader.java:161) at java.io.BufferedReader.readLine(BufferedReader.java:324) - locked <0x00000000d5fadb60> (a java.io.InputStreamReader) at java.io.BufferedReader.readLine(BufferedReader.java:389) at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:64) "Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x0000000017c2b000 nid=0x5050 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x0000000017be0000 nid=0x13b0 runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Finalizer" #3 daemon prio=8 os_prio=1 tid=0x0000000003043000 nid=0x2d9c in Object.wait() [0x0000000018f2f000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x00000000d5b86f58> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143) - locked <0x00000000d5b86f58> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164) at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209) "Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x0000000003040000 nid=0x539c in Object.wait() [0x0000000018e2e000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x00000000d5b86998> (a java.lang.ref.Reference$Lock) at java.lang.Object.wait(Object.java:502) at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:157) - locked <0x00000000d5b86998> (a java.lang.ref.Reference$Lock) "main" #1 prio=5 os_prio=0 tid=0x0000000002f53800 nid=0x1ea0 runnable [0x0000000002d5e000] java.lang.Thread.State: RUNNABLE at java.io.FileInputStream.readBytes(Native Method) at java.io.FileInputStream.read(FileInputStream.java:255) at java.io.BufferedInputStream.read1(BufferedInputStream.java:284) at java.io.BufferedInputStream.read(BufferedInputStream.java:345) - locked <0x00000000d5bdc1c8> (a java.io.BufferedInputStream) at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284) at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326) at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178) - locked <0x00000000d5efa2b8> (a java.io.InputStreamReader) at java.io.InputStreamReader.read(InputStreamReader.java:184) at java.io.Reader.read(Reader.java:100) at java.util.Scanner.readInput(Scanner.java:804) at java.util.Scanner.hasNext(Scanner.java:1339) at algorithm.Main.main(Main.java:29) "VM Thread" os_prio=2 tid=0x0000000017b97800 nid=0x3c78 runnable "GC task thread#0 (ParallelGC)" os_prio=0 tid=0x0000000002f69000 nid=0x3860 runnable "GC task thread#1 (ParallelGC)" os_prio=0 tid=0x0000000002f6b000 nid=0x2ab0 runnable "GC task thread#2 (ParallelGC)" os_prio=0 tid=0x0000000002f6c800 nid=0x507c runnable "GC task thread#3 (ParallelGC)" os_prio=0 tid=0x0000000002f6e000 nid=0x3458 runnable "VM Periodic Task Thread" os_prio=2 tid=0x00000000195f0000 nid=0x3814 waiting on condition JNI global references: 33