Java-Preliminary Understanding-The 12th chapter-Multi-threaded operation diagram

Source: Internet
Author: User

One.

Why do you tell the plot, because some of the results of DOS now have written to subvert the original ideology. Multi-threaded at execution time, the CPU switches this execution, the different content crosses appears. But before we say, a function can only be executed when the stack is not finished and will not play stack. Now explain all this through illustrations.

Using the above to study the illustrations, we only study stacks, others do not discuss.

Before the memory plot is, the main method into the stack, and then involves the invocation of the method sequentially into the stack, each execution of one, out of the stack. Finally the main method out of the stack, orderly, but in the multi-threaded here can not do so.

Now that the mode of operation has changed, you just have to open an execution path and it has a separate execution path. Once understood as a road, now is three.

As soon as the main thread is open, the main thread path has already appeared because it is calling, or going to execute the main function (this is the main thread path). During the execution of the code in the main function, two threads were created (creation and use are two different things that can be created and not used). When the main thread reads to D1.start (), a path is opened and a path is made. The contents of the first execution path are main, and the contents of the second execution path are run. When the main thread continues to execute the D2.start (), a path is opened, and the path executes the contents of run. This is equivalent to having their own space inside.

If you call a call to other methods, in the main stack stack, that is, as long as you are on the main path, the main thread in the stack area, and then into the stack bomb stack. If the run () method calls other methods, the stack is entered in the run stack area. These threads are also given names, Main,thread-0,thread-1. Each run method has a stack area of the thread to which it belongs.

At the same time, there is no conflict at all because there is a variable inside each run method called the X,run method that has a local variable for the FOR Loop statement (I think there is no relationship between the existence of X and the collision of multiple run. )

There are three tasks to perform here, and once a task has been executed, the task is finished. If it is thread 0, it calls a haha method, the Haha method will go into the stack, into the stack of thread 0. After entering, the execution is finished. If the Run method is executed, the task performed by the thread is finished, and the location is freed, and the path is not needed. Because the thread is going to execute the code, it

The code is over and the thread doesn't make any sense.

Is there a situation where these three tasks are executing at the same time, and the CPU switches between the three. Interesting come, main line enters upgradeable end, virtual machine end? No, as long as there is a running thread, it will exist. Can only say the main line enters upgradeable end.

Now let's expand on the narrative,

The following thread-0 and Thread-1 are still in the execution,

When the main thread executes to 4/0, an exception occurs. The exception is then thrown out to the JVM default processing and finally printed on the DOS console. The following for statement for the output statement is no longer executed,

The other is still executing, the main thread is having problems, and the other threads are not related.

(Even printing exceptions are separated here.)

After a few more tries, there is a case where the printed exception information is concentrated together

(But there is still a run content appearing)

In the first line of the printed result, Exception in thread "main" was not mentioned before, before the java.lang.ArithmeticException is described ... The first sentence that comes out of the box means that the exception thread is called the inside of Main.

We're just going to compile and run a nonexistent haha.

The result is displayed in the main thread, which is a meaning.

Now further improvements,

That is, the thread of the Run method is also set to an exception.

The results of the entire compilation run are as follows,

The DOS results report that an exception occurred on three threads, and the thread of the line stopped running after an exception occurred.

Who has the problem, who ends, the other does not affect.

Java-Preliminary Understanding-The 12th chapter-Multi-threaded operation diagram

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.