-2

How can I specify maximum number of tasks executed in threadpool and discard tasks if queue (queues) is full? Should I use TaskSheduler somehow? I found Java solution for my problem (http server that rejects requests (503 unavalaible) if he reaches his maximum load), but can't figure it out how it may be implemented in C# and .Net.

Java version (source):

public static ExecutorService newBoundedFixedThreadPool(int nThreads, int capacity) {
    return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS,
        new LinkedBlockingQueue<Runnable>(capacity),
        new ThreadPoolExecutor.DiscardPolicy());
}

public static void boundedThreadPoolServerSocket() throws IOException {
    ServerSocket listener = new ServerSocket(8080);
    ExecutorService executor = newBoundedFixedThreadPool(4, 16);
    try {
        while (true) {
            Socket socket = listener.accept();
            executor.submit( new HandleRequestRunnable(socket) );
        }
    } finally {
        listener.close();
    }
}

EDIT (Clarification, if it helps): When you set MaxThreads number it just limits number of threads in threadpool. In my situation (some small service that handles user file and gives it back - http server) I need to reject tasks (Task or Task) if queue to this threads in threadpool reaches some maximum number.

ence ladus
  • 62
  • 7

1 Answers1

-1

You can set the SetMaxThreads property of the ThreadPool object.

https://msdn.microsoft.com/en-us/library/system.threading.threadpool.setmaxthreads(v=vs.110).aspx

You can check the available threads in the Threadpool via the GetAvailableThreads property.

https://msdn.microsoft.com/en-us/library/system.threading.threadpool.getavailablethreads(v=vs.110).aspx

Your Service could check this property and reject incoming tasks if either the workerThreads and completionPortThreads equal to zero

Difference between the 2 properties is best explained in this post:

"Worker threads are to be used for active work, such as when you post a work item to the ThreadPool. I/O completion threads are used to wait for asychronous I/O to complete, for example waiting for data to be received on the network. I/O completion threads don't spend much time doing stuff, they simply block until they receive a signal.

The reason there are two groups of threads in the ThreadPool is because of the work they do. Since I/O threads may block for longer periods its not ideal to share these threads with an active work queue. This all comes down to optimizing the number of threads to the number of cores available in the system."

https://social.msdn.microsoft.com/Forums/sqlserver/en-US/bad3458c-4cb8-4018-8177-941fb1947fdf/whats-differences-between-workerthreads-and-completionportthreads-of-threadpoolsetmaxthreads?forum=netfxbcl

Further reading: Simple description of worker and I/O threads in .NET

Francis
  • 1,798
  • 1
  • 26
  • 32
  • How this answers "*How can I specify maximum number of tasks executed in threadpool **and discard tasks if queue (queues) is full?***" – Eser Feb 04 '18 at 21:06
  • Hello, thanks for answer. My question is about a little different situation. When you set MaxThreads number it just limits number of threads in threadpool. In my situation (some small service that handles user file and give it back - http server) I need to reject tasks (Task or Task) if queue to this threads in threadpool reaches some maximum number. – ence ladus Feb 04 '18 at 21:10
  • @Eser, you're absolutely right. I should read the question more carefully next time. – Francis Feb 05 '18 at 04:51
  • @enceladus : so to understand you correctly, you have a service that accepts jobs/tasks (file processing) and it's within this service that enqueues a job to the Threadpool? And you want the service to reject the jobs/tasks out right if the Threadpool is full after some check, instead of just queuing it up? – Francis Feb 05 '18 at 04:58
  • Or is it the tasks running in the Threadpool you want to cancel instead of rejecting the incoming tasks? – Francis Feb 05 '18 at 05:07
  • @Francis it's first – ence ladus Feb 05 '18 at 07:15
  • @enceladus I've attempted another answer. Let me know if I've missed any requirements or prerequisites . – Francis Feb 05 '18 at 10:08
  • @Francis, I want to specify ThreadPool queue size to worker threads like in Java version above. If there is no avalaible threads I don't want to discard jobs. I want queue this jobs until maximum queue size is not reached and just then discard them if queue is full and threads are busy. – ence ladus Feb 05 '18 at 10:08
  • So you have another queue outside the Threadpool that receives the task initially, and you're dispatching tasks from this queue to the Threadpool until the Threadpool is full. And will start rejecting jobs if this queue gets full up whilst the Threadpool is full? – Francis Feb 05 '18 at 10:28
  • 1
    So I think I figured it out how it can be implemented. I'll edit my answer and give a link to implementation for situtation if somebody will need it in the future. – ence ladus Feb 05 '18 at 11:39