JezzaShea

Untitled

Aug 11th, 2014
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.59 KB | None | 0 0
  1. package org.gridgain.examples.compute;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5.  
  6. import org.gridgain.grid.Grid;
  7. import org.gridgain.grid.GridException;
  8. import org.gridgain.grid.GridFuture;
  9. import org.gridgain.grid.GridGain;
  10. import org.gridgain.grid.lang.GridCallable;
  11.  
  12. public class ComputeSleepExample {
  13.  
  14.     public static void main(String[] args) throws Exception {
  15.         // start gridgain node & invoke sleep jobs on it
  16.         try (Grid grid = GridGain.start("examples/config/example-compute.xml")) {
  17.             System.out.println(">>> Sleep example started.");
  18.             jobs(grid, args[0], args[1]);
  19.             System.out.println("<<< Jobs done");
  20.        }
  21.     }
  22.  
  23.     public static void jobs(Grid g, String count, String wait) throws GridException {
  24.         GridFuture<Collection<Long>> rv = g.compute().call(jobs(Integer.parseInt(count), Integer.parseInt(wait)));
  25.         System.out.println("--- waiting");
  26.         rv.get();
  27.     }
  28.  
  29.     private static Collection<GridCallable<Long>> jobs(final int size, final int wait) {
  30.         Collection<GridCallable<Long>> clos = new ArrayList<>(size);
  31.         for (int i = 0; i < size; i++) {
  32.             clos.add(new GridCallable<Long>() {
  33.                 private static final long serialVersionUID = 1L;
  34.                 @Override
  35.                 public Long call() throws Exception {
  36.                     sleep(wait);
  37.                     System.out.println("done");
  38.                     return 33L;
  39.                 }
  40.             });
  41.         }
  42.         return clos;
  43.     }
  44.     private static void sleep(long pause) {
  45.         try {
  46.             Thread.sleep(pause);
  47.         } catch (InterruptedException e) {
  48. //          Thread.currentThread().interrupt();
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment