Can I make my Thread pool create its threads as foreground?
Tag : chash , By : Daniel Reslie
Date : March 29 2020, 07:55 AM
To fix this issue The notion of a "background thread" means very little in .NET. The Thread.IsBackground property is just a simple flag that helps the CLR decide whether or not it should rudely abort the thread when the AppDomain gets unloaded or whether it should wait for the thread to exit voluntarily. You can just change the property if you want a thread-pool thread to keep running at program exit. This snippet shows how that's possible: using System;
using System.Threading;
class Program {
static void Main(string[] args) {
var sync = new ManualResetEvent(false);
ThreadPool.QueueUserWorkItem((_) => {
Console.WriteLine("Running");
Thread.CurrentThread.IsBackground = false;
sync.Set();
Thread.Sleep(5000);
Console.WriteLine("Done, should exit now");
Thread.Sleep(250);
});
sync.WaitOne(); // Ensures IsBackground is set
}
}
|
Spring WebFlux create pool of no-blocking threads
Tag : spring , By : silvervino
Date : March 29 2020, 07:55 AM
|
Why does a cached thread pool create two threads, and why does shutting it down change that?
Tag : java , By : user161314
Date : March 29 2020, 07:55 AM
With these it helps Your runnable crashes its thread. So the pool will replace that thread with a new one (for the next task you might submit). If the pool is already being shut down, it will not do that.
|
How to create threads in ASP.NET pages from CLR thread pool instead of ASP.NET pool?
Tag : chash , By : Debashree
Date : March 29 2020, 07:55 AM
|
Thread pool to create set number of threads per second
Tag : java , By : user171752
Date : March 29 2020, 07:55 AM
I wish this help you call a method 10 times per second on separate threads using a thread pool class MyClass{
static void myMethod(){
// some task
}
}
Runnable runnable = () ->{
for(int i=0; i<10; i++){
try{
Thread.sleep(1000);
MyClass.myMethod();
}catch{}
}
};
int threadCount = 10; //whatever number of threads you wish
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
executor.execute(runnable);
|