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.