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>
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();
public static void main(String[] args) { Book novel = new Book(true); // Proper cleanup: novel.checkIn(); // Drop the reference, forget to clean up: new Book(true); // Force garbage collection & finalization: System.gc(); new Nap(1); // One second delay } }
public static void main(String[] args) { new TimedAbort(1); System.out.println("Napping for 4"); new Nap(4); } }
@Override public void run() { rand.ints(10, 0, 20) .mapToObj(Prioritized::new) .peek(p -> new Nap(rand.nextDouble() / 10)) .forEach(p -> queue.add(p)); queue.add(new Prioritized.EndSentinel()); } }
public static Machina work(Machina m) { if(!m.state.equals(State.END)){ new Nap(0.1); m.state = m.state.step(); } System.out.println(m); return m; } @Override
Step forward() { if(equals(BOXED)) return BOXED; new Nap(effort * 0.1); return values()[ordinal() + 1]; } }