xref: /OpenGrok/opengrok-indexer/src/main/java/org/opengrok/indexer/util/BlockingObjectPool.java (revision 9805b761e45c385edb69a70a4238545fe57f8ac0)
1b5840353SAdam Hornáček /*
2b5840353SAdam Hornáček  * The contents of this file are Copyright (c) 2012, Swaranga Sarma, DZone MVB
3b5840353SAdam Hornáček  * made available under free license,
4b5840353SAdam Hornáček  * http://javawithswaranga.blogspot.com/2011/10/generic-and-concurrent-object-pool.html
5b5840353SAdam Hornáček  * https://dzone.com/articles/generic-and-concurrent-object : "Feel free to use
6b5840353SAdam Hornáček  * it, change it, add more implementations. Happy coding!"
7b5840353SAdam Hornáček  * Copyright (c) 2017, Chris Fraire <cfraire@me.com>.
8b5840353SAdam Hornáček  */
9b5840353SAdam Hornáček 
10*9805b761SAdam Hornáček package org.opengrok.indexer.util;
11b5840353SAdam Hornáček 
12b5840353SAdam Hornáček import java.util.concurrent.TimeUnit;
13b5840353SAdam Hornáček 
14b5840353SAdam Hornáček /**
15b5840353SAdam Hornáček  * Represents an API for a pool of objects that makes the
16b5840353SAdam Hornáček  * requesting threads wait if no object is available.
17b5840353SAdam Hornáček  * @author Swaranga
18b5840353SAdam Hornáček  * @param <T> the type of objects to pool.
19b5840353SAdam Hornáček  */
20b5840353SAdam Hornáček public interface BlockingObjectPool<T> extends ObjectPool<T> {
21b5840353SAdam Hornáček 
22b5840353SAdam Hornáček     /**
23b5840353SAdam Hornáček      * Returns an instance of type T from the pool,
24b5840353SAdam Hornáček      * waiting up to the
25b5840353SAdam Hornáček      * specified wait time if necessary
26b5840353SAdam Hornáček      * for an object to become available..
27b5840353SAdam Hornáček      * <p>
28b5840353SAdam Hornáček      * The call is a blocking call,
29b5840353SAdam Hornáček      * and client threads are made to wait
30b5840353SAdam Hornáček      * for time until an object is available
31b5840353SAdam Hornáček      * or until the timeout occurs.
32b5840353SAdam Hornáček      * The call implements a fairness algorithm
33b5840353SAdam Hornáček      * that ensures that an FCFS service is implemented.
34b5840353SAdam Hornáček      * <p>
35b5840353SAdam Hornáček      * Clients are advised to react to {@link InterruptedException}.
36b5840353SAdam Hornáček      * If the thread is interrupted while waiting
37b5840353SAdam Hornáček      * for an object to become available,
38b5840353SAdam Hornáček      * most implementations
39b5840353SAdam Hornáček      * set the interrupted state of the thread
40b5840353SAdam Hornáček      * to <code>true</code> and returns null.
41b5840353SAdam Hornáček      * However this is subject to change
42b5840353SAdam Hornáček      * from implementation to implementation.
43b5840353SAdam Hornáček      *
44b5840353SAdam Hornáček      * @param time amount of time to wait before giving up,
45b5840353SAdam Hornáček      *   in units of {@code unit}
46b5840353SAdam Hornáček      * @param unit a {@code TimeUnit} determining
47b5840353SAdam Hornáček      *   how to interpret the {@code timeout} parameter
48b5840353SAdam Hornáček      * @return T an instance of the Object
49b5840353SAdam Hornáček      * of type T from the pool.
50b5840353SAdam Hornáček      * @throws InterruptedException
51b5840353SAdam Hornáček      * if interrupted while waiting
52b5840353SAdam Hornáček      */
get(long time, TimeUnit unit)53b5840353SAdam Hornáček     T get(long time, TimeUnit unit) throws InterruptedException;
54b5840353SAdam Hornáček }
55