Ejemplo n.º 1
0
            internal QueueingFuture(ExecutorCompletionService <T> enclosingInstance, IRunnableFuture <T> task)
                : base(task, null)
            {
                _enclosingInstance = enclosingInstance;
                _task = task;
                var contextCopyingTask = task as IContextCopyingTask;

                if (contextCopyingTask != null && contextCopyingTask.ContextCarrier != null)
                {
                    // The task is already copying the context, so we don't do it again.
                    ((IContextCopyingTask)this).ContextCarrier = NoContextCarrier.Instance;
                }
            }
Ejemplo n.º 2
0
        private T DoInvokeAny <T>(IEnumerable tasks, int count, bool timed, TimeSpan durationToWait, Converter <object, IRunnableFuture <T> > converter)
        {
            if (tasks == null)
            {
                throw new ArgumentNullException("tasks");
            }
            List <IFuture <T> >           futures = count > 0 ? new List <IFuture <T> >(count) : new List <IFuture <T> >();
            ExecutorCompletionService <T> ecs     = new ExecutorCompletionService <T>(this);
            TimeSpan duration = durationToWait;

            // For efficiency, especially in executors with limited
            // parallelism, check to see if previously submitted tasks are
            // done before submitting more of them. This interleaving
            // plus the exception mechanics account for messiness of main
            // loop.

            try
            {
                // Record exceptions so that if we fail to obtain any
                // result, we can throw the last exception we got.
                ExecutionException ee       = null;
                DateTime           lastTime = (timed) ? DateTime.Now : new DateTime(0);
                IEnumerator        it       = tasks.GetEnumerator();
                bool hasMoreTasks           = it.MoveNext();
                if (!hasMoreTasks)
                {
                    throw new ArgumentException("No tasks passed in.");
                }
                futures.Add(ecs.Submit(converter(it.Current)));
                int active = 1;

                for (;;)
                {
                    IFuture <T> f = ecs.Poll();
                    if (f == null)
                    {
                        if (hasMoreTasks && (hasMoreTasks = it.MoveNext()))
                        {
                            futures.Add(ecs.Submit(converter(it.Current)));
                            ++active;
                        }
                        else if (active == 0)
                        {
                            break;
                        }
                        else if (timed)
                        {
                            f = ecs.Poll(duration);
                            if (f == null)
                            {
                                throw new TimeoutException();
                            }
                            //TODO: done't understand what are we doing here. Useless!? -K.X.
                            duration = duration.Subtract(DateTime.Now.Subtract(lastTime));
                            lastTime = DateTime.Now;
                        }
                        else
                        {
                            f = ecs.Take();
                        }
                    }
                    if (f != null)
                    {
                        --active;
                        try
                        {
                            return(f.GetResult());
                        }
                        catch (ThreadInterruptedException)
                        {
                            throw;
                        }
                        catch (ExecutionException eex)
                        {
                            ee = eex;
                        }
                        catch (SystemException rex)
                        {
                            ee = new ExecutionException(rex);
                        }
                    }
                }

                if (ee == null)
                {
                    ee = new ExecutionException();
                }
                throw ee;
            }
            finally
            {
                foreach (IFuture <T> future in futures)
                {
                    future.Cancel(true);
                }
            }
        }
 internal QueueingFuture(ExecutorCompletionService <T> enclosingInstance, IRunnableFuture <T> task)
     : base(task, null)
 {
     _enclosingInstance = enclosingInstance;
     _task = task;
 }
 [SetUp] public void SetUp()
 {
     _executor      = Mockery.GeneratePartialMock <AbstractExecutorService>();
     _sut           = new ExecutorCompletionService <T>(_executor);
     _blockingQueue = MockRepository.GenerateMock <IBlockingQueue <IFuture <T> > >();
 }