Tag: size Consumer mode consumer reason end cap should have no exe
Original address: http://hellojava.info/?p=464
Recently a fault, the time spent a long time, review the whole process of investigation, Empiricism here played a bad role, directly led to the entire troubleshooting time is very long, the root cause of this failure is blockingqueue with the problem, Incidentally, say a few blockingqueue:arrayblockingqueue, Linkedblockingqueue, and synchronousqueue that are commonly used in Java.
At that time, the problem is that the application processing request of the thread pool is full, so that the request can not be processed, so the dump thread, see what the thread is doing, the results found that the thread is block in the writing log place, there have been a lot of problems before, to thread dump see also a bunch of block in the log, But it is usually caused by other reasons, so this time is also according to such experience, that certainly will not be the issue of writing log this place, so various troubleshooting ... Toss over n long, looking back to find that the place to hold the log lock is one of the code, that code after the log lock, from the thread stack, see, Block in the Arrayblockingqueue.put this place, and then look at this code, and found that this is a 1024-length blockingqueue, which means that if the queue is placed 1024 objects, the put will be block, and actually the code of the When you can see the code of the classmate is to consider the blockingqueue if the full should be processed, the code reads:
if (blockingQueue.remainingCapacity() < 1) { //todo } blockingQueue.put
Here are two sad questions, one is that if the judgment is finished or go directly to put, rather than else, the second is the key is full of the processing logic is still in//todo ...
In addition, I think this code also reflects the classmate of the Blockingqueue interface is not very familiar, to achieve this effect, do not need to judge the first, more appropriate practice is to use Blockingqueue.offer, return false and then do the corresponding exception handling.
Blockingqueue is a data structure that is often used in production/consumer mode and is typically used primarily by arrayblockingqueue, Linkedblockingqueue, and Synchronousqueue.
Arrayblockingqeue/linkedblockingqueue the biggest difference between the two is the way the object is stored in the queue, one is an array, one is a linked list, and the code note also writes the difference between the two:
Linked queues typically has higher throughput than array-based queues but less predictable performance in most concurrent Applications.
Synchronousqueue is a very special blockingqueue, its pattern is when the offer, if no other thread is taking or poll, then the offer will fail; If no other thread is in the way of an offer, it will fail, and this particular pattern is ideal for a queue that requires a high response and threads out of a thread pool that is not fixed.
For online business scenarios, where all concurrency, external access is blocked, one truth is that there must be a timeout mechanism, and I don't know how many times I've seen it. Because there is no time-out caused by the serious failure of the online business, the online business is most emphasis on the rapid processing of a request, so fail fast is the online business system design, The most important principle in code writing, according to this principle the least obvious mistake is to use put instead of the offer with the timeout mechanism, or if it is not important, it should be directly used Offer,false directly throw the exception or record the exception.
For Blockingqueue this scenario, in addition to the timeout mechanism, there is a queue length must be limited, otherwise the default is Integer.max_value, in case the code out of the bug, memory is played hang.
Speaking of Blockingqueue, it is still necessary to mention the Blockingqueue is used most places: Thread pool, Java Threadpoolexecutor has a parameter is Blockingqueue, If this place is Arrayblockingqueue or linkedblockingqueue, and the thread pool coresize and poolsize are different, after the coresize thread is full, This time the thread pool will first do is offer to Blockingqueue, the success of the end, this scenario also does not meet the needs of the online business, the online business is more likely to be faster processing, rather than queuing first, and in fact, the online business is best not to let the request heap in the queue, Online business This can easily lead to avalanches, beyond the scope of processing ability to directly refuse to throw the wrong is a relatively good practice, as for the queue on the front page what this is possible, it is another kind of current-limiting mechanism.
So in addition to the system design, the skill of code detail is very important when writing high concurrency and distributed code.
Three blockingqueue from a fault talk about Java