static void Main(string[] args) { long testthread = ThreadPool.GetThreadModel("测试线程"); ThreadPool.AddTask(testthread, new ActionTask((task) => { log.Error("指定线程执行一次"); })); /*指定间隔时间无限执行,但是我只让他执行一次就取消*/ ThreadPool.AddGlobTimerTask(new CancelTest()); ThreadPool.AddGlobTask(new ActionTask((task) => { log.Error("全局线程执行一次"); })); System.Threading.Thread.Sleep(1000); /*指定间隔时间无限执行,全局线程*/ ThreadPool.AddGlobTimerTask(new ActionTimerTask(100, (task) => { log.Error("全局线程,指定间隔时间无限执行"); })); }
internal static void Dispatch(NonBlockingConnection conn, ActionBean bean, Protocol protocol) { int priority = 1; Task task = new Task(priority, bean, conn, protocol); ThreadPool.AddTask(task); }
public override void Run() { session._Action(); if (!"favicon.ico".Equals(session.Http_Url)) { if (log.IsDebugEnabled()) { log.Debug("Create Http Socket Remote Socket LocalEndPoint:" + session.LocalEndPoint + " RemoteEndPoint:" + session.RemoteEndPoint.ToString()); } HttpActionBean httpbean = null; if (!httpServer.httpHandlers.TryGetValue(session.Http_Url, out httpbean)) { httpServer.httpHandlers.TryGetValue("*", out httpbean); } if (httpbean != null) { HttpActionRun taskmodel = new HttpActionRun(session, httpbean.ihttpHandler); ThreadPool.AddTask(httpbean.threadId, taskmodel); return; } if (log.IsErrorEnabled()) { log.Error("未找到监听状态:" + session.Http_Url, new Exception("未找到监听状态:" + session.Http_Url)); } } session.Close(); }
private void AcceptAsync_Async(IAsyncResult iar) { this.AcceptAsync(); try { TcpClient client = this._Listeners.EndAcceptTcpClient(iar); /*先添加到队列,让他缓冲数据块,执行是读取客户端传来的数据*/ HttpSession socket = new HttpSession(client.Client); ThreadPool.AddTask(this.ThreadId, new HttpAction() { httpServer = this, session = socket }); } catch (Exception ex) { if (log.IsErrorEnabled()) { log.Error("执行http", ex); } } }
public static void MultithreadEmptyValuesSorted() { int nbrThreads = Environment.ProcessorCount; var container = new ListContainer(); int TOTAL_TEST_COUNT = 100000; long nbrThreadsFinished = 0; long nbrThreadsGate1 = 0; Exception taskTestException = null; // Note: Using a Barrier to synchronize all the threads at each iteration using (Barrier barrier = new Barrier(nbrThreads, (b) => { Interlocked.Increment(ref nbrThreadsGate1); container.List.AddRange(new List <string> { }); // Adding an empty collection makes the UniqueList dirty })) { ThreadPool.TaskCallback taskLambda = (object taskParams) => { var listContainersTask = (ListContainer)taskParams; try { for (int i = 0; i < TOTAL_TEST_COUNT; ++i) { // Synchronize all threads barrier.SignalAndWait(); if (taskTestException != null) { break; // Abort once we got an exception } // Attempt to access the SortedList property from multiple threads. // It must not create any exception! foreach (var s in container.SortedList) { Console.WriteLine(s); } } } catch (Exception e) { // Keep the exception around as we will need it in the main thread of this test. taskTestException = e; } finally { barrier.RemoveParticipant(); // Must remove the participant to unblock all the other threads. Interlocked.Increment(ref nbrThreadsFinished); } }; // Start a thread pool and execute tasks. using (ThreadPool pool = new ThreadPool()) { // Add 1 task per thread pool.Start(nbrThreads); for (int i = 0; i < nbrThreads; ++i) { pool.AddTask(taskLambda, container); } // Wait for all the tasks to complete. pool.Wait(); // Check the results. TestContext.Out.WriteLine("nbr Finished: {0}, nbr Gate1: {1}", nbrThreadsFinished, nbrThreadsGate1); if (taskTestException != null) { throw taskTestException; } } } }