Java-Preliminary Understanding-The 12th chapter-Multi-threading creation way one inherits

Source: Internet
Author: User

One.

Now that the thread is created, how should the thread be created? Before we learned Java, who were these programs executing? is done by the operating system we operate on. Windows has a task manager that manages these tasks to create these processes and to create the threads used in the process. is done by the system, and we can't use the system directly to help us do this thing. When it comes to solving the problem, the first thing to think about is whether there are any objects in Java that will solve the problem, especially if the thread is still out of the system. You don't play Java, it doesn't have multiple threads, because Windows has the ability to do this assignment for you. We can not do, we want to say whether Java has such an object, to find the bottom to do this thing, and expose the object to our use. We do things to solve problems, we need to find the object first. (Do you find the thread object?) Yes, the thread object does not need us to perform the action.

So who's the target? Let's look it up in the Java API documentation. Inside Java's core package, Java.lang (Java language Pack). Why to find the core package, because the virtual machine starts, with multi-threading, so the object is here (control multi-threaded objects in the core package, because from this). There's such an object in the Java.lang bag thread.

This thread, we know that there is such a thread class (originally the thread class represents the threads). How to create it?

When you're learning about objects, read the API documentation if no one teaches.

In the API documentation, there is also an example of using the thread method, which has constructors and a replication run method. (although there are two methods, only one method for creating an execution path is described)

(comb, to create a new thread, first inherit the thread class, and then make a duplicate

The Run method)

Why would you rewrite the Run method?

This is the first way to create a thread,

Inheriting the thread class is understandable (inheritance is to have the attributes of the parent class-creating the thread), why overwrite the Run method?

The composition of the thread class is as follows, with a constructor,

At the same time, the thread class is not abstract (stating that thread can create an object) and it can be a new object. The problem is solved, Java has provided us with the thread class, and the creation of its object is the thread (how can this sentence come out?). Because thread objects are threads, this is why, depending on the inheritance in the API documentation, what to overwrite.

Creating a Thread object is creating a threading object.

What is the purpose of creating a thread? The purpose of creating a thread is to open an execution path, what is the purpose of opening the execution path? is to let part of the code run at the same time as the rest of the code, which we call a task. This is the characteristic that has been analyzed just now.

The task is a piece of code that is to be encapsulated before it can be used. This package is the Run method. This is the reason that the thread class has the Run method.

Some people say that the direct new object, called the Run method, is finished. That's right, but is the task in your run method what I need? (This is the reason for the replication)

We open threads to run our defined tasks, you have task functions, I have the task content, what should I do? Should inherit you and cover you and write my content into your run method. That's why we inherited and replicated the Run method.

(Inheriting the thread class with subclasses, just to overwrite the Run method in the parent class to run what you want to run)

Therefore, the operation of the new thread object is no longer done, but it is inherited.

According to the previous example, the card is looping this, and we have to re-open a thread to run the loop statement. This section describes how to introduce a looping statement into a new thread.

(This is another form of looping statement introduced into the Run method)

So far, the coverage has been done. The third step is to create a thread object, make a class, inherit Thread,thread is a thread, inherit it, and I am also a thread (inherit all its features).

→ I know, why to create a subclass object, the light has inherited subclasses, covered by the Run method, no use. You must have an object to invoke the method to run.

Now the code in the main program is modified, and the object is calling the Run method. This time, whether it is D1 or D2 are threads,

The results of the compilation run are still the same as before.

Why is that? Why is the main thread in this line? (How do you know that the main thread executes it?) Imagine if two threads were actually created, and if they were to run, it would mean that D1 and D2 ran the Run method, which might be random.

Now modify the program to add an output statement between the two loops. See if they are orderly.

The results show that two cyclic outputs are fixed and orderly. Note So far, there is only one main thread executing. Why is it? Threads such things are special, after the creation, you call the Run method, in fact, we have to create the object call method is not the same as before. This is a standard way of calling, and if you want to use threading to do this, you get the thread class to find the method.

After the example, there is a sentence, then .... Create and start a thread. This thread inherits the subclass object created and has to be started.

Who does the open action, start method. How to turn it on, the thread knows for itself (so find the appropriate method in the threading Class),

Do you call the Run method by hand? No, direct start is all right.

The Start method does two things, one is to open the thread, and the other is to call the Run method and tell the virtual machine to call the method.

What is the difference between calling run and calling start, as explained in the previous example?

The result of this operation is finally in line with our original idea. The CPU is doing a random switchover (so the two results are random), we have three threads here (two loop threads, throw the main thread away), the main thread and both of its own open. The CPU quickly switches between three threads, cutting to who runs who, so it's random. The result is how to print, how the output is CPU.

Java-Preliminary Understanding-The 12th chapter-Multi-threading creation way one inherits

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.