Multithreaded details issues
Similarities and differences between sleep method and wait method?
Similarities:
Leave the thread in a frozen state.
Differences:
Sleep must specify the time
wait can specify time or not
The sleep time is up, the thread is in a temporary blocking state or running state
If wait does not have time, it must be awakened through notify or notifyAll
Sleep doesn't have to be defined in synchronization
wait must be defined in synchronization
When all are defined in synchronization
Sleep releases execution rights, not locks
wait to release execution rights, lock
syschronized(obj) { wait();// 0 1 2 code... } syschronized(obj) { notifyAll();// 3 code... }How to stop a thread
Stop method
The stop method is outdated. I found that there are other solutions after reading the description.
Thread end: let the thread task code be executed and the run method ends
How to end the run method?
By defining a loop
Note: Can he still judge the mark if the thread is in a frozen state in the task?
The so-called interrupt state does not mean stopping the thread.
interrupt interrupt state causes if the target thread waits for a long time, the interrupt method should be used to interrupt the wait for the so-called interrupt is not to stop the thread.
The function of interrupt is to clear the freezing state of the thread and restore the running state to the thread (let the thread re-qualify for CPU execution).
Because it is mandatory, there will be an exception InterruptedException, and you can catch exceptions in catch.
In exception handling, change the mark to end the loop and let the run method end.
Daemon thread
Daemon thread: It can also be understood as a background thread, and the foreground threads created before are all foreground threads.
As long as the thread calls setDaemon(true); the thread can be marked as a daemon thread.
The front-end background threads are the same when running, obtaining the CPU's execution rights.
Only when it ends is a little different.
The foreground thread must end through the run method and the thread must end.
The background thread can also end with the run method, and the thread ends. There is another situation.
When all foreground threads in the process end, no matter what state the background thread is in, it will end, and the process will end.
The process ends with the foreground threads all depend on.
Thread priority
Thread priority: identified by numbers, 1-10
Among them, the default initial priority is 5 and the most obvious three priority levels 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 identified through the Thread constructor.
The benefits of thread groups are that they can perform unified operations on multiple threads in the same group.
By default, they all belong to the main thread group.
Anonymous internal class
Runnable rn = new Runnable() { public void run() { }};//The above code is equivalent to class Anomymous implements Runnable { public void run() { }}Runnable rn = new Anomymous();Thank you for reading, I hope it can help you. Thank you for your support for this site!