Multi-thread programming BASICS (III)

Source: Internet
Author: User

Basic (III)

Several important methods of thread objects

Although the common methods of thread objects can be learned through the API documentation, there are many methods that are not detailed only from the API description.

I was going to use a section to finish some important knowledge about the thread method, but it is estimated that it would take a very long time to use it.
Several sections can explain some important knowledge related to thread methods.

First, we will explain the START () method in the basic article (II.

After a thread object is generated, if you want to generate an execution thread, you must call its start () method. When introducing this method
I have to explain the run method at the same time. In fact, the run method of the thread object is an interface callback method, which is the end of your thread object.
The specific logic. Simply put, you can do what you want to do in the run, and you don't need to control when you do it.
With the start () method, JVM will manage this thread object to generate a thread and register it in the thread processing system (Thread Stack).

On the surface, the START () method calls the run () method. In fact, the START () method does not directly call the run method.
The START () method is a local method. It is no longer known to Java programmers to call the run method. In jdk1.5, the original
The local start () method is replaced by start0 (), and the local method start0 () is called in the START () method of the pure Java ()
The method is used to verify a global variable (object variable) started. If it is true, start () throws an exception and does not
The local method start0 () is called. Otherwise, set the variable to true and call start0 ()

We can see that to control a thread object, the START () method can only run successfully once.
Obtains the current environment, including security, permissions of the parent thread, priority, and other conditions. If a thread object can be run multiple times, a static
The thread obtains the corresponding permissions and priorities in one environment. After running, it uses the original permissions and priorities in another environment.
This causes unpredictable results. Simply put, a thread object can only run once successfully, which is based on
Thread management needs.

The most essential function of the START () method is to apply for another thread space from the CPU to execute the code in the run () method. It and the current thread are
The two lines run in a relatively independent thread space. That is to say, if you directly call the run () method of the thread object, it will also be executed,
It is executed in the current thread. After the run () method is executed, it continues to execute the following code. After the start () method is called, The run () method
The code is executed concurrently with the current thread (single CPU) or concurrently (multiple CPUs.

Therefore, remember one sentence [calling the run method of the thread object will not generate a new thread]. Although the same execution result can be achieved
The row process and execution efficiency are different.

[Thread's interrupt () method, interrupted () and isinterrupted ()]

These three methods are closely related and complex. Although their respective functions are clear, the relationships between them are mostly
People are not really familiar with it.

The interrupt () method is an instance method, and it is also the most strange method. In Java, the thread was initially designed as "obscure"
Until now, its semantics is not as accurate as its name.
Most people think that if a thread calls the interrupt () method, the corresponding thread should be interrupted and an exception will be thrown,
In fact, when a thread object calls the interrupt () method, its corresponding thread is not interrupted, but its interruption status is changed.
Change the status of the current thread to the interrupted state. If there is no other impact, the thread will continue to execute it on its own.
Only when the thread executes sleep, wait, join, or other methods, or checks the interrupt status and throws an exception, the thread
Will throw an exception.

If the corresponding thread of the thread object is immediately interrupted after interrupt () is called, the interrupted () method cannot be executed.
Because the interrupted () method is a static method, that is, it can only be called on the current thread.
It has been interrupted. How can it interrupted () itself ()?

Because a thread only changes the interrupt status after calling interrupt (), it can continue to run without calling sleep,
Before using the wait, join, or other methods or throwing exceptions, it can call interrupted () to clear the interrupt status (which will remain unchanged)
The interrupted () method checks the interrupt status of the current thread. If it is "interrupted", it changes the current thread to "non-interrupted ".
"And returns true. If it is" non-interrupted ", false is returned. It not only checks whether the current thread is in the interrupted state, but also ensures the current line
The process is not interrupted, so it is called "interrupted", indicating that the interrupted state has ended (to the non-interrupted state)

The isinterrupted () method only checks whether the thread corresponding to the thread object is in the interrupted state and does not change its state.

At present, you can only remember the functions of these three methods. You can only understand why these methods are used in multi-threaded programming.
Is the object method, why is it a class method.

When the thread will be interrupted and an interruptedexception exception will be thrown. We will discuss it in detail in the article on improvement.

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

In the current phase, I can only explain the functions and calling principles of these methods. As for why, I cannot go deep in the basics, but I can only
Detailed descriptions are provided in the article for improvement.

The sleep () method is a class method, that is, for the current thread, the programmer cannot specify a thread to sleep, but only the current line
When the thread runs to the sleep () method, the specified sleep time (for other threads to run). In fact, it can only be a class method and called on the current thread.
If you call the sleep () method of a thread object, if the thread corresponding to this object is not running, how does it sleep ()?
So only the current thread, because it is being executed, can you ensure that it can call the sleep () method.

Principle: [try not to call the thread's sleep () method in the synchronous method], or simply put, for average-level programmers, you are basically
The sleep () method should not be called.

The join () method, as stated in section 1, calling the join method on a thread object is the corresponding
The thread ends. For example, if there are two jobs, it takes 10 seconds for job A and 10 seconds for Job B.
We first generate a thread in the program to do work B, and then do work.

New B (). Start (); // do work B
A (); // do work

After job A is completed, I will wait for the result of Job B to handle it. If Job B is not completed, I will not be able to perform the following job C, so

B = new B ();
B. Start (); // do B
A (); // do work
B. Join (); // wait for work B to complete.
C (); // continue working C.

Principle: [join is the only correct way to test other working states]. I have seen many people, even some doctoral students, working on a job.
If another job is not completed, say let the current working thread sleep (x), I asked him, how did you specify this X, how do you know it is 100 milliseconds
Not 99 Ms or 101 Ms? In fact, this is the essence of the onxxx event. We don't have to wait for a long time to do anything, but wait
Work is done exactly when the work is completed.

The yield () method is also a class method. It is called only on the current thread for the same reason. The main reason is to let the current thread discard the time slice allocated this time.
Principle: [There is no reason to call this method unless necessary]. Calling this method does not increase any efficiency, but reduces the total CPU cycle.

The thread methods described above can only be briefly mentioned based on (basic). In the future, I will discuss them in detail with examples.

For other methods of the thread, see the API documentation. The next section describes non-thread methods, but the two [three] object methods closely related to the thread:
[Wait (), notify ()/notifyall ()]

This is a very important method in multithreading.

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.