Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number of Threads in Monotouch application

I'm using Task and TaskCompletionSource code in my application in scenarios that are frequently invoked, such as downloading a images from the Internet asynchronously from a 'scrolling table view'. This allows me to write async/await code without touching the UI thread for downloading/caching operations.

e.g.:

public override Task<object> GetCachedImage (string key)
    {
        UIImage inMemoryImage = sdImageCache.ImageFromMemoryCache (key);

        //
        // Return synchronously since the image was found in the memory cache.
        if (inMemoryImage != null) {
            return Task.FromResult ((object)inMemoryImage);
        }

        TaskCompletionSource<object> tsc = new TaskCompletionSource<object> ();

        //
        // Query the disk cache asynchronously, invoking the result asynchronously.
        sdImageCache.QueryDiskCache (key, (image, cacheType) => {
            tsc.TrySetResult (image);
        });

        return tsc.Task;
    }

The GetCachedImage is invoked multiple times because a table view may have lots of images to be downloaded and the user may scroll the table view as well. The Task itself does not take too long to be performed (in some cases the result is returned synchronously), so I'd expect that the system create a lot of threads but also REUSE them. However I'm seeing in the console the following output:

Thread finished: <Thread Pool> #149

The number of threads always get bigger and I'm worried my application is creating too many threads and may get stuck because of that after a long period of usage. What does the Thread finished: <Thread Pool> #149 mean? Are threads being created and destroyed? Are threads being reused? Does my application have #149 live threads? Can (should) I limit the max number of threads?


EDIT

As suggested by @usr I ran my application again and stopped the debugger to see how many threads were there, see the screenshots: Debugger threadsDebugger console

Looks like 38 threads were created, but some of them were destroyed, I am right? Does that mean that the Thread finished: <Thread Pool> #... message will always appear with a bigger number as long as the application is running? Why aren't threads re-used ?

like image 644
Eduardo Coelho Avatar asked Oct 02 '22 05:10

Eduardo Coelho


1 Answers

The thread number shown in the Application Output is the nth thread created, not the nth running thread.

For example:

Thread started: <Thread Pool> #123

means that in the entire app's lifetime 123 threads have started. It does not say anything about how many threads are currently running.

The opposite message:

Thread finished: <Thread Pool> #123

means that this thread has now exited.

And if you now create a new thread, you'll see this:

Thread started: <Thread Pool> #124

This means that to know how many threads are currently running, you can count the number of "Thread started" lines and subtract the number of "Thread finished" lines.

An alternative solution is to to just look at the Threads pad (like your screenshot), and count the number of threads there instead.

like image 149
Rolf Bjarne Kvinge Avatar answered Oct 06 '22 00:10

Rolf Bjarne Kvinge