chris
chris

Reputation: 111

Is prioritization of tasks in Threadpool possible in .NET?

Is prioritization of tasks in Threadpool possible in .NET?

I have a list of.. A LOT.. of Actions. I want to run them in threadpool.. but. What I'm trying to do, is to setup the threadpool, so it works this way:

  1. Give the first action/task the highest priority - focus only on first task.
  2. Rest of tasks will get lowest priority.
  3. When first task is finished, set the highest priority to the second task.
  4. When second is finished, set the highest priority to the third task, etc etc.

It's like when you want to download the entire season of TV Show from torrents (which I don't recommend) and start watching as soon as possible.

Can I set up a threadpool like this? Or is there a better solution than threadpool?

I was trying to do many solutions, but everything was "too manual". Like, running separate Task.Factory.StartNew thread along side ThreadPool. Or running threads only through Task.Factory.StartNew.

EDIT: Ok, looking at the first comment here, maybe I wasn't clear enough. My single task/action is multithreaded. It needs 3-4 threads. Running 100,000 of such tasks in a sync queue doesn't make sense, because I will use just 3-4 threads at the time. What I'm trying is: (ok, let's say we have a queue) 1st task in the queue gets everything it wants (up to 4 threads, right?)... I still have 4 free threads (or more, depending on machine). I don't want to waste them so I want to run other tasks there... is it crazy?

EDIT2: Right now I use just a threadpool. It's really bad. I have 8 core-cpu. I started 40 tasks just for a test. It computes almost 40 at once... Meaning, for the first result I need to wait 10min (it's 30s/task running sync)

Upvotes: 5

Views: 3525

Answers (2)

zmbq
zmbq

Reputation: 39013

Allocating 4 remaining threads to 100,000 tasks seems inefficient, especially since you'll want to rush the 2nd task once the 1st task is done. Why don't you do something else - allocate 4 threads for the first task, 4 threads for the second task and so on until you run out of CPUs. Once one of the tasks is done, allocate threads for the next in queue.

Upvotes: 0

Kapoor
Kapoor

Reputation: 1428

Thread pool is especially designed for recycling of threads, therefore altering the priority will interfere with this philosophy.

However, while working with tasks you can do so by creating a an explicit thread in a custom made TaskScheduler

Windows supports seven relative thread priorities: Idle, Lowest, Below Normal, Normal, Above Normal, Highest, and Time-Critical.

High level approach -

  1. Create a custom TaskScheduler by extenting TaskScheduler.
  2. The key point here is that TaskScheduler will use a separate thread (with the priority you desire) to process the en-queued tasks
  3. Task.Factory.StartNew will use this custom TaskScheduler to execute the task.

Upvotes: 1

Related Questions