java - pthreads是PHP多线程的东西。。我有一个奇葩的问题需要问和讨论
巴扎黑
巴扎黑 2017-04-17 11:58:11
0
7
791

比如说JAVA吧!JAVA是有多线程的。
既然有了多线程,为什么还需要队列这个东西呢?
比如发邮件吧!
直接新开一个线程发邮件不就行了。为什么要放到(异步)队列里面呢?

巴扎黑
巴扎黑

reply all(7)
小葫芦

The thread has a bottleneck, and the queue can be processed slowly first.

黄舟

For a simple example, you have a batch of tasks at this time, but the characteristics are as follows:
1. Long execution time
2. Don’t care about the execution results

If there are a large number of tasks like this, you should choose multi-threading (that is, one task starts a thread). Then the CPU will eventually be "busy" in context switching due to too many threads.
Since you don't care about the execution results, you can put all the tasks into the queue and use a thread pool to execute them. For example, if you only execute 4 tasks at the same time, then you will only have 4 threads globally, context switching is fast, and tasks are orderly. Completed execution. It's just that the efficiency may not be as fast as multi-threading to some extent.

For this difference, please refer to the difference between tomcat or apache httpd and nginx or node.

Ty80

In order to buffer, for example, when the request you submit is not stable, you can have a buffer through the queue, so that the data can be backlogged into the queue first

迷茫

Multiple threads and queues are not the same thing
Multithreading is unstable.

大家讲道理

Thread

You need to consider memory safety issues, scheduling and many other issues, which are relatively uncontrollable.

Queue

The execution frequency and order are within controllable range, so it is very suitable for buffer zone~

伊谢尔伦

You will know when you need to store dozens of gigabytes of data in memory

小葫芦

When you were learning operating systems, did you learn a producer-consumer model? The producer produces products and puts them into the queue, and the consumer reads data from the queue for consumption. Why put the two actions of production and consumption into two threads? If you understand this principle, you will understand why queues are used.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!