问题描述:

Looking at the source code of the Volley library, I'm curious of how exactly does the "cleanup" of the dispatcher threads work. A RequestQueue contains one CacheDispatcher instance and an array of NetworkDispatcher instances. Both of these dispatcher classes extend Thread and are started as soon as they are created. After that, they run in an infinite loop until their quit() methods are called.

My question is, how are those threads actually stopped (i.e. what, if anything, prevents them from running indefinitely until the system kills the app), for example when the user leaves the application. The quit() methods of the dispatchers is called only from the RequestQueues stop() method, but that method itself isn't called from anywhere in the Volley, aside from the start() method to cleanup the possible previous dispatchers before initializing the new ones.

What I am sort of aiming at with the question is how much of an issue are the idle threads, and how they behave when the task (i.e. a group of activities) goes from the foreground to the background and hangs around for a while. Androids task switcher can hold quite a bit of apps (altough some of them might have been ejected/stopped), and most of them presumably use libs like volley/okhttp/picasso which have their thread pools. In theory that could add up to quite a lot of threads (albeit idle).

I understand this is a fairly low-level question and would probably require a lot of theory to explain correctly, if someone can provide a satisfactory answer touching on the bold stuff above, I'd be happy to accept it.

相关阅读:
Top