线程池,封装使用,实现控制子线程
生活随笔
收集整理的這篇文章主要介紹了
线程池,封装使用,实现控制子线程
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
類代碼 ? using?System;
using?System.Collections.Generic;
using?System.Text;
using?System.Threading;
namespace?TxWeb.CnForums
{
????public?class?TxThread
????{
????????public?delegate?void?Delegate(object?obj);
????????///?<summary>
????????///?執行指定的方法,如果在指定的時間之內沒有完成,則中止
????????///?</summary>
????????///?<param?name="func">任務過程</param>
????????///?<param?name="threadID">帖子編號</param>
????????///?<param?name="timeSpan">超時時間</param>
????????///?<param?name="timeoutCallback">如果超時,則調用該方法</param>
????????///?<param?name="updateID">變化主表記錄編號</param>
????????///?<returns>是否正確執行完畢</returns>
????????public?static?bool?Call(Delegate?func,?object?threadID,?TimeSpan?timeSpan,?Delegate?timeoutCallback,?object?updateID)
????????{
????????????if?(func?==?null)
????????????????throw?new?ArgumentNullException("func");
????????????ManualResetEvent?resetEvent?=?new?ManualResetEvent(false);
????????????ManualResetEvent?waitThreadEvent?=?new?ManualResetEvent(false);
????????????Exception?error?=?null;
????????????Thread?thread?=?null;
????????????//?將任務加到線程當中
????????????ThreadPool.QueueUserWorkItem(delegate
????????????{
????????????????thread?=?Thread.CurrentThread;
????????????????try?{?func(threadID);?}
????????????????catch?(ThreadAbortException)?{?}
????????????????catch?(Exception?ex)?{?error?=?ex;?}
????????????????resetEvent.Set();
????????????????//?每次線程執行結束都等待后續的處理邏輯
????????????????waitThreadEvent.WaitOne();
????????????});
????????????try
????????????{
????????????????//?等待任務的結束
????????????????bool?result?=?resetEvent.WaitOne(timeSpan,?false);
????????????????//?說明在執行過程中出現異常,直接拋出異常
????????????????if?(error?!=?null)
????????????????{
????????????????????throw?error;
????????????????}
????????????????if?(!result)
????????????????{
????????????????????if?(thread?!=?null)
????????????????????{
????????????????????????//?此時可以確保該線程沒有開始運行新的任務
????????????????????????thread.Abort();
????????????????????????waitThreadEvent.Set();
????????????????????}
????????????????????if?(timeoutCallback?!=?null)
????????????????????????timeoutCallback(updateID);
????????????????}
????????????????return?result;
????????????}
????????????finally
????????????{
????????????????//?最后確保釋放線程池線程
????????????????waitThreadEvent.Set();
????????????}
????????}
????}
} 調用: TxWeb.CnForums.TxThread.Delegate?createHtml?=?new?TxWeb.CnForums.TxThread.Delegate(CreateHtml);
????????????????TxWeb.CnForums.TxThread.Delegate?dealOverTime?=?new?TxWeb.CnForums.TxThread.Delegate(DealOverTime);
????????????????TxWeb.CnForums.TxThread.Call(createHtml,?threadID,?TimeSpan.FromSeconds(OverTime),?dealOverTime,?updateID);
using?System.Collections.Generic;
using?System.Text;
using?System.Threading;
namespace?TxWeb.CnForums
{
????public?class?TxThread
????{
????????public?delegate?void?Delegate(object?obj);
????????///?<summary>
????????///?執行指定的方法,如果在指定的時間之內沒有完成,則中止
????????///?</summary>
????????///?<param?name="func">任務過程</param>
????????///?<param?name="threadID">帖子編號</param>
????????///?<param?name="timeSpan">超時時間</param>
????????///?<param?name="timeoutCallback">如果超時,則調用該方法</param>
????????///?<param?name="updateID">變化主表記錄編號</param>
????????///?<returns>是否正確執行完畢</returns>
????????public?static?bool?Call(Delegate?func,?object?threadID,?TimeSpan?timeSpan,?Delegate?timeoutCallback,?object?updateID)
????????{
????????????if?(func?==?null)
????????????????throw?new?ArgumentNullException("func");
????????????ManualResetEvent?resetEvent?=?new?ManualResetEvent(false);
????????????ManualResetEvent?waitThreadEvent?=?new?ManualResetEvent(false);
????????????Exception?error?=?null;
????????????Thread?thread?=?null;
????????????//?將任務加到線程當中
????????????ThreadPool.QueueUserWorkItem(delegate
????????????{
????????????????thread?=?Thread.CurrentThread;
????????????????try?{?func(threadID);?}
????????????????catch?(ThreadAbortException)?{?}
????????????????catch?(Exception?ex)?{?error?=?ex;?}
????????????????resetEvent.Set();
????????????????//?每次線程執行結束都等待后續的處理邏輯
????????????????waitThreadEvent.WaitOne();
????????????});
????????????try
????????????{
????????????????//?等待任務的結束
????????????????bool?result?=?resetEvent.WaitOne(timeSpan,?false);
????????????????//?說明在執行過程中出現異常,直接拋出異常
????????????????if?(error?!=?null)
????????????????{
????????????????????throw?error;
????????????????}
????????????????if?(!result)
????????????????{
????????????????????if?(thread?!=?null)
????????????????????{
????????????????????????//?此時可以確保該線程沒有開始運行新的任務
????????????????????????thread.Abort();
????????????????????????waitThreadEvent.Set();
????????????????????}
????????????????????if?(timeoutCallback?!=?null)
????????????????????????timeoutCallback(updateID);
????????????????}
????????????????return?result;
????????????}
????????????finally
????????????{
????????????????//?最后確保釋放線程池線程
????????????????waitThreadEvent.Set();
????????????}
????????}
????}
} 調用: TxWeb.CnForums.TxThread.Delegate?createHtml?=?new?TxWeb.CnForums.TxThread.Delegate(CreateHtml);
????????????????TxWeb.CnForums.TxThread.Delegate?dealOverTime?=?new?TxWeb.CnForums.TxThread.Delegate(DealOverTime);
????????????????TxWeb.CnForums.TxThread.Call(createHtml,?threadID,?TimeSpan.FromSeconds(OverTime),?dealOverTime,?updateID);
轉載于:https://www.cnblogs.com/dajiang02/archive/2011/11/25/2263100.html
總結
以上是生活随笔為你收集整理的线程池,封装使用,实现控制子线程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JAVA网络编程Socket常见问题 【
- 下一篇: cadence -- FPM0.0.8.