Java-Preliminary Understanding-12th chapter-Main thread Run example

Source: Internet
Author: User

One.

We focus on the main thread, we generally do not pay attention to garbage collection, because this action is automatic, it is in what way to judge? Once garbage is generated in the heap memory, it is not immediately cleaned up, and if cleared immediately, it will reduce efficiency. Because garbage collection is in operation, the main thread will rob the CPU of control (get CPU execution). In a moment who gets the executive right, who operates. What is the next time to recycle it? It will first determine the size of the heap memory, if a certain amount will be released. Do not release the load will not go in, fear of overflow, it is necessary to do automatic release action.

Then use the example code to reflect, (what does it embody?) )

Only the main thread part is considered,

The result of this DOS compilation run is the main thread control execution.

Now draw a multi-threaded graph, (not in the painting heap, directly to the stack study), where the memory map compared to the previous memory diagram has a great increase. (This complex process has been in existence, not previously introduced)

Not that there is no heap of memory, but that we are now focusing on stack memory.

Stack memory after painting, there is the main thread (when the virtual machine starts, tell everyone to execute this class, is not the first to start a thread, it actually starts a number of threads, one of which is responsible for the main function), so this thread started, then came a called the main function, ( A thread is started before the program code runs, because it controls the entire running process. )

In fact, there is a path, (how do you draw this thing?) It doesn't matter, the execution process is the same anyway. When we create two objects in main, note that the constructor I do not draw (first heap memory is not drawn, the objects inside are not drawn.) The construction of the object when it was created is to be drawn in the stack memory and is now omitted. When we call show in D, Show is in the stack, show into the stack or rely on the main thread, (it is possible that the constructor is in the stack is dependent on the main thread)

(The diagram has been modified, the stack memory should not be drawn from the top down execution path, it seems to be executed from the top down, should be the program code from the top down)

The first is the main thread advanced stack (why does the main thread have the concept of a stack?) ), followed by the show method into the stack. This is going to execute show, show is not finished, can execute to the main function? is impossible.

After the show executes, the main function continues to execute after the stack is out. Execution is d2.show, then the show method again into the stack, after execution, again out of the stack. After that, there is a return, which executes the post function out of the stack. This time, there is no executable content, the main thread is over (it seems that the main thread corresponds to the code inside the primary function), the main thread of the end, no content, the virtual machine to the process is closed, (we do not say the underlying content, which is the virtual machine automatically run)

Now we have one such worry, how to solve this problem? For example, when our program runs down the process, it encounters a circle (as shown), in other words, it is a loop, it has to loop several times. Once it's turned up, it can't be executed. This means that the following code is not running and we want to run it. Don't want to because this cycle in here, delay time, how to do?

At this point, it is possible to have a separate execution path. To be responsible for the execution of this piece of content.

The left is the main thread, the right side is the thread that you created, or the thread that you defined, so is there a loop condition? Now look at the demo,

(modified the program, to demonstrate)

The result of the whole DOS is to jump out slowly.

What is my demand now? Because there is a program constantly in the circle to stop, and constantly go to run, and not go down, resulting in my program is relatively slow. So, we would like to say, can we achieve the "prosperous" and xiaoqiang simultaneous implementation? That's what we need,

How to achieve "Wang Choi" and Xiaoqiang at the same time, is not the need to do an action, Xiaoqiang in a path, Wang Choi to execute the code in another code, is not able to complete the action, the CPU on the two path to switch. How do you create a new path?

Java-Preliminary Understanding-12th-Main thread Run example

Related Article

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.