Why do Java thread pools use blocking queues?

Why do thread pools use blocking queues? Can you complete a task with a general queue?

Mar.16,2021

blocking queues are mainly used in the case of the producer-consumer model.
for example, if a thread fetches an element from an empty blocking queue, the thread will be blocked until there is an element in the blocking queue. When there are elements in the queue, the blocked thread is automatically awakened (there is no need for us to write code to wake up). This provides great convenience.
if you use a non-blocking queue, it will not block the current thread, so you must implement the Synchronize policy and the inter-thread wake-up policy in addition, which is very troublesome to implement.


in a simple scenario, consumers may not always submit tasks and execute them immediately. There may be many factors, such as computing or IO resources cannot be put in place immediately, so there is waiting, but the task queue may not be infinite, so there is a blocking queue. When the task pool is full and there are many tasks in the queue, Need to block and wait


A general queue can only be guaranteed as a buffer of limited length. If the buffer length is exceeded, the current task cannot be retained. The blocking queue can retain the current task that you want to continue to join the queue through blocking.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e81c47-b81.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e81c47-b81.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?