Why do thread pools use blocking queues? Can you complete a task with a general queue?
Why do thread pools use blocking queues? Can you complete a task with a general queue?
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.
Previous: Input text box can not be entered, how to solve the problem
Next: Can background templates developed in vue be packaged and released to npm like components?
read a section of the concurrent programming network http: ifeve.com volatile Volatile is a lightweight synchronized, that ensures the " visibility "of shared variables in multiprocessor development . Visibility means that when one thread modifies a...
problem description in Chapter 7 of "Java concurrent programming practice ", the author implements thread cancellation through newTaskFor method encapsulation. The demo given is as follows: public abstract class SocketUsingTask <T> implement...