Java Thread in JVM

zhaozj2021-02-16  50

Java Thread in JVM

WANG Hailong

This article explores the grammar and compile results of Java Thread from the perspective of JVM. If you need to obtain first-hand information, please directly access the following resources - Java language specification, and the definition instructions for threads in the Java virtual machine specification.

This article is intended to introduce these comparison thread-related specifications, basically do not work. (In addition to mention Microsoft's "public language base structure". :-)

Java Language Specification

Http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#30531

JVM Specification

Http://java.sun.com/docs/books/vmspec/2nd-edition/html/compiling.doc.html#6530

http://java.sun.com/docs/books/vmspec/2nd-edition/html/instructions2.doc9.html

Http://java.sun.com/docs/books/vmspec/2nd-edition/html/threads.doc.html

Microsoft CLI - Common Language Infrastructure (Sorry, Offic :-)

http://msdn.microsoft.com/net/ecma/

1. Java language specification for Synchronized Method

See http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#30531.

Divided into two cases with synchronized keyword modified: (static) static method, and instance method.

(static) The "lock" of the static method is the Class object of this object that has this method; "lock" of the instance method is this, which has the current object instance of this method.

How to understand this, look at the example below.

The following two codes are exactly the same. Code 1 == Code 2.

Code 1:

Class test {

INT country;

Synchronized void bump () {count ;}

Static int classcount;

Static synchronized void classbump () {

ClassCount ;

}

}

Code 2:

Class Bumptest {

INT country;

Void Bump () {

Synchronized (this) {

COUNT ;

}

}

Static int classcount;

Static void classbump () {

Try {

Synchronized (Class.Forname ("BUMPTEST")) {

ClassCount ;

}

} catch (classnotfoundexception e) {

...

}

}

}

2. SYNCHRONIZED keyword compile results

In this section, let's take a look at the Java virtual machine instruction after synchronized keyword compilation.

If you need first-hand information, see the Java virtual machine specification related part

Http://java.sun.com/docs/books/vmspec/2nd-edition/html/compiling.doc.html#6530

In this specification, the Java virtual machine specification provides two instructions, Monitorenter, and MonitorExit to support threads. However, for the first section, uses synchronized modified methods, and does not use these two methods, but simply use the ACC_SYNCHRONIZED flag to modify. This flag is checked when the virtual machine is modified. The compilation result of the synchronized statement corresponds to the two instructions of Monitorenter and MonitorExit.

For example, the following code:

Void Onlyme (foo f) {

SYNCHRONIZED (f) {

DOSMETHING ();

}

}

The result is

Method void Onlyme (foo)

0 aload_1 // push f

1 ASTORE_2 // Store IT IN Local Variable 2

2 Aload_2 // Push Local Variable 2 (f)

3 Monitorenter // Enter the Monitor Associated with f

4 ALOAD_0 // Holding The Monitor, Pass this and ...

5 Invokevirtual # 5 // ... Call Example.Dosomething () V

8 ALOAD_2 / / PUSH LOCAL VARIABLE 2 (f)

9 MonitorExit // EXIT The Monitor Associated with f

10 Return // Return Normally

11 ALOAD_2 // In Case of Any Throw, End Up Here

12 MonitorExit // Be Sure To EXIT MONITOR ...

13 Athrow // ... Then Rethrow The Value to the Invoker

3. Monitorenter and MonitorExit

See http://java.sun.com/docs/books/vmspec/2nd-edition/html/instructions2.doc9.html

One of the modes of definition of Monitorenter:

Operation: ENTER MONITOR for Object

Operand Stack: ..., ObjectRef

...

Description:

The ObjectRef Must Be of Type Reference.

Each object has a monitor associated with it. The thread that executes monitorenter gains ownership of the monitor associated with objectref. If another thread already owns the monitor associated with objectref, the current thread waits until the object is unlocked, then tries again to gain ownership . If the current thread already owns the monitor associated with objectref, it increments a counter in the monitor indicating the number of times this thread has entered the monitor. If the monitor associated with objectref is not owned by any thread, the current thread becomes the Owner of the Monitor, Setting The Entry Count of this Monitor To 1. This means that the target of the MonitorEnter is a certain object, and the type is Reference. Reference is actually the address of a stack in the heap. Each object has a Monitor, if there is other thread to get the Monitor of this object, the current thread will wait until you get the Monitor thread to give up Monitor, the current thread has the opportunity to get Monitor.

If Monitor is not obtained by any thread, then the current thread gets this Monitor, set Monitor's entry count to 1. This Monitor is taken up by 1 thread.

After the current thread gets Monitor, the time count of this Monitor will be added to record how the current thread takes up Monitor.

We see that the word Monitor appears in the Java virtual machine specification, but there is no appearance in the Java language and the API document. Monitor is the principles and concepts hidden behind the thread synchronization.

4. Threads and Locks

See http://java.sun.com/docs/books/vmspec/2nd-edition/html/threads.doc.html.

This specification describes the principles of Thread and Lock. The highlight of this specification is given below.

8.4 Nonatomic Treatment of Double and long atmosphere: non-atomic operations for Double and long.)

8.7 Rules for Volatile Variables

8.10 EXAMPLE: POSSIBLE SWAP

8.11 EXAMPLE: OUT-of-ORDER WRITES

If you are interested in these highlights listed, please visit the appropriate Java virtual machine specification URL.

5. Why Specification?

This article mainly discusses the contents of Java related specification. The standard document is very important, especially for Java, C # this language to generate intermediate code.

The above is the relevant specification of Java. Here is the way Microsoft .NET related specification.

Microsoft's "Public Language Foundation Construction" specification:

Microsoft CLI - Common Language Infrastructure (Sorry, Off: -) http://msdn.microsoft.com/net/ecma/

There is a C # language specification on this website, the download of the CLI specification.

Enjoy it. :-)

转载请注明原文地址:https://www.9cbs.com/read-23519.html

New Post(0)