public T BorrowObject(long maxWaitMillisecs)
        {
            CheckClosed();

            PooledObject <T> p = null;
            bool             blockWhenExhausted = this.blockWhenExhausted;
            bool             create;

            while (p == null)
            {
                create = false;
                if (blockWhenExhausted)
                {
                    p = idleObjects.PollFirst();
                    if (p == null)
                    {
                        create = true;
                        p      = Create();
                    }

                    if (p == null)
                    {
                        if (maxWaitMillisecs < 0)
                        {
                            p = idleObjects.TakeFirst();
                        }
                        else
                        {
                            p = idleObjects.PollFirst(TimeSpan.FromMilliseconds(maxWaitMillisecs));
                        }
                    }

                    if (p == null)
                    {
                        throw new NoSuchElementException("Timeout waiting for idle object");
                    }

                    if (!p.Allocate())
                    {
                        p = null;
                    }
                }
                else
                {
                    p = idleObjects.PollFirst();
                    if (p == null)
                    {
                        create = true;
                        p      = Create();
                    }

                    if (p == null)
                    {
                        throw new NoSuchElementException("Pool exhausted");
                    }

                    if (!p.Allocate())
                    {
                        p = null;
                    }
                }

                if (p != null)
                {
                    try
                    {
                        factory.ActivateObject(p.TheObject);
                    }
                    catch (Exception e)
                    {
                        try
                        {
                            Destroy(p);
                        }
                        catch
                        {
                            // Ignore - activation failure is more important
                        }
                        p = null;
                        if (create)
                        {
                            NoSuchElementException nsee = new NoSuchElementException(
                                "Unable to activate object", e);
                            throw nsee;
                        }
                    }

                    if (p != null && TestOnBorrow)
                    {
                        bool      validate            = false;
                        Exception validationThrowable = null;
                        try
                        {
                            validate = factory.ValidateObject(p.TheObject);
                        }
                        catch (Exception ex)
                        {
                            PoolUtils.CheckRethrow(ex);
                        }
                        if (!validate)
                        {
                            try
                            {
                                Destroy(p);
                            }
                            catch
                            {
                                // Ignore - validation failure is more important
                            }
                            p = null;
                            if (create)
                            {
                                NoSuchElementException nsee = new NoSuchElementException(
                                    "Unable to validate object", validationThrowable);
                                throw nsee;
                            }
                        }
                    }
                }
            }

            return(p.TheObject);
        }