OnJava8-Examples/concurrency/SemaphoreDemo.java

98 lines
3.2 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// concurrency/SemaphoreDemo.java
2015-12-15 11:47:04 -08:00
// (c)2016 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
2015-06-15 17:47:35 -07:00
// Testing the Pool class
import java.util.concurrent.*;
import java.util.*;
// A task to check a resource out of a pool:
class CheckoutTask<T> implements Runnable {
private static int counter = 0;
private final int id = counter++;
private Pool<T> pool;
public CheckoutTask(Pool<T> pool) {
this.pool = pool;
}
@Override
public void run() {
try {
T item = pool.checkOut();
2015-11-03 12:00:44 -08:00
System.out.println(this + "checked out " + item);
2015-06-15 17:47:35 -07:00
TimeUnit.SECONDS.sleep(1);
2015-11-03 12:00:44 -08:00
System.out.println(this +"checking in " + item);
2015-06-15 17:47:35 -07:00
pool.checkIn(item);
} catch(InterruptedException e) {
// Acceptable way to terminate
}
}
@Override
public String toString() {
return "CheckoutTask " + id + " ";
}
}
public class SemaphoreDemo {
final static int SIZE = 25;
public static void
main(String[] args) throws Exception {
final Pool<Fat> pool = new Pool<>(Fat.class, SIZE);
ExecutorService exec = Executors.newCachedThreadPool();
for(int i = 0; i < SIZE; i++)
exec.execute(new CheckoutTask<>(pool));
2015-11-03 12:00:44 -08:00
System.out.println("All CheckoutTasks created");
2015-06-15 17:47:35 -07:00
List<Fat> list = new ArrayList<>();
for(int i = 0; i < SIZE; i++) {
Fat f = pool.checkOut();
2015-11-03 12:00:44 -08:00
System.out.print(i + ": main() thread checked out ");
2015-06-15 17:47:35 -07:00
f.operation();
list.add(f);
}
Future<?> blocked = exec.submit(() -> {
try {
// Semaphore prevents additional checkout,
// so call is blocked:
pool.checkOut();
} catch(InterruptedException e) {
2015-11-03 12:00:44 -08:00
System.out.println("checkOut() Interrupted");
2015-06-15 17:47:35 -07:00
}
});
TimeUnit.SECONDS.sleep(2);
blocked.cancel(true); // Break out of blocked call
2015-11-03 12:00:44 -08:00
System.out.println("Checking in objects in " + list);
2015-06-15 17:47:35 -07:00
for(Fat f : list)
pool.checkIn(f);
for(Fat f : list)
pool.checkIn(f); // Second checkIn ignored
exec.shutdown();
}
2015-09-07 11:44:36 -06:00
}
/* Output: (First and last 10 Lines)
2015-06-15 17:47:35 -07:00
CheckoutTask 4 checked out Fat id: 2
CheckoutTask 22 checked out Fat id: 24
CheckoutTask 14 checked out Fat id: 23
CheckoutTask 21 checked out Fat id: 22
CheckoutTask 18 checked out Fat id: 21
CheckoutTask 17 checked out Fat id: 20
CheckoutTask 10 checked out Fat id: 19
CheckoutTask 13 checked out Fat id: 18
CheckoutTask 9 checked out Fat id: 17
CheckoutTask 6 checked out Fat id: 16
________...________...________...________...________
20: main() thread checked out Fat id: 5
21: main() thread checked out Fat id: 1
CheckoutTask 7 checking in Fat id: 3
CheckoutTask 0 checking in Fat id: 0
CheckoutTask 8 checking in Fat id: 4
22: main() thread checked out Fat id: 3
23: main() thread checked out Fat id: 0
24: main() thread checked out Fat id: 4
checkOut() Interrupted
Checking in objects in [Fat id: 2, Fat id: 23, Fat id: 24,
Fat id: 22, Fat id: 21, Fat id: 20, Fat id: 19, Fat id: 18,
Fat id: 17, Fat id: 16, Fat id: 15, Fat id: 14, Fat id: 13,
Fat id: 11, Fat id: 12, Fat id: 9, Fat id: 10, Fat id: 8,
Fat id: 7, Fat id: 6, Fat id: 5, Fat id: 1, Fat id: 3, Fat
id: 0, Fat id: 4]
2015-09-07 11:44:36 -06:00
*/