接上文多執行緒程式設計學習筆記——執行緒池(一)
接上文 多執行緒程式設計學習筆記——執行緒池(二)
執行緒池還有乙個threadpool.registerwaitforsingleobject,這個方法允許我們將**函式放入執行緒池中的佇列中。當提供的等待事件處理器接收到訊號或發生超時時,這個**函式將被呼叫,這樣就實現了為執行緒池中操作實現超時操作。
1.**如下:
using2.程式結果如下。system;
using
system.collections.generic;
using
system.diagnostics;
using
system.linq;
using
system.text;
using
system.threading;
namespace
threadtpldemo
private
static
void
timesoperation(timespan worktimes)} }
private
static
void
asyncoper(cancellationtoken token,manualresetevent mrevt)
", thread.currentthread.managedthreadid);
return
; }
thread.sleep(timespan.fromseconds(
1));
}mrevt.set();
console.writeline(
"-------執行緒池中的第乙個工作執行緒 發出訊號----------");
}private
static
void asyncoperwait(cancellationtokensource cts, bool
istimeout)
", thread.currentthread.managedthreadid);
}else}}
}
程式啟動之後按順序放入了一些長時間執行的操作,這個操作執行6秒,如果執行成功,則會設定乙個manualresetevent訊號。如果取消了這個操作,則這個操作會被丟棄。
我們還註冊了第二個非同步操作,當從manualresetevent物件中接受了乙個訊號之後,這個非同步操作會被呼叫。如果第乙個操作順利執行,則會設定訊號。如果第乙個操作執行超時,則會通過cancellationtoken來取消第乙個操作。
注:當執行緒池中大量的操作被阻塞時,上面的方法就非常有用了。
六、 使用計時器
使用threading.timer物件實現執行緒池中的週期性呼叫的非同步操作。
1.**如下:
using2.程式執行結果如下。system;
using
system.collections.generic;
using
system.diagnostics;
using
system.linq;
using
system.text;
using
system.threading;
namespace
threadpooldemo
console.read();
}private
static
void
timesoperation(datetime starttime)
從 開始 執行了 秒",
thread.currentthread.managedthreadid, starttime.tostring(
"yyyy-mm-dd hh:mm:ss
"), time.seconds); }}
}
七、 使用backgroundworker元件
本示例使用backgroundworker元件實現非同步操作。
程式啟動時建立了乙個backgroundworker物件例項,顯示的指示這個後台工作執行緒支援取消操作及操作進度通知。
1.**如下:
using2.程式正常執行結束,如下圖。system;
using
system.collections.generic;
using
system.componentmodel;
using
system.diagnostics;
using
system.linq;
using
system.text;
using
system.threading;
namespace
threadtpldemo
} while
(bgwork.isbusy);
console.read();
}static
void worker_dowork(object
sender,doworkeventargs e)
開始 執行",
thread.currentthread.managedthreadid);
int result = 0
;
var bgwork =(backgroundworker)sender;
for (int i = 0; i < 100; i++)
if (i%10==0
)
thread.sleep(
200);
result +=i;
}e.result =result;
}static
void worker_progresschanged(object
sender,progresschangedeventargs e)
已經完成工作量的 %
", thread.currentthread.managedthreadid,e.progresspercentage);
}static
void worker_completed(object
sender,runworkercompletedeventargs e)
已經執行結束!",
thread.currentthread.managedthreadid);
if (e.error!=null
) 發生錯誤,錯誤資訊:",
thread.currentthread.managedthreadid,e.error.message);
}else
if(e.cancelled)
已經取消",
thread.currentthread.managedthreadid);
}else
執行成功,結果是:",
thread.currentthread.managedthreadid, e.result);}}
}}
程式執行的中途,人工干預,取消。如下圖。請看下圖中黃框的位置,我輸入了字母c,則執行緒被取消。
在程式中我們定義了三個事件。
progresschanged事件,通過接收backgroundworker的reportprogress方法傳遞過來的引數,顯示執行緒執行的進度。
runworkercompleted事件,在此事件中可以知道操作是成功完成,還是發生了錯誤,或是被取消。
執行緒池 多執行緒學習筆記(三)
執行緒池產生原因 建立很多執行緒造成的兩個問題 1.構建乙個新的執行緒會涉及到與作業系統的互動,會消耗一定的系統資源,當使用完這些新建立的執行緒後,執行緒就會被銷毀,然後當我們再建立的時候就會再次消耗系統資源,所以如果建立很多生命期很短的執行緒,就會消耗很大的系統資源,甚至給系統帶來很大的壓力。2....
多執行緒程式設計學習筆記 執行緒池(一)
接上文 多執行緒程式設計學習筆記 執行緒同步 一 接上文 多執行緒程式設計學習筆記 執行緒同步 二 接上文 多執行緒程式設計學習筆記 執行緒同步 三 建立多執行緒操作是非常昂貴的,所以每個執行時間非常短的操作,建立多執行緒進行操作,可能並不能提高效率,反而降低了效率。執行緒池,就是我們先分配一些資源...
多執行緒程式設計 執行緒池 threadpool
很多公司裡,雇員通常會在辦公室度過他們的辦公時光 偶爾也會外出訪問客戶或 商 或是參加 展會。雖然外出可能很有必要,並且可能需要很多人一起去,不過對於一些特別的雇員來說,一趟可能就是幾個月,甚至是幾年。公司要給每個雇員都配一輛車,這基本上是不可能的,不過公司可以提供一些共用車輛 這樣就會有一定數量車...