Basic methods and skills of thread in Java

The skilled use of the basic methods of threads in Java is the only way to master multi-threaded programming. The basic methods related to threads include wait, notify, notifyAll, sleep, join, yield, etc. This paper briefly introduces their usage.

Thread state diagram

Java refers to the ready and running states of the operating system as runnable states. The thread states in Java can be considered as the above six states.

wait

The thread calling this method enters the waiting state. It will return only after waiting for the notification of another thread or being interrupted. It should be noted that after calling the wait () method, the lock of the object will be released.

Therefore, the wait method is generally used in synchronization methods or synchronization code blocks.

sleep

Sleep causes the current thread to sleep. Unlike the wait method, sleep does not release the lock currently held. Sleep (long) causes the thread to enter the timed-waiting state, while the wait () method causes the current thread to enter the waiting state

yield

Yield will cause the current thread to give up the CPU execution time slice and compete for the CPU time slice again with other threads. Generally, threads with high priority are more likely to successfully compete for CPU time slices, but this is not absolute. Some operating systems are not sensitive to thread priority.

interrupt

Interrupting a thread is intended to give the thread a notification signal, which will affect an interrupt identification bit inside the thread. The thread itself will not change its state (such as blocking, termination, etc.).

1. Calling the interrupt () method does not interrupt a running thread. In other words, the running thread will not be terminated because it is interrupted, but only the interrupt identification bit maintained internally has been changed.

2. If the thread is in timed-waiting state by calling sleep (), the interrupt () method will throw an interruptedexception, which will cause the thread to end the timed-waiting state in advance.

3. many ways to declare InterruptedException are thrown out, such as Thread.sleep (long mills method). Before throwing an exception, the interrupt identifier is cleared. After throwing an exception, the isInterrupted () method will return to false.

4. Interrupt status is an inherent identification bit of a thread, which can be used to safely terminate the thread. For example, when you want to terminate a thread, you can call thread Interrupt () method. In the run method of the thread, you can use thread The value of isinterrupted() to gracefully terminate the thread. Of course, you can maintain a boolean variable inside the thread to control the operation and termination of the thread.

Now, let's take a look at how this method is described in the source code.

It's basically very simple. First check the security permissions of the current thread on this thread. If modification is not allowed, an exception will be thrown. Then lock and synchronously set the interrupt identification bit. Although there is a detailed description in the method declaration, it can not be seen in the code. After the thread in the waiting state is interrupted, the interrupt ID will be reset. I think this is achieved by the native code interrupt0.

join

Thread B. join() statement is executed in the context of thread a, which means that after the execution of thread B, the join() method will return and thread a can continue to execute.

For example, if you create 10 threads and call the start () method to execute the threads at the same time, but you want them to execute orderly, you can use join to assist in completion. The code is as follows:

summary

The above is the basic methods and skills of threads in Java introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>