Long-running task vs. threads -- performance

Viewed 4824

Let's say I have some long running background jobs. Each job will do some work, then grab the next job and run it, and continue until the end of time.

This is currently implemented using Tasks. I have a JobStream which runs one job at a time within a loop. I could have 5, 15, or 50 of these streams running at once, depending on load.

JobManager

public Task Run(CancellationToken cancellationToken) {
    var jobTasks = Enumerable
        .Range(0, _config.BackgroundProcessor.MaximumSimultaneousJobs)
        .Select(o => JobStream.StartNew(..., () => RunNextJob(cancellationToken), cancellationToken));

    return Task.WhenAll(jobTasks);
}

JobStream

public static Task StartNew(Func<Task> nextJobRunner, CancellationToken cancellationToken) {
    var jobStream = new JobStream(nextJobRunner, cancellationToken);

    return jobStream.Start();
}

private Task Start() {
    return Task.Run(async () => {
        do {
            await _nextJobRunner();
        } while (!_cancellationToken.IsCancellationRequested);
    });
}

My question is, are tasks a good move here, or should I just create threads the old fashioned way? I'm mostly concerned about performance and ensuring that the jobs can run independently without getting tied up because another is grinding hard.

2 Answers
Related