public static void main(String[] args) throws Exception {
   ForkJoinPool pool = new ForkJoinPool();
   // 提交可分解的PrintTask任务
   pool.submit(new PrintTask(0, 600));
   pool.awaitTermination(2, TimeUnit.SECONDS);
   // 关闭线程池
   pool.shutdown();
 }
Пример #2
0
 /**
  * This method is required to be public, but should never be called explicitly. It performs the
  * main run loop to execute {@link ForkJoinTask}s.
  */
 public void run() {
   Throwable exception = null;
   try {
     onStart();
     pool.runWorker(workQueue);
   } catch (Throwable ex) {
     exception = ex;
   } finally {
     try {
       onTermination(exception);
     } catch (Throwable ex) {
       if (exception == null) exception = ex;
     } finally {
       pool.deregisterWorker(this, exception);
     }
   }
 }
Пример #3
0
 static void test(ForkJoinPool pool, int num) throws Exception {
   int ps = pool.getParallelism();
   long start = System.nanoTime();
   DynamicFib f = new DynamicFib(num);
   pool.invoke(f);
   long time = System.nanoTime() - start;
   double secs = ((double) time) / NPS;
   long result = f.number;
   System.out.print("DynamicFib " + num + " = " + result);
   System.out.printf("\tTime: %9.3f", secs);
   long sc = pool.getStealCount();
   long ns = sc - lastStealCount;
   lastStealCount = sc;
   System.out.printf(" Steals: %4d", ns / ps);
   System.out.printf(" Workers: %4d", pool.getPoolSize());
   System.out.println();
 }
Пример #4
0
 public static void main(String[] args) throws Exception {
   procs = 0;
   int num = 45;
   try {
     if (args.length > 0) procs = Integer.parseInt(args[0]);
     if (args.length > 1) num = Integer.parseInt(args[1]);
   } catch (Exception e) {
     System.out.println("Usage: java DynamicFib <threads> <number>");
     return;
   }
   for (int reps = 0; reps < 2; ++reps) {
     ForkJoinPool pool = (procs == 0) ? new ForkJoinPool() : new ForkJoinPool(procs);
     for (int i = 0; i < 20; ++i) test(pool, num);
     System.out.println(pool);
     pool.shutdown();
   }
 }
Пример #5
0
  public static void main(String args[]) {
    int pLevel;
    int threshold;

    if (args.length != 2) {
      System.out.println("Usage: FJExperiment threshold parallism");
      return;
    }

    pLevel = Integer.parseInt(args[0]);
    threshold = Integer.parseInt(args[1]);

    // These variables are used to time the task.
    long beginT, endT;

    // Create a task pool.  Notice that the parallelsim level is set.
    ForkJoinPool fjp = new ForkJoinPool(pLevel);

    double[] nums = new double[1000000];

    for (int i = 0; i < nums.length; i++) nums[i] = (double) i;

    Transform task = new Transform(nums, 0, nums.length, threshold);

    // Starting timing.
    beginT = System.nanoTime();

    // Start the main ForkJoinTask.
    fjp.invoke(task);

    // End timing.
    endT = System.nanoTime();

    System.out.println("Level of parallelism: " + pLevel);
    System.out.println("Sequential threshold: " + threshold);
    System.out.println("Elapsed time: " + (endT - beginT) + " ns");
    System.out.println();
  }
Пример #6
0
 /**
  * Creates a ForkJoinWorkerThread operating in the given pool.
  *
  * @param pool the pool this thread works in
  * @throws NullPointerException if pool is null
  */
 protected ForkJoinWorkerThread(ForkJoinPool pool) {
   // Use a placeholder until a useful name can be set in registerWorker
   super("aForkJoinWorkerThread");
   this.pool = pool;
   this.workQueue = pool.registerWorker(this);
 }
Пример #7
0
 public static void main(String[] args) throws Exception {
   int n = 1 << 18;
   int reps = 1 << 8;
   Rand[] array = new Rand[n];
   for (int i = 0; i < n; ++i) array[i] = new Rand(i + 1);
   ForkJoinPool fjp = new ForkJoinPool(1);
   ParallelArray<Rand> pa = ParallelArray.createUsingHandoff(array, fjp);
   final GetNext getNext = new GetNext();
   final Accum accum = new Accum();
   final Long zero = Long.valueOf(0);
   long last, now;
   double elapsed;
   for (int j = 0; j < 2; ++j) {
     long rseed = rng.nextLong();
     resetSeeds(array, rseed);
     long seqsum = 0;
     last = System.nanoTime();
     for (int k = 0; k < reps; ++k) {
       seqsum += seqMapReduce(array, getNext, accum, zero);
       Rand tmp = array[k];
       array[k] = array[n - k - 1];
       array[n - k - 1] = tmp;
     }
     now = System.nanoTime();
     elapsed = (double) (now - last) / NPS;
     last = now;
     System.out.printf("sequential:    %7.3f\n", elapsed);
     for (int i = 2; i <= NCPU; i <<= 1) {
       resetSeeds(array, rseed);
       long sum = 0;
       fjp.setPoolSize(i);
       last = System.nanoTime();
       for (int k = 0; k < reps; ++k) {
         sum += pa.withMapping(getNext).reduce(accum, zero);
         Rand tmp = array[k];
         array[k] = array[n - k - 1];
         array[n - k - 1] = tmp;
       }
       now = System.nanoTime();
       elapsed = (double) (now - last) / NPS;
       last = now;
       System.out.printf("poolSize %3d:  %7.3f\n", i, elapsed);
       if (sum != seqsum) throw new Error("checksum");
     }
     for (int i = NCPU; i >= 1; i >>>= 1) {
       resetSeeds(array, rseed);
       long sum = 0;
       fjp.setPoolSize(i);
       last = System.nanoTime();
       for (int k = 0; k < reps; ++k) {
         sum += pa.withMapping(getNext).reduce(accum, zero);
         Rand tmp = array[k];
         array[k] = array[n - k - 1];
         array[n - k - 1] = tmp;
       }
       now = System.nanoTime();
       elapsed = (double) (now - last) / NPS;
       last = now;
       System.out.printf("poolSize %3d:  %7.3f\n", i, elapsed);
       if (sum != seqsum) throw new Error("checksum");
     }
   }
   fjp.shutdownNow();
   fjp.awaitTermination(1, TimeUnit.SECONDS);
   Thread.sleep(100);
 }
Пример #8
0
 Long countOccurrencesInParallel(Folder folder, String searchedWord) {
   return forkJoinPool.invoke(new FolderSearchTask(folder, searchedWord));
 }