What does "long-running tasks" mean?

23,287

In general thread pooling, you distinguish short-running and long-running threads based on the comparison between their start-up time and run time.

Threads generally take some time to be created and get up to the point where they can start running your code.

The means that if you run a large number of threads where they each take a minute to start but only run for a second (not accurate times but the intent here is simply to show the relationship), the run time of each will be swamped by the time taken to get them going in the first place.

That's one of the reasons for using a thread pool: the threads aren't terminated once their work is done. Instead, they hang around to be reused so that the start-up time isn't incurred again.

So, in that sense, a long running thread is one whose run time is far greater than the time required to start it. In that case, the start-up time is far less important than it is for short running threads.

Conversely, short running threads are ones whose run time is less than or comparable to the start-up time.


For .NET specifically, it's a little different in operation. The thread pooling code will, once it's reached the minimum number of threads, attempt to limit thread creation to one per half-second.

Hence, if you know your thread is going to be long running, you should notify the scheduler so that it can adjust itself accordingly. This will probably mean just creating a new thread rather than grabbing one from the pool, so that the pool can be left to service short-running tasks as intended (no guarantees on that behaviour but it would make sense to do it that way).

However, that doesn't change the meaning of long-running and short-running, all it means is that there's some threshold at which it makes sense to distinguish between the two. For .NET, I would suggest the half-second figure would be a decent choice.

Share:
23,287
Person.Junkie
Author by

Person.Junkie

Updated on September 15, 2020

Comments

  • Person.Junkie
    Person.Junkie over 3 years

    By default, the CLR runs tasks on pooled threads, which is ideal for short-running compute-bound work. For longer-running and blocking operations, you can prevent use of a pooled thread as follows:

    Task task = Task.Factory.StartNew (() => ...,
    TaskCreationOptions.LongRunning);
    

    I am reading topic about thread and task. Can you explain to me what are "long[er]-running" and "short-running" tasks?

  • Mick
    Mick over 9 years
    And I thought there were no threads... blog.stephencleary.com/2013/11/there-is-no-thread.html
  • paxdiablo
    paxdiablo over 9 years
    Mick, that link talks about async I/O operations, there are plenty of threads in existence doing other things, such as things you may want to do when you call the task factory :-)
  • Mick
    Mick over 9 years
    I've done a lot of multi-threaded programming but not with TPL. So what distinguishes an Async operation from an Async I/O operation?
  • paxdiablo
    paxdiablo over 9 years
    @Mick, an async operation is anything that happens in parallel. Example, your word processor may be formatting text in one thread and spell-checking in another, neither of which need involve I/O.
  • Mick
    Mick over 9 years
    I didn't ask you what asynchronous means, to do so would be completely redundant.
  • paxdiablo
    paxdiablo over 9 years
    Mick, you asked what distinguished an async op from an async I/O op. Well, that would be the I/O bit as I explained. Beyond that, I'm not sure what your question could mean. Perhaps you might like to clarify.
  • Mick
    Mick over 9 years
    I guess my question should have been "So when does an Asychronous operation have no thread associated with it?". The answer is probably when the method "is implemented using the standard P/Invoke asynchronous I/O system in .NET, which is based on overlapped I/O (msdn.microsoft.com/en-us/library/…)." Which "starts a Win32 overlapped I/O operation on the device’s underlying HANDLE."