Objective
The Java part has the foundation, the design pattern, IO, NIO, Multi-threading, then has the time also will the collection this part to fill up, so many content inside, inevitably has some knowledge point omission, this article mainly is explains these omission the knowledge point. These points of knowledge, is not particularly difficult, so there is no need to write an article to explain, but these points of knowledge is not one or two words to say clearly, so put here. This article and the previous code optimization, class inheritance interface implementation, as well as a long-term update of the article, as long as the idea of missing knowledge points, will be updated at any time this article.
Does the Thread.Sleep (XXX) method consume the CPU?
This knowledge point is a knowledge point I have been wrong before, in my previous understanding, I have always thought that thread.sleep (1000) of this second time, the thread of sleep is always occupied by the CPU of the time slice hibernate, viewed the data and after careful thinking found not. Thread.Sleep (1000) Means: Code execution here, in 1 seconds I take a break, do not participate in the CPU competition, 1 seconds later I come to participate in CPU competition.
Speaking of which, it is necessary to mention the difference between sleep and wait, the JDK source code to provide us with the comments are very rigorous:
1 /** 2 * Causes the currently executing thread to sleep (temporarily cease3 * execution) for the specified number of milliseconds, subject to4 * The precision and accuracy of the system timers and schedulers. The thread5 * Does not lose ownership of any monitors.6 *7 * @paramMillis the length of time to sleep in milliseconds.8 * @exceptioninterruptedexception If any thread has interrupted9 * the current thread. The <i>interrupted status</i> of theTen * Current thread was cleared when this exception is thrown. One * @seeobject#notify () A */ - Public Static native voidSleepLongMillis)throwsInterruptedexception;
1 /**2 * Causes the current thread to wait until another thread invokes the3 * {@linkjava.lang.object#notify ()} method or the4 * {@linkJava.lang.object#notifyall ()} method for this Object. 5 * In other words, this method behaves exactly as if it simply6 * Performs the call <tt>wait (0) </tt>.7 * <p>8 * The current thread is must own this object ' s monitor. The thread9 * Releases ownership of this monitor and waits until another threadTen ... One */ A Public Final voidWait ()throwsinterruptedexception { -Wait (0); -}
Look at the 4th and 5th lines of the Sleep method, "the thread does not lose ownership of any monitors"
Look at lines 8th and 9th of the Wait method, "the thread releases ownership of this monitor"
So the difference is that the difference is in "Monitor " , which is the monitor, the execution of the sleep and wait methods will release the CPU resources, but the sleep method will not release the ownership of the monitor, and the wait method will release the ownership of the monitor. The so-called monitor is that if the sleep method and the wait method are in the synchronous method/synchronous method block, they hold the object lock
The role of Thread.Sleep (0)
To talk about this problem, first of all, the two methods of thread scheduling, Zhou Zhiming Teacher's "in-depth understanding of Java Virtual Machine: JVM advanced features and best Practices," chapter 12th 4th section of this content has a relatively clear explanation.
Thread scheduling refers to the process of assigning a processor to a thread, and there are two main scheduling methods: collaborative thread scheduling and preemptive thread scheduling .
1. Collaborative thread scheduling
Using a multithreaded system with collaborative thread scheduling, the execution time of a thread is controlled by the thread itself, and after the thread has done its job, the system is proactively notified to switch to another thread. The biggest benefit of this scheduling method is that it is simple to implement, and because the thread has to do its job to get the thread to switch, the switch operation is known to the thread itself, so there is no thread synchronization problem. However, the disadvantage of collaborative thread scheduling is also obvious: the thread execution time is not controlled. If a thread is writing a problem and never tells the system to switch threads, the program will always be stuck there. So this approach is very unstable, and a thread that insists on not giving up CPU execution time can cause the entire system to crash.
2. Preemptive thread scheduling
Preemptive thread scheduling is performed by the system to allocate execution time, and thread switching is not determined by the thread itself. In this way of implementing thread scheduling, the execution time of a thread is system-controllable and there is no problem that a thread is causing the entire process to block.
It is obvious that Java is taking preemptive scheduling. Java threads are implemented by mapping to the native thread of the system, and after a thread hangs or the CPU time is given to it, the operating system calculates a total priority based on thread priority, thread starvation, and then picks a thread to give it a time slice.
Speaking so much, back to our topic, I summarize two points:
1, the CPU split out the time slice, can compete the thread all will go to compete obtains
2. The thread that called the Thread.Sleep (XXX) method means that the thread does not participate in the CPU time slice in the time of the XXX millisecond competition
So what is Thread.Sleep (0) for? Its role is to force the operating system to trigger a CPU compute priority and allocate time slice actions . For example, thread a gains 5 milliseconds of CPU execution time, and if it encounters a thread.sleep (0) statement at 2 milliseconds, the subsequent 3 milliseconds of time slice will not run, the operating system recalculates the priority, and assigns the next CPU time slice to which thread.
This little detail is good for the system to run:
1, to avoid a thread for a long time to occupy the CPU resources, we know in Java, such as two non-daemon threads, thread priority 10 thread A and thread priority 5 thread B concurrently execute, which means that the operating system basically most of the time is running thread A, Basically does not give CPU control to thread B
2, avoid the system suspended animation
3, let the thread have a more average chance to get CPU resources
But Thread.Sleep (0) Although good, but do not abuse it. A system with high CPU usage is a good thing, which means that the CPU is doing things and not idle. But high CPU occupancy is also to ensure that the CPU is doing things that should be done , such as high CPU usage, but in the dead loop, is it meaningful? That's the problem with code writing. Thread.Sleep (0) Also, this sentence triggered the operating system to calculate the priority, the allocation of time slices of the action, is bound to occupy the CPU time, if in many threads abuse this method, CPU utilization is up, but most of the time to do is meaningless things. I think the purpose of this action is to optimize the system rather than part of the code that must be executed .
Leak check (long-term update)