A preliminary understanding of java--the analysis of multithreading in the 12th chapter-JVM

Source: Internet
Author: User

One.

We learn Java program to learn now, in the end is multi-threaded, or single-threaded? (In reality, multithreading is always there, we just began to learn this concept, not learned does not mean that there is no.) )

The startup of a virtual machine relies on multiple threads, why?

For example, (for example, to study multithreading)

New three anonymous objects, draw a heap memory demo.

On the left is the stack and the right is the heap. There is a main function inside the stack, and there is code in the main function. Inside the heap, the object is created, the anonymous object is garbage when it is created (no point), and now there are three of garbage.

There is a lot of code inside the main function and it continues to execute.

From here, we can get the 1th immediately, that is, our Java Virtual machine must have a thread executing the main function when it is running. This is necessary, without this thread, the main function cannot run at all. Some people say, isn't the virtual machine calling the main function? Again, it depends on the thread to execute, because only the thread can execute the code. One of the threads is responsible for the main thread execution. (The calling function is completely a programming language, and the thread is a bit of hardware.)

Garbage is present in the heap memory and needs to be recycled. If there is a recycling action, it is said that the main thread to perform garbage collection, which means that the program runs to half stop, the main function to perform garbage collection. This is not right, we can not see the recycling of garbage.

During the execution of the main thread, the garbage collector recycles the garbage on a regular basis, which means that someone is doing the recycling, which represents two threads. In fact, more than two, there are many threads, there is no need to introduce each one. Virtual machine underlying it has the task of turning on multithreading, but this task we can analyze to come out (this task is the main function run, garbage collection). → Virtual machines seem to be important in the process of running a program.

The start of the virtual machine itself, the thread is more than one. The thread that specializes in garbage collection is what we call a garbage collection thread, and the thread that executes the code in the main function is called the main path.

After any thread has started, it has its own task.

The content of the garbage collection thread, or the task code is defined inside the garbage collector, we don't have to do it, it's itself running internally, and we don't need to do it.

For the garbage collector, we do a simple demonstration of how it is recycled. This is done inside the bottom.

How do you show it? Imagine that a lot of objects are generated in the heap memory, and each object has the possibility of being recycled. How objects are recycled, only the object itself is clearest. Once the object has been created, each object has its own execution characteristics, how it is recycled, it knows for itself, and each object has a method of being recycled. Do you call this method when you write to the object? No, each object has a method in the object class.

This method is finalize, and you do not have to rewrite the Finalize method if you do not need to configure system resources or perform other cleanup. (Finalize is recycling)

Now verify what the Finalize method is all about.

In fact, there is no need to make a carbon copy, because it does not have to do this thing, it does not have to do a lot of packaging data, the memory is removed directly to the finished, with our stack memory release principle, clear memory space to erase data, store other data on the finished. The definition of finalize and the shutdown of system resources are only possible if the system resources are involved.

DOS running results without demo OK.

We know that these three anonymous objects have become garbage, but are they immediately recycled? Not necessarily, they are recycled at irregular times.

What is a garbage collector? That's the garbage collection program. This program is doing garbage collection action, this program is responsible for executing by a separate thread. If you find this garbage collector.

There is a GC method inside the system class. This method can run the garbage collector. Run the garbage collector through the static method of this system class.

The GC in is not immediately let the garbage collector start, is telling the garbage collector you want to start, as to how to start it in the call.

Why is Hello world output first? As a matter of course, the garbage collector starts, it recycles two garbage bars, and calls the Finalize method of the object when it is received. That said, the garbage collector calls the Finalize method, and a printout shows that the garbage is being reclaimed.

(GC method starts the garbage collector, the garbage collector enables the Finalize method, the real recovery is finalize)

Why does Hello world print in front of the demo OK? Because this is done by two threads. When the main thread calls to the GC, this is the time to tell the garbage collector. The garbage collector starts the machine at an indefinite time, while the main thread continues to run down, and the garbage collection thread is independent of the main thread operation. After the main thread has printed Hello world, the garbage processing

Only two garbage objects were processed by the device.

Revise the program and look at the results

After the compilation runs several times, two DOS results appear. There are two concepts, one is slime, the other is priority. Let's talk about the phenomenon first.

In addition to the above two cases, there may be a demo OK first out of the situation.

In this case, the garbage collector has just reclaimed one and the virtual machine is over. The end of the virtual machine, it will force the end of its process (that is, the memory area), that is, the Java.exe program is the end of the process, the memory is cleared. As a result, the call to finalize has not been completed. (The entire virtual machine is over, not to mention the garbage collection thread inside)

The above example verifies that there are at least two threads.

In fact, there is a thread, and after the main thread executes, the code ends. As soon as the thread's task is finished, the thread ends, which means the main path ends. Some say the main thread is over, what's the virtual machine doing? When Hello World is finished printing, the virtual machine should exit. I'm telling you, it's just the end of the main thread, the other threads in the virtual machine are still executing, and the virtual machine is not over yet.

Summary: The multi-threaded content involved in the virtual machine.

A preliminary understanding of java--the analysis of multithreading in the 12th chapter-JVM

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.