Beispiel #1
0
        /// <summary>
        ///     Run the given <see cref="IRunnable" /> object in the next available
        ///     <see cref="Thread" />. If while waiting the thread pool is asked to
        ///     shut down, the Runnable is executed immediately within a new additional
        ///     thread.
        /// </summary>
        /// <param name="runnable">
        ///     The <see cref="IRunnable" /> to be added.
        /// </param>
        public virtual bool RunInThread(IRunnable runnable)
        {
            if (runnable == null)
            {
                return(false);
            }

            lock (nextRunnableLock)
            {
                try
                {
                    handoffPending = true;

                    // Wait until a worker thread is available
                    while ((availWorkers.Count < 1) && !isShutdown)
                    {
                        try
                        {
                            Monitor.Wait(nextRunnableLock, 500);
                        }
                        catch (ThreadInterruptedException)
                        {
                        }
                    }

                    if (!isShutdown)
                    {
                        WorkerThread wt = availWorkers.First.Value;
                        availWorkers.RemoveFirst();
                        busyWorkers.AddLast(wt);
                        wt.Run(runnable);
                    }
                    else
                    {
                        //停止后不再添加job
                        // If the thread pool is going down, execute the Runnable
                        // within a new additional worker thread (no thread from the pool).
                        //var wt = new WorkerThread(this, "WorkerThread-LastJob", prio, MakeThreadsDaemons, runnable);
                        //busyWorkers.AddLast(wt);
                        //workers.Add(wt);
                        //wt.Start();
                    }
                    Monitor.PulseAll(nextRunnableLock);
                }
                catch (Exception exDebug)
                {
                    log.Fatal("添加任务时异常!!", exDebug);
                }
                finally
                {
                    handoffPending = false;
                }
            }

            return(!isShutdown);
        }
Beispiel #2
0
        /// <summary>
        /// Run the given <see cref="IThreadRunnable" /> object in the next available
        /// <see cref="Thread" />. If while waiting the thread pool is asked to
        /// shut down, the Runnable is executed immediately within a new additional
        /// thread.
        /// </summary>
        /// <param name="runnable">The <see cref="IThreadRunnable" /> to be added.</param>
        public virtual bool RunInThread(IThreadRunnable runnable)
        {
            if (runnable == null)
            {
                return(false);
            }

            lock (nextRunnableLock)
            {
                handoffPending = true;

                // Wait until a worker thread is available
                while ((availWorkers.Count < 1) && !isShutdown)
                {
                    try
                    {
                        Monitor.Wait(nextRunnableLock, 500);
                    }
                    catch (ThreadInterruptedException)
                    {
                    }
                }

                if (!isShutdown)
                {
                    WorkerThread wt = (WorkerThread)availWorkers[0];
                    availWorkers.RemoveAt(0);
                    busyWorkers.Add(wt);
                    wt.Run(runnable);
                }
                else
                {
                    // If the thread pool is going down, execute the Runnable
                    // within a new additional worker thread (no thread from the pool).
                    WorkerThread wt =
                        new WorkerThread(this, "WorkerThread-LastJob", prio, MakeThreadsDaemons, runnable);
                    busyWorkers.Add(wt);
                    workers.Add(wt);
                    wt.Start();
                }
                Monitor.PulseAll(nextRunnableLock);
                handoffPending = false;
            }

            return(true);
        }
        public virtual bool RunInThread(IThreadRunnable runnable)
        {
            if (runnable == null)
            {
                return(false);
            }

            lock (nextRunnableLock)
            {
                handoffPending = true;

                while ((availWorkers.Count < 1) && !isShutdown)
                {
                    try
                    {
                        Monitor.Wait(nextRunnableLock, 500);
                    }
                    catch (ThreadInterruptedException)
                    {
                    }
                }

                if (!isShutdown)
                {
                    WorkerThread wt = availWorkers.First.Value;
                    availWorkers.RemoveFirst();
                    busyWorkers.AddLast(wt);
                    wt.Run(runnable);
                }
                else
                {
                    WorkerThread wt = new WorkerThread(this, "WorkerThread-LastJob", prio, MakeThreadsDaemons, runnable);
                    busyWorkers.AddLast(wt);
                    workers.Add(wt);
                    wt.Start();
                }
                Monitor.PulseAll(nextRunnableLock);
                handoffPending = false;
            }

            return(true);
        }
        public static void ThreadMain(object data)
        {
            WorkerThread workerThread = (WorkerThread)data;

            workerThread.Run();
        }