简体   繁体   中英

Availability of threads in a thread pool?

How to find 60%(Or N%) availability of threads from a thread pool? What is the logic behind this?
Parent thread spawning multiple urls using thread pool threads and waiting for the completion of all child threads.

Code is given below;

 public void  Save()
 {
  List<Job> Jobs = PickJobs();

  int workerThreads = 0,compThreads = 0;
  ThreadPool.GetMinThreads(workerThreads, compThreads);

  int requiredThreads = 15;
  ThreadPool.SetMaxThreads(requiredThreads, compThreads);

  WaitCallback waitCallBack = default(WaitCallback);
  ManualResetEvent mEvent = default(ManualResetEvent);

 foreach (Job _job in Jobs) 
   {
   waitCallBack = new WaitCallback(CallBackFunc);
   mEvent = new ManualResetEvent(false);
   events.Add(mEvent);
   ThreadPool.QueueUserWorkItem(waitCallBack, new UrlData(_job, mEvent, HttpContext.Current));
   }
     WaitHandle.WaitAll(events.ToArray(), 300000);//05 Minutes
 }


Child Threads

private void CallBackFunc(object obj)
{
     UrlData msgObj = (UrlData)obj;
   WebRequest lWebRequest = WebRequest.Create(psUrl);
   lWebRequest.Timeout = 60000;
   WebResponse lWebResponse = lWebRequest.GetResponse;

    msgObj.FinishEvent.Set();
}


Object data for communication across threads

public class UrlData
{
public Job job;
public ManualResetEvent FinishEvent;
public HttpContext HttpContextRef;

public UrlData(Job pJob, ManualResetEvent pEvent, HttpContext pContext)
  {
      job= pJob;
      FinishEvent = pEvent;
      HttpContextRef = pContext;
  }
}


In above code, the required threads are hard coded as:

int requiredThreads = 15;
ThreadPool.SetMaxThreads(requiredThreads, compThreads);

Will this hard coding leads to threadpool starvation? And what happens if no threads are available in threadpool? How to find the total number of threads available in a threadpool in a hosting server?

Thanks.

After some research I ended up with a Max Thread Count retrieval function.
Which returns number of threads available in thread pool for a moment. If thread starvation occurs, it returns zero.

/// <returns>int (Number of threads currently available)</returns>
private int GetMaxItemsRetrievalCount()
{
int rtnVal = 1;
try {
    //Get Available idle threads currently in the thead pool.
    ThreadPool.GetAvailableThreads(workerThreads, completionThreads);

    rtnVal = workerThreads > MaxConcurrentThreads ? MaxConcurrentThreads : workerThreads;
    //Math.Min(MaxConcurrentThreads - currentWorkLength, workerThreads - ThreadBuffer);

    rtnVal = rtnVal > 0 ? rtnVal : 0;
} catch (Exception ex) {
    WriteTransactionalJobLog(new JobLogDTO {
        Mode = "Parallel",
        UniqueId = "GetMaxItemsRetrievalCount Exception",
        ThreadId = Thread.CurrentThread.ManagedThreadId.ToString(),
        StartTime = DateTime.Now.ToString(),
        ExceptionOrResult = ex.ToString()
    });
}

return rtnVal;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM