/// <summary> Creates a new <see cref="ThreadPoolExecutor"/> with the given initial
 /// parameters.
 /// 
 /// </summary>
 /// <param name="corePoolSize">the number of threads to keep in the pool, even if they are idle.</param>
 /// <param name="maximumPoolSize">the maximum number of threads to allow in the pool.</param>
 /// <param name="keepAliveTime">
 /// When the number of threads is greater than
 /// <see cref="CorePoolSize"/>, this is the maximum time that excess idle threads
 /// will wait for new tasks before terminating.
 /// </param>
 /// <param name="workQueue">
 /// The queue to use for holding tasks before they
 /// are executed. This queue will hold only the <see cref="IRunnable"/>
 /// tasks submitted by the <see cref="AbstractExecutorService.Execute(IRunnable)"/> method.
 /// </param>
 /// <param name="threadFactory">
 /// <see cref="IThreadFactory"/> to use for new thread creation.
 /// </param>
 /// <param name="handler">
 /// The <see cref="IRejectedExecutionHandler"/> to use when execution is blocked
 /// because the thread bounds and queue capacities are reached.
 /// </param>
 /// <exception cref="System.ArgumentOutOfRangeException">
 /// If <paramref name="corePoolSize"/> or <paramref name="keepAliveTime"/> is less than zero, or if <paramref name="maximumPoolSize"/>
 /// is less than or equal to zero, or if <paramref name="corePoolSize"/> is greater than <paramref name="maximumPoolSize"/>
 /// </exception>
 /// <exception cref="System.ArgumentNullException">if <paramref name="workQueue"/>, <paramref name="handler"/>, or <paramref name="threadFactory"/> is null</exception>
 public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, TimeSpan keepAliveTime, IBlockingQueue<IRunnable> workQueue,
                           IThreadFactory threadFactory, IRejectedExecutionHandler handler)
 {
     if (corePoolSize < 0)
     {
         throw new ArgumentOutOfRangeException("corePoolSize", corePoolSize,
                                               "core pool size cannot be less than zero.");
     }
     if (maximumPoolSize <= 0)
     {
         throw new ArgumentOutOfRangeException("maximumPoolSize", maximumPoolSize,
                                               "maximum pool size must be greater than zero");
     }
     if (maximumPoolSize < corePoolSize)
     {
         throw new ArgumentException("maximum pool size, " + maximumPoolSize +
                                     " cannot be less than core pool size, " + corePoolSize + ".", "maximumPoolSize");
     }
     if (keepAliveTime.Ticks < 0)
     {
         throw new ArgumentOutOfRangeException("keepAliveTime", keepAliveTime,
                                               "keep alive time must be greater than or equal to zero.");
     }
     if (workQueue == null)
     {
         throw new ArgumentNullException("workQueue");
     }
     if (threadFactory == null)
     {
         throw new ArgumentNullException("threadFactory");
     }
     if (handler == null)
     {
         throw new ArgumentNullException("handler");
     }
     _corePoolSize = corePoolSize;
     _maximumPoolSize = maximumPoolSize;
     _workQueue = workQueue;
     KeepAliveTime = keepAliveTime;
     _threadFactory = threadFactory;
     _rejectedExecutionHandler = handler;
     _termination = _mainLock.NewCondition();
 }
Beispiel #2
0
 /// <summary>Creates a new instance of <see cref="DefaultEventLoopGroup"/>.</summary>
 public DefaultEventLoopGroup(int nThreads, IEventExecutorChooserFactory <DefaultEventLoop> chooserFactory,
                              IRejectedExecutionHandler rejectedHandler, int maxPendingTasks)
     : base(nThreads, chooserFactory, group => new DefaultEventLoop(group, rejectedHandler, maxPendingTasks))
 {
 }
 /// <summary> 
 /// Creates a new <see cref="ThreadPoolExecutor"/> with the given initial
 /// parameters and <see cref="IThreadFactory"/>.
 /// </summary>
 /// <summary> 
 /// Creates a new <see cref="ThreadPoolExecutor"/> with the given initial
 /// parameters and default <see cref="RejectedExecutionException"/>.
 /// </summary>
 /// <param name="corePoolSize">the number of threads to keep in the pool, even if they are idle.</param>
 /// <param name="maximumPoolSize">the maximum number of threads to allow in the pool.</param>
 /// <param name="keepAliveTime">
 /// When the number of threads is greater than
 /// <see cref="CorePoolSize"/>, this is the maximum time that excess idle threads
 /// will wait for new tasks before terminating.
 /// </param>
 /// <param name="workQueue">
 /// The queue to use for holding tasks before they
 /// are executed. This queue will hold only the <see cref="IRunnable"/>
 /// tasks submitted by the <see cref="AbstractExecutorService.Execute(IRunnable)"/> method.
 /// </param>
 /// <param name="handler">
 /// The <see cref="IRejectedExecutionHandler"/> to use when execution is blocked
 /// because the thread bounds and queue capacities are reached.
 /// </param>
 /// <exception cref="System.ArgumentOutOfRangeException">
 /// If <paramref name="corePoolSize"/> or <paramref name="keepAliveTime"/> is less than zero, or if <paramref name="maximumPoolSize"/>
 /// is less than or equal to zero, or if <paramref name="corePoolSize"/> is greater than <paramref name="maximumPoolSize"/>
 /// </exception>
 /// <exception cref="System.ArgumentNullException">if <paramref name="workQueue"/> or <paramref name="handler"/> is null</exception>
 public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, TimeSpan keepAliveTime, IBlockingQueue<IRunnable> workQueue,
                           IRejectedExecutionHandler handler)
     : this(corePoolSize, maximumPoolSize, keepAliveTime, workQueue, Executors.NewDefaultThreadFactory(), handler)
 {
 }
Beispiel #4
0
 /// <summary>Creates a new instance of <see cref="DefaultEventLoopGroup"/>.</summary>
 public DefaultEventLoopGroup(int nThreads, IThreadFactory threadFactory,
                              IRejectedExecutionHandler rejectedHandler, IEventLoopTaskQueueFactory queueFactory)
     : base(nThreads, group => new DefaultEventLoop(group, threadFactory, rejectedHandler, queueFactory))
 {
 }
Beispiel #5
0
 /// <summary>Creates a new instance of <see cref="DefaultEventLoopGroup"/>.</summary>
 public DefaultEventLoopGroup(int nThreads, IEventExecutorChooserFactory <DefaultEventLoop> chooserFactory, IRejectedExecutionHandler rejectedHandler)
     : base(nThreads, chooserFactory, group => new DefaultEventLoop(group, rejectedHandler, queueFactory: null))
 {
 }
 /// <summary>Creates a new instance of <see cref="SingleThreadEventExecutor"/>.</summary>
 /// <param name="threadFactory">the <see cref="IThreadFactory"/> which will be used for the used <see cref="Thread"/>.</param>
 /// <param name="addTaskWakesUp"><c>true</c> if and only if invocation of <see cref="AddTask(IRunnable)"/> will wake up the executor thread.</param>
 /// <param name="rejectedHandler">the <see cref="IRejectedExecutionHandler"/> to use.</param>
 protected SingleThreadEventExecutor(IThreadFactory threadFactory, bool addTaskWakesUp, IRejectedExecutionHandler rejectedHandler)
     : this(null, threadFactory, addTaskWakesUp, rejectedHandler)
 {
 }
Beispiel #7
0
 /// <summary>Creates a new instance of <see cref="DefaultEventLoopGroup"/>.</summary>
 public DefaultEventLoopGroup(int nThreads, IThreadFactory threadFactory,
                              IRejectedExecutionHandler rejectedHandler, int maxPendingTasks)
     : base(nThreads, group => new DefaultEventLoop(group, threadFactory, rejectedHandler, maxPendingTasks))
 {
 }
        private SingleThreadEventExecutor(IEventExecutorGroup parent, bool addTaskWakesUp, IRejectedExecutionHandler rejectedHandler)
            : base(parent)
        {
            if (rejectedHandler is null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.rejectedHandler);
            }

            _firstTask = true;

            _loopAction     = Loop;
            _loopCoreAciton = LoopCore;

            _addTaskWakesUp           = addTaskWakesUp;
            _rejectedExecutionHandler = rejectedHandler;

            _shutdownHooks = new HashSet <Action>();
            _terminationCompletionSource = NewPromise();
            _threadLock = new CountdownEvent(1);

            _taskScheduler = new ExecutorTaskScheduler(this);
        }
 /// <summary>Creates a new instance of <see cref="SingleThreadEventExecutor"/>.</summary>
 /// <param name="parent">the <see cref="IEventExecutorGroup"/> which is the parent of this instance and belongs to it.</param>
 /// <param name="threadFactory">the <see cref="IThreadFactory"/> which will be used for the used <see cref="Thread"/>.</param>
 /// <param name="addTaskWakesUp"><c>true</c> if and only if invocation of <see cref="AddTask(IRunnable)"/> will wake up the executor thread.</param>
 /// <param name="rejectedHandler">the <see cref="IRejectedExecutionHandler"/> to use.</param>
 public SingleThreadEventExecutor(IEventExecutorGroup parent, IThreadFactory threadFactory, bool addTaskWakesUp, IRejectedExecutionHandler rejectedHandler)
     : this(parent, threadFactory, addTaskWakesUp, DefaultMaxPendingExecutorTasks, rejectedHandler)
 {
 }
Beispiel #10
0
 public SingleThreadEventLoop(IEventLoopGroup parent, IThreadFactory threadFactory, IRejectedExecutionHandler rejectedHandler, TimeSpan breakoutInterval)
     : base(parent, threadFactory, false, int.MaxValue, rejectedHandler)
 {
     _emptyEvent            = new ManualResetEventSlim(false, 1);
     _breakoutNanosInterval = PreciseTime.ToDelayNanos(breakoutInterval);
     Start();
 }
Beispiel #11
0
 public SingleThreadEventLoop(IEventLoopGroup parent, IThreadFactory threadFactory, IRejectedExecutionHandler rejectedHandler)
     : this(parent, threadFactory, rejectedHandler, DefaultBreakoutInterval)
 {
 }
Beispiel #12
0
 public SingleThreadEventLoop(IEventLoopGroup parent, IRejectedExecutionHandler rejectedHandler, TimeSpan breakoutInterval)
     : this(parent, DefaultThreadFactory <SingleThreadEventLoop> .Instance, rejectedHandler, breakoutInterval)
 {
 }