Analysis on key points of Java multithreading

Multithreading details

What are the similarities and differences between sleep method and wait method?

Similarities:

Leave the thread frozen

difference:

Sleep must specify the time. Wait can specify the time or not

When the sleep time expires, the thread is temporarily blocked or running. If there is no time, it must wake up through notify or notifyAll

Sleep does not have to be defined in synchronization. Wait must be defined in synchronization

Both are defined in synchronization. Sleep releases the execution right, does not release the lock, wait releases the execution right, and releases the lock

How do threads stop

Stop method

The stop method is outdated. Look at the description and find that there are other solutions Thread end: it means that the thread task code is executed. The run method ends. How does the run method end? By defining cycles

Note: if the thread is frozen in the task, can it still judge the flag? The so - called interrupt state is not to stop the thread Interrupt state means that if the target thread waits for a long time, the interrupt method should be used to interrupt the wait. The so-called interrupt does not stop the thread. The function of interrupt is to clear the frozen state of the thread and restore the thread to its running state (make the thread qualified for CPU execution again). Because it is mandatory, there will be an exception interruptedexception. Exceptions can be caught in catch. In exception handling, change the flag to end the loop and the run method.

Daemon thread

Daemon thread: it can also be understood as background thread. Previously created threads are foreground threads. As long as the thread calls setdaemon (true); You can mark the thread as a daemon. The foreground and background threads are the same when running, and obtain the execution right of the CPU. It's only a little different at the end. The foreground thread ends through the run method. The background thread can also end through the run method, and the thread ends. In another case, when all the foreground threads in the process end, no matter what state the background thread is in, it will end, and the process will end. The end of the process depends on the foreground thread.

thread priority

Thread priority: digitally identified, 1-10. The default initial priority is 5. The three most obvious priorities are 1, 5 and 10. setPriority(Thread.MAX_PRIORITY);

Thread group

Thread group: ThreadGroup: the thread group to which the new thread object belongs can be specified through the constructor of thread. The advantage of thread group is that multiple threads in the same group can be operated uniformly. All belong to the main thread group by default.

Anonymous Inner Class

Thank you for reading, hope to help you, thank you for your support to this site!

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
分享
二维码
< <上一篇
下一篇>>