package edu.rice.comp322; import edu.rice.hj.api.SuspendableException; import edu.rice.hj.runtime.config.HjSystemProperty; import java.util.Arrays; import static edu.rice.hj.Module0.finish; import static edu.rice.hj.Module0.launchHabaneroApp; import static edu.rice.hj.Module1.async; import static edu.rice.hj.Module1.forall; import static edu.rice.hj.Module1.forseq; /** * Solution to Worksheet 5. * * @author Shams Imam */ public final class Lec19HelpFirstWorkStealing { /** * Disallow instance creation of utility class. */ private Lec19HelpFirstWorkStealing() { super(); } /** * The main method. * * @param args an array of {@link String} objects. */ public static void main(final String[] args) { HjSystemProperty.numWorkers.set(2); HjSystemProperty.fjAsyncMode.set(false); // work-first stealing launchHabaneroApp(() -> { foo(); }); } private static void foo() throws SuspendableException { finish(() -> { print("A"); async(() -> { print("B"); work(6); }); async(() -> { print("C"); work(1); }); async(() -> { print("D"); work(1); }); print("E"); }); print("F"); } private static void print(String msg) { System.out.println(msg + " [from thread-" + Thread.currentThread().getId() + "]"); } private static void work(int timeInSecs) { try { Thread.sleep(timeInSecs * 1_000); } catch (InterruptedException e) { e.printStackTrace(); } } }