public T put ( T t , long timeoutInMillis , int maximumCapacity ) throws InterruptedException { if ( ( t == null ) || ( maximumCapacity > buffer . length ) ) { throw new IllegalArgumentException ( ) ; } long start = ( timeoutInMillis <= 0 ) ? 0 : - 1 ; long waitTime = timeoutInMillis ; //TODO: Add expedited put/fix waiting logic // D312598 - begin T ret = null ; while ( true ) { synchronized ( lock ) { if ( numberOfUsedSlots . get ( ) < maximumCapacity ) { insert ( t ) ; numberOfUsedSlots . getAndIncrement ( ) ; ret = t ; } } if ( ret != null ) { notifyGet_ ( ) ; return ret ; } if ( start == - 1 ) start = System . currentTimeMillis ( ) ; int spinctr = SPINS_PUT_ ; while ( numberOfUsedSlots . get ( ) >= buffer . length ) { if ( waitTime <= 0 ) { return null ; } // busy wait if ( spinctr > 0 ) { if ( YIELD_PUT_ ) Thread . yield ( ) ; spinctr -- ; } else { // block on lock waitPut_ ( timeoutInMillis ) ; } waitTime = timeoutInMillis - ( System . currentTimeMillis ( ) - start ) ; } } // D312598 - end }
Puts an object into the buffer . If the buffer is at or above the specified maximum capacity the call will block for up to the specified timeout period .