예제 #1
0
        /// <summary>
        /// Acquires the given number of permits from this semaphore, if all
        /// become available within the given waiting time and the current
        /// thread has not been <seealso cref="Thread#interrupt interrupted"/>.
        ///
        /// <para>Acquires the given number of permits, if they are available and
        /// returns immediately, with the value {@code true},
        /// reducing the number of available permits by the given amount.
        ///
        /// </para>
        /// <para>If insufficient permits are available then
        /// the current thread becomes disabled for thread scheduling
        /// purposes and lies dormant until one of three things happens:
        /// <ul>
        /// <li>Some other thread invokes one of the <seealso cref="#release() release"/>
        /// methods for this semaphore, the current thread is next to be assigned
        /// permits and the number of available permits satisfies this request; or
        /// <li>Some other thread <seealso cref="Thread#interrupt interrupts"/>
        /// the current thread; or
        /// <li>The specified waiting time elapses.
        /// </ul>
        ///
        /// </para>
        /// <para>If the permits are acquired then the value {@code true} is returned.
        ///
        /// </para>
        /// <para>If the current thread:
        /// <ul>
        /// <li>has its interrupted status set on entry to this method; or
        /// <li>is <seealso cref="Thread#interrupt interrupted"/> while waiting
        /// to acquire the permits,
        /// </ul>
        /// then <seealso cref="InterruptedException"/> is thrown and the current thread's
        /// interrupted status is cleared.
        /// Any permits that were to be assigned to this thread, are instead
        /// assigned to other threads trying to acquire permits, as if
        /// the permits had been made available by a call to <seealso cref="#release()"/>.
        ///
        /// </para>
        /// <para>If the specified waiting time elapses then the value {@code false}
        /// is returned.  If the time is less than or equal to zero, the method
        /// will not wait at all.  Any permits that were to be assigned to this
        /// thread, are instead assigned to other threads trying to acquire
        /// permits, as if the permits had been made available by a call to
        /// <seealso cref="#release()"/>.
        ///
        /// </para>
        /// </summary>
        /// <param name="permits"> the number of permits to acquire </param>
        /// <param name="timeout"> the maximum time to wait for the permits </param>
        /// <param name="unit"> the time unit of the {@code timeout} argument </param>
        /// <returns> {@code true} if all permits were acquired and {@code false}
        ///         if the waiting time elapsed before all permits were acquired </returns>
        /// <exception cref="InterruptedException"> if the current thread is interrupted </exception>
        /// <exception cref="IllegalArgumentException"> if {@code permits} is negative </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public boolean tryAcquire(int permits, long timeout, TimeUnit unit) throws InterruptedException
        public virtual bool TryAcquire(int permits, long timeout, TimeUnit unit)
        {
            if (permits < 0)
            {
                throw new IllegalArgumentException();
            }
            return(Sync.TryAcquireSharedNanos(permits, unit.ToNanos(timeout)));
        }
예제 #2
0
        /// <exception cref="CancellationException"> {@inheritDoc} </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
        public virtual V Get(long timeout, TimeUnit unit)
        {
            if (unit == null)
            {
                throw new NullPointerException();
            }
            int s = State;

            if (s <= COMPLETING && (s = AwaitDone(true, unit.ToNanos(timeout))) <= COMPLETING)
            {
                throw new TimeoutException();
            }
            return(Report(s));
        }
예제 #3
0
        /// <summary>
        /// Waits for another thread to arrive at this exchange point (unless
        /// the current thread is <seealso cref="Thread#interrupt interrupted"/> or
        /// the specified waiting time elapses), and then transfers the given
        /// object to it, receiving its object in return.
        ///
        /// <para>If another thread is already waiting at the exchange point then
        /// it is resumed for thread scheduling purposes and receives the object
        /// passed in by the current thread.  The current thread returns immediately,
        /// receiving the object passed to the exchange by that other thread.
        ///
        /// </para>
        /// <para>If no other thread is already waiting at the exchange then the
        /// current thread is disabled for thread scheduling purposes and lies
        /// dormant until one of three things happens:
        /// <ul>
        /// <li>Some other thread enters the exchange; or
        /// <li>Some other thread <seealso cref="Thread#interrupt interrupts"/>
        /// the current thread; or
        /// <li>The specified waiting time elapses.
        /// </ul>
        /// </para>
        /// <para>If the current thread:
        /// <ul>
        /// <li>has its interrupted status set on entry to this method; or
        /// <li>is <seealso cref="Thread#interrupt interrupted"/> while waiting
        /// for the exchange,
        /// </ul>
        /// then <seealso cref="InterruptedException"/> is thrown and the current thread's
        /// interrupted status is cleared.
        ///
        /// </para>
        /// <para>If the specified waiting time elapses then {@link
        /// TimeoutException} is thrown.  If the time is less than or equal
        /// to zero, the method will not wait at all.
        ///
        /// </para>
        /// </summary>
        /// <param name="x"> the object to exchange </param>
        /// <param name="timeout"> the maximum time to wait </param>
        /// <param name="unit"> the time unit of the {@code timeout} argument </param>
        /// <returns> the object provided by the other thread </returns>
        /// <exception cref="InterruptedException"> if the current thread was
        ///         interrupted while waiting </exception>
        /// <exception cref="TimeoutException"> if the specified waiting time elapses
        ///         before another thread enters the exchange </exception>
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @SuppressWarnings("unchecked") public V exchange(V x, long timeout, TimeUnit unit) throws InterruptedException, TimeoutException
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
        public virtual V Exchange(V x,long timeout,TimeUnit unit)
        {
            Object v;
            Object item = (x == null) ? NULL_ITEM : x;
            long   ns   = unit.ToNanos(timeout);

            if ((Arena != null || (v = SlotExchange(item,true,ns)) == null) && ((Thread.Interrupted() || (v = ArenaExchange(item,true,ns)) == null)))
            {
                throw new InterruptedException();
            }
            if (v == TIMED_OUT)
            {
                throw new TimeoutException();
            }
            return((v == NULL_ITEM) ? null : (V)v);
        }
예제 #4
0
        /// <summary>
        /// Waits until all <seealso cref="#getParties parties"/> have invoked
        /// {@code await} on this barrier, or the specified waiting time elapses.
        ///
        /// <para>If the current thread is not the last to arrive then it is
        /// disabled for thread scheduling purposes and lies dormant until
        /// one of the following things happens:
        /// <ul>
        /// <li>The last thread arrives; or
        /// <li>The specified timeout elapses; or
        /// <li>Some other thread <seealso cref="Thread#interrupt interrupts"/>
        /// the current thread; or
        /// <li>Some other thread <seealso cref="Thread#interrupt interrupts"/>
        /// one of the other waiting threads; or
        /// <li>Some other thread times out while waiting for barrier; or
        /// <li>Some other thread invokes <seealso cref="#reset"/> on this barrier.
        /// </ul>
        ///
        /// </para>
        /// <para>If the current thread:
        /// <ul>
        /// <li>has its interrupted status set on entry to this method; or
        /// <li>is <seealso cref="Thread#interrupt interrupted"/> while waiting
        /// </ul>
        /// then <seealso cref="InterruptedException"/> is thrown and the current thread's
        /// interrupted status is cleared.
        ///
        /// </para>
        /// <para>If the specified waiting time elapses then <seealso cref="TimeoutException"/>
        /// is thrown. If the time is less than or equal to zero, the
        /// method will not wait at all.
        ///
        /// </para>
        /// <para>If the barrier is <seealso cref="#reset"/> while any thread is waiting,
        /// or if the barrier <seealso cref="#isBroken is broken"/> when
        /// {@code await} is invoked, or while any thread is waiting, then
        /// <seealso cref="BrokenBarrierException"/> is thrown.
        ///
        /// </para>
        /// <para>If any thread is <seealso cref="Thread#interrupt interrupted"/> while
        /// waiting, then all other waiting threads will throw {@link
        /// BrokenBarrierException} and the barrier is placed in the broken
        /// state.
        ///
        /// </para>
        /// <para>If the current thread is the last thread to arrive, and a
        /// non-null barrier action was supplied in the constructor, then the
        /// current thread runs the action before allowing the other threads to
        /// continue.
        /// If an exception occurs during the barrier action then that exception
        /// will be propagated in the current thread and the barrier is placed in
        /// the broken state.
        ///
        /// </para>
        /// </summary>
        /// <param name="timeout"> the time to wait for the barrier </param>
        /// <param name="unit"> the time unit of the timeout parameter </param>
        /// <returns> the arrival index of the current thread, where index
        ///         {@code getParties() - 1} indicates the first
        ///         to arrive and zero indicates the last to arrive </returns>
        /// <exception cref="InterruptedException"> if the current thread was interrupted
        ///         while waiting </exception>
        /// <exception cref="TimeoutException"> if the specified timeout elapses.
        ///         In this case the barrier will be broken. </exception>
        /// <exception cref="BrokenBarrierException"> if <em>another</em> thread was
        ///         interrupted or timed out while the current thread was
        ///         waiting, or the barrier was reset, or the barrier was broken
        ///         when {@code await} was called, or the barrier action (if
        ///         present) failed due to an exception </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public int await(long timeout, TimeUnit unit) throws InterruptedException, BrokenBarrierException, TimeoutException
        public virtual int @await(long timeout, TimeUnit unit)
        {
            return(Dowait(true, unit.ToNanos(timeout)));
        }
예제 #5
0
        /// <summary>
        /// Acquires a permit from this semaphore, if one becomes available
        /// within the given waiting time and the current thread has not
        /// been <seealso cref="Thread#interrupt interrupted"/>.
        ///
        /// <para>Acquires a permit, if one is available and returns immediately,
        /// with the value {@code true},
        /// reducing the number of available permits by one.
        ///
        /// </para>
        /// <para>If no permit is available then the current thread becomes
        /// disabled for thread scheduling purposes and lies dormant until
        /// one of three things happens:
        /// <ul>
        /// <li>Some other thread invokes the <seealso cref="#release"/> method for this
        /// semaphore and the current thread is next to be assigned a permit; or
        /// <li>Some other thread <seealso cref="Thread#interrupt interrupts"/>
        /// the current thread; or
        /// <li>The specified waiting time elapses.
        /// </ul>
        ///
        /// </para>
        /// <para>If a permit is acquired then the value {@code true} is returned.
        ///
        /// </para>
        /// <para>If the current thread:
        /// <ul>
        /// <li>has its interrupted status set on entry to this method; or
        /// <li>is <seealso cref="Thread#interrupt interrupted"/> while waiting
        /// to acquire a permit,
        /// </ul>
        /// then <seealso cref="InterruptedException"/> is thrown and the current thread's
        /// interrupted status is cleared.
        ///
        /// </para>
        /// <para>If the specified waiting time elapses then the value {@code false}
        /// is returned.  If the time is less than or equal to zero, the method
        /// will not wait at all.
        ///
        /// </para>
        /// </summary>
        /// <param name="timeout"> the maximum time to wait for a permit </param>
        /// <param name="unit"> the time unit of the {@code timeout} argument </param>
        /// <returns> {@code true} if a permit was acquired and {@code false}
        ///         if the waiting time elapsed before a permit was acquired </returns>
        /// <exception cref="InterruptedException"> if the current thread is interrupted </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public boolean tryAcquire(long timeout, TimeUnit unit) throws InterruptedException
        public virtual bool TryAcquire(long timeout, TimeUnit unit)
        {
            return(Sync.TryAcquireSharedNanos(1, unit.ToNanos(timeout)));
        }
예제 #6
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException
        public virtual List <Future <T> > invokeAll <T, T1>(Collection <T1> tasks, long timeout, TimeUnit unit) where T1 : Callable <T>
        {
            if (tasks == null)
            {
                throw new NullPointerException();
            }
            long nanos = unit.ToNanos(timeout);
            List <Future <T> > futures = new List <Future <T> >(tasks.Size());
            bool done = false;

            try
            {
                foreach (Callable <T> t in tasks)
                {
                    futures.Add(NewTaskFor(t));
                }

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final long deadline = System.nanoTime() + nanos;
                long deadline = System.nanoTime() + nanos;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int size = futures.size();
                int size = futures.Size();

                // Interleave time checks and calls to execute in case
                // executor doesn't have any/much parallelism.
                for (int i = 0; i < size; i++)
                {
                    Execute((Runnable)futures.Get(i));
                    nanos = deadline - System.nanoTime();
                    if (nanos <= 0L)
                    {
                        return(futures);
                    }
                }

                for (int i = 0; i < size; i++)
                {
                    Future <T> f = futures.Get(i);
                    if (!f.Done)
                    {
                        if (nanos <= 0L)
                        {
                            return(futures);
                        }
                        try
                        {
                            f.Get(nanos, TimeUnit.NANOSECONDS);
                        }
                        catch (CancellationException)
                        {
                        }
                        catch (ExecutionException)
                        {
                        }
                        catch (TimeoutException)
                        {
                            return(futures);
                        }
                        nanos = deadline - System.nanoTime();
                    }
                }
                done = true;
                return(futures);
            }
            finally
            {
                if (!done)
                {
                    for (int i = 0, size = futures.Size(); i < size; i++)
                    {
                        futures.Get(i).Cancel(true);
                    }
                }
            }
        }
예제 #7
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
        public virtual T invokeAny <T, T1>(Collection <T1> tasks, long timeout, TimeUnit unit) where T1 : Callable <T>
        {
            return(DoInvokeAny(tasks, true, unit.ToNanos(timeout)));
        }