public static void main(String[] args) { // Returns right away: new DiningPhilosophers(5); // [4] // Keeps main() from exiting: new Nap(3, "Shutdown"); } }
static <T> T prepare(T ingredient) { new Nap(0.1); return ingredient; } static <T> CompletableFuture<T> prep(T ingredient) {
@Override public synchronized int next() { ++currentEvenValue; new Nap(0.01); // Cause failure faster ++currentEvenValue; return currentEvenValue; } public static void main(String[] args) {
static Baked heat(Pan p) { new Nap(0.1); return new Baked(); } static CompletableFuture<Baked>
public FrostedCake(Baked baked, Frosting frosting) { new Nap(0.1); } @Override
@Override public void run() { new Nap(1); } }
static CompletableFuture<Frosting> make() { new Nap(0.1); return CompletableFuture .completedFuture(new Frosting()); } }
static void test(SerialNumbers producer) { for(int i = 0; i < 10; i++) CompletableFuture.runAsync( new SerialNumberChecker(producer)); new Nap(4, "No duplicates detected"); } }
public synchronized void method() { new Nap(0.01); callCount.incrementAndGet(); } }
public void method() { new Nap(0.01); synchronized(this) { callCount.incrementAndGet(); } } }
public static Workable work(Workable tt) { new Nap(tt.duration); // Seconds tt.id = tt.id + "W"; System.out.println(tt); return tt; } public static CompletableFuture<Workable>
@Override public void run() { System.out.println("Spinning"); new Nap(0.5); } }
@Override public void run() { System.out.println("Washing"); new Nap(0.5); } }
@Override public void run() { System.out.println("Rinsing"); new Nap(0.5); } }
public void g(boolean nap) { synchronized(syncObject) { for(int i = 0; i < 5; i++) { trace.add(String.format("g() " + i)); if(nap) new Nap(0.01); } } } }
@Override public void run() { while(running.get()) // [1] new Nap(0.1); System.out.print(id + " "); // [2] } }
public synchronized void f(boolean nap) { for(int i = 0; i < 5; i++) { trace.add(String.format("f() " + i)); if(nap) new Nap(0.01); } } private Object syncObject = new Object();
@Override public int next() { lock.lock(); try { ++currentEvenValue; new Nap(0.01); // Cause failure faster ++currentEvenValue; return currentEvenValue; } finally { lock.unlock(); } } public static void main(String[] args) {
@Override public void run() { new Nap(0.1); // Seconds System.out.println(this + " " + Thread.currentThread().getName()); } @Override
public static void main(String[] args) { ExecutorService es = Executors.newCachedThreadPool(); List<QuittableTask> tasks = IntStream.range(1, COUNT) .mapToObj(QuittableTask::new) .peek(qt -> es.execute(qt)) .collect(Collectors.toList()); new Nap(1); tasks.forEach(QuittableTask::quit); es.shutdown(); } }