Black Horse Programmer _ Multithreading in Java

Source: Internet
Author: User

Although the common methods of threading objects can be understood through API documentation, there are many ways to understand them simply from API descriptions.

Originally intended to use a section of the thread approach to some of the important knowledge to finish, but this down estimate to very often, may have to use several sections to say and threading methods related to some important knowledge to finish.

First, we take the basic article (ii) to illustrate the start () method.

After a thread object is generated, it is important to invoke its start () method if it is to produce an executing thread. The Run method must be described in the introduction of this method. Actually, the run method of the thread object is completely an interface callback method. It is the specific logic that you want the thread object to complete. Simply say what you're going to do in run, and what you do, when you don't need control, you just call the start () method, and the JVM manages the thread object so that it generates a thread and registers it with the threading system.

On the surface, the start () method invokes the run () method, in fact the start () method does not directly call the Run method. Before JDK1.5 the start () method is a local method, How it ends up calling the Run method is not what a Java programmer can understand. In JDK1.5, the original local start () method is replaced by Start0 (), and another pure Java start () calls the Local method Start0 (), while the start () The method verifies that a global variable (object variable) started is checked, and if true, start () throws an exception and does not call the local method start0 (), otherwise the variable is first set to true and then called Start0 ().

From which we can see this in order to control a thread object can only run successfully once the start () method. This is because the thread is running to get the current environment, including security, permissions for the parent thread, priority, and so on, if a thread object can run multiple times, then define a static Thread gets the appropriate permissions and priority in one environment, it runs in the current environment with the original permissions and priority attributes in another environment, resulting in unpredictable results. In short, having a thread object run successfully only once is based on the need for thread management.

The most essential function of the start () method is to request another thread space from the CPU to execute the code in the Run () method, which is two lines from the current thread and runs in a relatively separate thread space, that is, if you call the run () method of the thread object directly, it will, of course, be executed, but that is Executes in the current thread, and executes the following code after execution of the run () method. When the start () method is called, the Code of the Run () method executes concurrently (single CPU) or parallel (multi-CPU) with the current thread.

So remember one sentence [the Run method of the calling thread object does not produce a new thread], although the same execution result can be achieved, but the execution process and execution efficiency are different.

[Thread's Interrupt () method, Interrupted () and isinterrupted ()]

These three methods are very close and complex, though their respective functions are clear, but the relationship between them is not really understood by most people.

First say the interrupt () method, which is an instance method, and it is also the strangest way, in the Java language, the thread was originally designed to be "obscure" things, until now its semantics are not as accurate as its name. Most people think that a thread called the Interrupt () method, the corresponding thread should be interrupted and throw an exception, in fact, when a thread object calls the interrupt () method, its corresponding thread is not interrupted, just changed its interrupt state.

Changes the state of the current thread to an interrupt state, and if there are no other effects, the thread will continue to execute itself.

The thread throws an exception only if the thread executes to a method such as Sleep,wait,join, or if it throws an exception by checking the interrupt state itself.

If the thread object calls interrupt () and its corresponding thread is immediately interrupted, then the interrupted () method cannot be executed.

Because the interrupted () method is a static method, which means it can only be called on the current thread, and if a thread interrupt () it has been interrupted, how does it let itself interrupted ()?

Just because a thread calls interrupt () only changes the interrupt state, it can continue execution, it can call interrupted () to clear the interrupt state before calling Sleep,wait,join or throwing an exception itself. The interrupted () method checks the interrupt state of the current thread and, if it is "interrupted", changes the current thread to "non-disruptive state" and returns True if the "non-disruptive state" returns false, which not only checks whether the current thread is in a broken state, but also guarantees that when The front end comes back to the non-interruptible state, so it's called "interrupted", which means that the state of the interrupt has ended (to a non-interruptible state) The isinterrupted () method simply checks that the thread object's corresponding thread is in a broken state and does not change its state.

At present, we can only remember the functions of these three methods, only really deep into the practice of multi-threaded programming, will realize why they are object methods, why is the class method.

Exactly when the thread is interrupted throws the interruptedexception exception, which we will discuss in detail in the improvement article.

[Sleep (), join (), yield () method]

In the current link, I can only explain the role of these methods and the principle of invocation, as to why, in the basic article can not be in-depth, can only be described in the improvement of the article.

The sleep () method is a class method, that is, for the current thread, the programmer cannot specify a thread to sleep, only the time specified by the current thread when it executes to the sleep () method, which allows other threads to run. In fact, it can only be a class method, Called on the current thread. Imagine if you invoke the sleep () method of a thread object, how does it sleep () if the object's corresponding thread is not running? So you can only guarantee that it will call the sleep () method if it is executing.

Principle: [Try not to invoke the sleep () method of the thread in the synchronization method], or simply say that you should not call the sleep () method for a general level programmer.

Join () method, as described in the first section, calls the Join method on a thread object, which is the current thread that waits for the thread object to end, such as two jobs, work a takes 10 seconds, and work B takes 10 seconds or more. We're in the program Mr. A thread goes to work B and then does a.

Black Horse Programmer _ Multithreading in Java

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.