Computation programming in Java (1)

xiaoxiao2021-03-05  23

Multi-threaded can design more loose-coupled applications

Basic thread

To create threads, the easiest way is to inherit java.lang.thread.

Run () is the most important way for Thread, which must override this method. Run () is included in the code to "simultaneously" execute in the program.

Usually Run () is an unlimited loop, that is, planing the accident that will stop Run (), the thread will run.

Thread's Start () method will initialize threads and then call Run ().

The entire step should be: Call the constructor to create an object and call start () in the constructor to configure this thread, and then let the thread's execution mechanism call Run (). If you do not call START (), thread never start.

The scheduling mechanism of the thread is non-decisive, so the output is different when the program is running.

Created Thread, but didn't take it about it. If it is a normal object, this is enough to make it garbage, but Thread will not. Thread will "register" itself, so it is actually reserved in a place. The garbage collector cannot move it unless Run () exits.

Yielding

If you know that Run () has already filed a fall, you can give the thread scheduling machine as a hint, tell it that you are finished, allowing other threads to use the CPU. This hint (note, just hint - unable to ensure that this JVM you use will reflect this) is given in yield ().

Java's thread scheduling mechanism is a preemptive, that is, it will interrupt the current thread at any time as it is considered necessary, and switch to other threads. Therefore, if I / O (via main () thread is performed is too long, the thread scheduling mechanism will stop before run () runs to Yield (). In short, yield () will only work with few situations, and cannot be used to conduct very serious adjustments.

Sleeping

There is also a way to control threads, just use sleep () to stop a period of time in milliseconds. Sleep () must be placed in the TRY domain, because it is possible that there is no time to be SLEEP () is interrupted.

If someone gets the thread's Reference, and calls its interrupt (), this happens. (Interrupt () also affects threads in a wait () or JOIN () state, so these two methods must be placed in the TRY field.) If you are ready to wake up with the interrupt (), then it is best to use Wait ( ) Instead of sleep (), because the Catch statements of both are different.

Each thread uses join (), main () before the implementation of the next step, first wait until the end of this thread. If there is no join (), thread is still running in a random manner, that is, Sleep () is not a way to control thread execution. It is just suspend threads. The only thing that can be guaranteed is that it will sleep at least 100 milliseconds, but it will resume the time spending time, because after the end of sleep, the thread scheduling mechanism will take the time to take over.

If you must control the execution order of the thread, the most thorough approach is still no thread. You can write a collaboration procedure to let it exchange the rule of the program in a certain order.

priority

The priority of the thread is to tell the thread scheduling mechanism. Although the order in which the CPU server thread is non-decisive, if there are many threaded plugs waiting to start, the thread scheduling mechanism will tend to start the highest priority thread. But this does not mean that the low priority thread has no chance to run (that is, the priority will not cause a deadlock). The priority is low, only the opportunity to operate is less. The priority of thread 1 is set to the highest level, while the priority of other threads is located at the lowest level.

Setting the variable D to Volatile to exclude optimization. Otherwise, you can't see the effect of the priority. Through the operation, you can see that the thread scheduling mechanism arranges the most opportunities for threads 1. Although the output of the console is also a very "expensive" operation, you can't use it to observe the effect of the priority, because the thread is not interrupted when printing (otherwise the display of the console is chaos ), But mathematical operation can be interrupted. For thread scheduling mechanisms, the time consumption of this operation is sufficient to keep it a high priority of thread 1.

In addition, you can also use getPriority () to read the thread's priority, modify the priority of the thread at any time (not like SimplePriorIities.java, etc.).

Daemon

The so-called "daemon thread" means that as long as the program is still running, it should provide a thread for a public service in the background, but the guardian thread does not belong to the core part of the program.

Therefore, when all the non-standard threads are running, the program is over. Instead, as long as there is a non-guarded thread running, the program cannot end. For example, the thread running main () is a non-daemon. To create a daemon, setDaemon () must be set before it starts.

You can use IsDaemon () to determine whether a thread is a daemon. The thread created by the guarding thread is also automatically a daemon.

Join thread

The thread can also call another thread JOIN (), and then continue to run after the end of the thread. If the thread calls T. Join () called another thread T, then before the thread T ends (the criteria is equal to FALSE), the calling thread is hang.

When you call Join (), you can give a Timeout parameter, (can be in milliseconds, or in nanosecond unit), so if the target thread has not ended after the time limit expires, Join () will force return.

Join () calls can be interrupted by the interrupt () of the calling thread, so Join () should also be enclosed in try-catch.

转载请注明原文地址:https://www.9cbs.com/read-39103.html

New Post(0)