Ejemplo n.º 1
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void mustCombineWork() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        internal virtual void MustCombineWork()
        {
            BinaryLatch       startLatch = new BinaryLatch();
            BinaryLatch       blockLatch = new BinaryLatch();
            FutureTask <Void> blocker    = new FutureTask <Void>(new CallableWork(this, new AddWorkAnonymousInnerClass(this, startLatch, blockLatch)));

            (new Thread(blocker)).Start();
            startLatch.Await();
            ICollection <FutureTask <Void> > tasks = new List <FutureTask <Void> >();

            tasks.Add(blocker);
            for (int i = 0; i < 20; i++)
            {
                CallableWork      task       = new CallableWork(this, new AddWork(1));
                FutureTask <Void> futureTask = new FutureTask <Void>(task);
                tasks.Add(futureTask);
                Thread thread = new Thread(futureTask);
                thread.Start();
                //noinspection StatementWithEmptyBody,LoopConditionNotUpdatedInsideLoop
                while (thread.State != Thread.State.TIMED_WAITING)
                {
                    // Wait for the thread to reach the lock.
                }
            }
            blockLatch.Release();
            foreach (FutureTask <Void> task in tasks)
            {
                task.get();
            }
            assertThat(_count.sum(), lessThan(_sum.sum()));
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Construct a new {@code AsyncEvents} instance, that will use the given consumer to process the events.
 /// </summary>
 /// <param name="eventConsumer"> The <seealso cref="Consumer"/> used for processing the events that are sent in. </param>
 public AsyncEvents(System.Action <T> eventConsumer, Monitor monitor)
 {
     this._eventConsumer = eventConsumer;
     this._monitor       = monitor;
     this._startupLatch  = new BinaryLatch();
     this._shutdownLatch = new BinaryLatch();
     this._stack         = _endSentinel;
 }
Ejemplo n.º 3
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void releaseThenAwaitDoesNotBlock()
        internal virtual void ReleaseThenAwaitDoesNotBlock()
        {
            assertTimeout(ofSeconds(3), () =>
            {
                BinaryLatch latch = new BinaryLatch();
                latch.Release();
                latch.Await();
            });
        }
Ejemplo n.º 4
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void releaseMustUnblockAwaiters()
        internal virtual void ReleaseMustUnblockAwaiters()
        {
            assertTimeout(ofSeconds(10), () =>
            {
                BinaryLatch latch         = new BinaryLatch();
                ThreadStart awaiter       = latch.await;
                int awaiters              = 24;
                Future <object>[] futures = new Future <object> [awaiters];
                for (int i = 0; i < awaiters; i++)
                {
                    futures[i] = _executor.submit(awaiter);
                }

                assertThrows(typeof(TimeoutException), () => futures[0].get(10, TimeUnit.MILLISECONDS));

                latch.Release();

                foreach (Future <object> future in futures)
                {
                    future.get();
                }
            });
        }