private int[] computeClusterNumberRange(int min, int max, int hop, Optimizer optimizer)
      throws ExecutionException, InterruptedException {
    ExecutorService executor;
    if (parallelWorkers > 1) {
      executor = Executors.newFixedThreadPool(2);
    } else {
      executor = Executors.newFixedThreadPool(1);
    }
    ConcurrentMap<Integer, Double> sharedScores = new ConcurrentHashMap<>();
    SearchSpaceBoundaryFinder_old upperBoundFinder =
        new SearchSpaceUpperBoundaryFinder_old(min, max, hop, optimizer, sharedScores);
    Future<Integer> futureUpperBound = executor.submit(upperBoundFinder);
    SearchSpaceBoundaryFinder_old lowerBoundFinder =
        new SearchSpaceLowerBoundaryFinder_old(min, max, hop, optimizer, sharedScores);
    Future<Integer> futureLowerBound = executor.submit(lowerBoundFinder);
    executor.shutdown();
    int[] r = new int[2];
    Integer realMin = futureLowerBound.get();
    Integer realMax = futureUpperBound.get();
    r[0] = realMin == null ? -1 : realMin;
    r[1] = realMax == null ? -1 : realMax;

    scores.putAll(lowerBoundFinder.getSplitsAndScores());
    // scores.putAll(upperBoundFinder.getSplitsAndScores());
    return r;
  }
  private void executeConcurrentRandomReadAndWriteOperations()
      throws InterruptedException, ExecutionException {
    for (int i = 0; i < THREAD_COUNT; i++) {
      futures.add(executorService.submit(new Writer()));
    }
    for (int i = 0; i < THREAD_COUNT; i++) {
      futures.add(executorService.submit(new Reader()));
    }

    for (Future<Void> future : futures) future.get();
  }
  /** {@inheritDoc} */
  @Override
  public void loadCache(GridBiInClosure<K, V> c, @Nullable Object... args) throws GridException {
    ExecutorService exec =
        new ThreadPoolExecutor(
            threadsCnt,
            threadsCnt,
            0L,
            MILLISECONDS,
            new ArrayBlockingQueue<Runnable>(batchQueueSize),
            new BlockingRejectedExecutionHandler());

    Iterator<I> iter = inputIterator(args);

    Collection<I> buf = new ArrayList<>(batchSize);

    try {
      while (iter.hasNext()) {
        if (Thread.currentThread().isInterrupted()) {
          U.warn(log, "Working thread was interrupted while loading data.");

          break;
        }

        buf.add(iter.next());

        if (buf.size() == batchSize) {
          exec.submit(new Worker(c, buf, args));

          buf = new ArrayList<>(batchSize);
        }
      }

      if (!buf.isEmpty()) exec.submit(new Worker(c, buf, args));
    } catch (RejectedExecutionException ignored) {
      // Because of custom RejectedExecutionHandler.
      assert false : "RejectedExecutionException was thrown while it shouldn't.";
    } finally {
      exec.shutdown();

      try {
        exec.awaitTermination(Long.MAX_VALUE, MILLISECONDS);
      } catch (InterruptedException ignored) {
        U.warn(log, "Working thread was interrupted while waiting for put operations to complete.");

        Thread.currentThread().interrupt();
      }
    }
  }
  public static void main(String[] args) {
    ExecutorService pool = Executors.newFixedThreadPool(2);

    final Vector[] vecs = {
      new Vector(), new Vector(),
    };
    vecs[0].add(vecs[1]);
    vecs[1].add(vecs[0]);

    for (int i = 0; i < 2; i++) {
      final int threadNumber = i;
      pool.submit(
          new Callable() {
            public Object call() throws Exception {
              for (int i = 0; i < 1000 * 1000; i++) {
                ObjectOutputStream out = new ObjectOutputStream(new NullOutputStream());
                out.writeObject(vecs[threadNumber]);
                out.close();
              }
              System.out.println("done");
              return null;
            }
          });
    }
  }
示例#5
0
  public static <T> List<T> executeAndGetResult(
      Collection<? extends Callable<T>> tasks, long waitingTimeInMillis) {
    ExecutorService executor = Executors.newFixedThreadPool(tasks.size());
    List<T> finalResults = new ArrayList<T>(tasks.size());
    List<Future<T>> temporaryResults = new ArrayList<Future<T>>(tasks.size());

    try {
      for (Callable<T> task : tasks) {
        Future<T> future = executor.submit(task);
        temporaryResults.add(future);
      }
      executor.awaitTermination(waitingTimeInMillis, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
      throw new RuntimeException(e);
    } finally {
      executor.shutdown();
    }

    finalResults = new ArrayList<T>(temporaryResults.size());

    for (Future<T> result : temporaryResults) {
      try {
        finalResults.add(result.get());
      } catch (InterruptedException e) {
        throw new RuntimeException(e);
      } catch (ExecutionException e) {
        throw new RuntimeException(e);
      }
    }

    return finalResults;
  }
示例#6
0
 private CudaEngine(final int deviceId) {
   exe = Executors.newSingleThreadExecutor(); // mandatory: Only one cuda thread per context
   Id = deviceId;
   try {
     exe.submit(
             new Runnable() {
               @Override
               public void run() {
                 CUdevice device = new CUdevice();
                 JCudaDriver.cuDeviceGet(device, deviceId);
                 int array[] = {0};
                 JCudaDriver.cuDeviceGetAttribute(
                     array, CU_DEVICE_ATTRIBUTE_MAX_THREADS_PER_BLOCK, device);
                 maxThreads = (int) Math.sqrt(array[0]);
                 context = new CUcontext();
                 //					JCudaDriver.cuCtxCreate(context, CUctx_flags.CU_CTX_SCHED_BLOCKING_SYNC,
                 // device);
                 JCudaDriver.cuCtxCreate(context, 0, device);
                 CUmodule m = new CUmodule();
                 initModules(m);
                 for (Kernel k : Kernel.values()) {
                   initFunction(m, k);
                 }
                 //					JCudaDriver.cuCtxSetCacheConfig(CUfunc_cache.CU_FUNC_CACHE_PREFER_NONE);>
                 //
                 //	JCudaDriver.cuCtxSetSharedMemConfig(CUsharedconfig.CU_SHARED_MEM_CONFIG_EIGHT_BYTE_BANK_SIZE);
               }
             })
         .get();
   } catch (InterruptedException | ExecutionException e) {
     throw new RuntimeException(e.getMessage());
   }
   neigborsPtrs = new HashMap<>();
 }
示例#7
0
  private void singleRequestCall(
      ExecutorService jobService, final InstanceRequest request, int jobsPerRequest)
      throws Exception {
    TraceContext.register(request);
    Future[] tasks = new Future[jobsPerRequest];

    // fan out multiple threads for this request
    for (int i = 0; i < jobsPerRequest; i++) {
      final int taskId = i;
      tasks[i] =
          jobService.submit(
              new TraceRunnable() {
                @Override
                public void runJob() {
                  String tid = Thread.currentThread().getId() + "";
                  TraceContext.log(tid, request.getRequestId() + SEP + taskId);
                }
              });
    }

    // wait for all threads to finish the job
    for (int i = 0; i < jobsPerRequest; i++) {
      // block waiting
      tasks[i].get();
    }
    TraceContext.unregister(request);
  }
示例#8
0
    final void test() throws Exception {
      Future[] futures = new Future[nthreads];
      for (int i = 0; i < nthreads; ++i) futures[i] = pool.submit(this);

      barrier.await();
      Thread.sleep(TIMEOUT);
      boolean tooLate = false;
      for (int i = 1; i < nthreads; ++i) {
        if (!futures[i].cancel(true)) tooLate = true;
        // Unbunch some of the cancels
        if ((i & 3) == 0) Thread.sleep(1 + rng.next() % 10);
      }

      Object f0 = futures[0].get();
      if (!tooLate) {
        for (int i = 1; i < nthreads; ++i) {
          if (!futures[i].isDone() || !futures[i].isCancelled())
            throw new Error("Only one thread should complete");
        }
      } else System.out.print("(cancelled too late) ");

      long endTime = System.nanoTime();
      long time = endTime - timer.startTime;
      if (print) {
        double secs = (double) (time) / 1000000000.0;
        System.out.println("\t " + secs + "s run time");
      }
    }
 /** Execute a task that is executing something else inside. Nested Execution. */
 @Test(timeout = 10000)
 public void testNestedExecution() throws Exception {
   Callable<String> task = new NestedExecutorTask();
   ExecutorService executor = createSingleNodeExecutorService("testNestedExecution");
   Future future = executor.submit(task);
   future.get();
 }
示例#10
0
  void renderPage(CharSequence source) {
    final List<ImageInfo> imageInfos = scanForImageInfo(source);
    Callable<List<ImageData>> task =
        new Callable<List<ImageData>>() {
          public List<ImageData> call() {
            List<ImageData> result = new ArrayList<ImageData>();
            for (ImageInfo imageInfo : imageInfos) result.add(imageInfo.downloadImage());
            return result;
          }
        };

    Future<List<ImageData>> future = executor.submit(task);
    renderText(source);

    try {
      List<ImageData> imageData = future.get();
      for (ImageData data : imageData) renderImage(data);
    } catch (InterruptedException e) {
      // Re-assert the thread's interrupted status
      Thread.currentThread().interrupt();
      // We don't need the result, so cancel the task too
      future.cancel(true);
    } catch (ExecutionException e) {
      throw launderThrowable(e.getCause());
    }
  }
  @Override
  public Boolean call() throws Exception {

    long timeoutMillis = 5000;

    try {
      getServersFile();
      getZkRunning();

      while (true) {
        while (!restartQueue.isEmpty()) {
          LOG.debug("Restart queue size [" + restartQueue.size() + "]");
          RestartHandler handler = restartQueue.poll();
          Future<ScriptContext> runner = pool.submit(handler);
          ScriptContext scriptContext = runner.get(); // blocking call
          if (scriptContext.getExitCode() != 0) restartQueue.add(handler);
        }

        try {
          Thread.sleep(timeoutMillis);
        } catch (InterruptedException e) {
        }
      }

    } catch (Exception e) {
      e.printStackTrace();
      LOG.error(e);
      pool.shutdown();
      throw e;
    }
  }
示例#12
0
  /**
   * Somewhat multi-threaded depth-first search. Performs a DFS of the subtrees from the current
   * node in parallel.
   *
   * @param s The tile sequence
   * @param b The board to search
   * @param pool The thread pool in which to submit jobs to.
   * @return The board with the highest evaluation or null if no board can continue.
   */
  private Board solve_pdfs(Board b, ExecutorService pool) {
    List<Future<Board>> rets = new ArrayList<>(BOARD_WIDTH);
    Board best = null;
    int best_score = -1;

    for (Direction d : directions) {
      Board n = new Board(b);
      if (n.move(tileSequence, d)) {
        rets.add(pool.submit(new ParallelDFS(n)));
      }
    }

    for (Future<Board> ret : rets) {
      try {
        Board c = ret.get();
        if (c != null) {
          int score = evaluate(c);
          if (score > best_score) {
            best = c;
            best_score = score;
          }
        }
      } catch (InterruptedException | ExecutionException e) {
        System.err.println("Error: " + e.getMessage());
      }
    }

    return best;
  }
 /** Run a basic task */
 @Test
 public void testBasicTask() throws Exception {
   Callable<String> task = new BasicTestTask();
   ExecutorService executor = createSingleNodeExecutorService("testBasicTask");
   Future future = executor.submit(task);
   assertEquals(future.get(), BasicTestTask.RESULT);
 }
示例#14
0
 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
   if (attrs.isRegularFile()) {
     if (m != null) m.reset(root.relativize(file).toString());
     if (m == null || m.matches())
       futures.add(exec.submit(new FileLoader(root, file, blocSize)));
   }
   return FileVisitResult.CONTINUE;
 }
示例#15
0
 static Future<String> futureOutputOf(final InputStream is) {
   return drainers.submit(
       new Callable<String>() {
         public String call() throws IOException {
           return outputOf(is);
         }
       });
 }
  @Test
  public void testPostregisteredExecutionCallbackCompletableFuture() throws Exception {
    HazelcastInstanceProxy proxy = (HazelcastInstanceProxy) createHazelcastInstance();
    Field originalField = HazelcastInstanceProxy.class.getDeclaredField("original");
    originalField.setAccessible(true);
    HazelcastInstanceImpl hz = (HazelcastInstanceImpl) originalField.get(proxy);
    NodeEngine nodeEngine = hz.node.nodeEngine;
    ExecutionService es = nodeEngine.getExecutionService();

    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    final ExecutorService executorService = Executors.newSingleThreadExecutor();
    try {
      Future future =
          executorService.submit(
              new Callable<String>() {
                @Override
                public String call() {
                  try {
                    return "success";
                  } finally {
                    latch1.countDown();
                  }
                }
              });

      final ICompletableFuture completableFuture = es.asCompletableFuture(future);
      latch1.await(30, TimeUnit.SECONDS);

      final AtomicReference reference = new AtomicReference();
      completableFuture.andThen(
          new ExecutionCallback() {
            @Override
            public void onResponse(Object response) {
              reference.set(response);
              latch2.countDown();
            }

            @Override
            public void onFailure(Throwable t) {
              reference.set(t);
              latch2.countDown();
            }
          });

      latch2.await(30, TimeUnit.SECONDS);
      if (reference.get() instanceof Throwable) {
        ((Throwable) reference.get()).printStackTrace();
      }

      assertEquals("success", reference.get());

    } finally {
      executorService.shutdown();
    }
  }
示例#17
0
 public Future<Float> calculateFloat(final float x, final float y) {
   return ex.submit(
       new Callable<Float>() {
         public Float call() {
           print("starting " + x + " + " + y);
           pause(2000);
           return x + y;
         }
       });
 }
示例#18
0
 /**
  * Execute database transactions in an isolated context.
  *
  * @param <T> return type
  * @param callable transaction
  * @return
  */
 synchronized <T> T exec(Callable<T> callable) {
   // We want to submit db jobs to an executor to isolate
   // them from the current thread,
   Future<T> future = _executor.submit(callable);
   try {
     return future.get();
   } catch (Exception e) {
     throw new ContextException("DbError", e);
   }
 }
示例#19
0
 public Future<Integer> calculateInt(final int x, final int y) {
   return ex.submit(
       new Callable<Integer>() {
         public Integer call() {
           print("starting " + x + " + " + y);
           pause(500);
           return x + y;
         }
       });
 }
  private void fillFilesWithContent() throws InterruptedException, ExecutionException, IOException {
    for (int i = 0; i < THREAD_COUNT; i++) {
      futures.add(executorService.submit(new Writer()));
    }

    for (Future<Void> future : futures) future.get();

    futures.clear();

    buffer.flushBuffer();
  }
  @Test
  public void testCancellationAwareTask2() {
    Callable task1 = new CancellationAwareTask(5000);
    ExecutorService executor = createSingleNodeExecutorService("testCancellationAwareTask", 1);
    executor.submit(task1);

    Callable task2 = new BasicTestTask();
    Future future = executor.submit(task2);
    assertFalse(future.isDone());
    assertTrue(future.cancel(true));
    assertTrue(future.isCancelled());
    assertTrue(future.isDone());

    try {
      future.get();
      fail("Should not complete the task successfully");
    } catch (CancellationException expected) {
    } catch (Exception e) {
      fail("Unexpected exception " + e);
    }
  }
示例#22
0
 /** Free memory from the currently registered CUObjects */
 public void freeCUObjectsMemory() {
   exe.submit(
       new Runnable() {
         @Override
         public void run() {
           cuCtxSynchronize();
           for (CudaObject co : cudaObjects) {
             co.freeMemory();
           }
           JCudaDriver.cuCtxDestroy(context);
         }
       });
 }
  /** Shutting down the cluster should act as the ExecutorService shutdown */
  @Test(expected = RejectedExecutionException.class)
  public void testClusterShutdown() throws Exception {
    ExecutorService executor = createSingleNodeExecutorService("testClusterShutdown");
    shutdownNodeFactory();
    Thread.sleep(2000);

    assertNotNull(executor);
    assertTrue(executor.isShutdown());
    assertTrue(executor.isTerminated());

    // New tasks must be rejected
    Callable<String> task = new BasicTestTask();
    executor.submit(task);
  }
示例#24
0
 public static boolean init() {
   synchronized (cudaEngines) {
     System.err.println("---------Initializing Cuda----------------");
     try {
       extractAndLoadNativeLibs();
       JCudaDriver.setExceptionsEnabled(true);
       JCudaDriver.cuInit(0);
       compileKernelsPtx();
       // Obtain the number of devices
       int deviceCountArray[] = {0};
       JCudaDriver.cuDeviceGetCount(deviceCountArray);
       availableDevicesNb = deviceCountArray[0];
       if (availableDevicesNb == 0) return false;
       availableDevicesNb = NB_OF_DEVICE_TO_USE; // TODO
       initialization = Executors.newCachedThreadPool();
       System.out.println("Found " + availableDevicesNb + " GPU devices");
       for (int i = 0 /*-NB_OF_DEVICE_TO_USE*/; i < availableDevicesNb; i++) {
         final int index = i;
         Future<?> initJob =
             initialization.submit(
                 new Runnable() {
                   public void run() {
                     System.err.println("Initializing device n°" + index);
                     cudaEngines.put(index, new CudaEngine(index));
                   }
                 });
         initJob.get();
         initialization.shutdown();
       }
     } catch (InterruptedException
         | ExecutionException
         | IOException
         | CudaException
         | UnsatisfiedLinkError e) {
       e.printStackTrace();
       System.err.println("---------Cannot initialize Cuda !!! ----------------");
       return false;
     }
     Runtime.getRuntime()
         .addShutdownHook(
             new Thread() {
               @Override
               public void run() {
                 CudaEngine.stop();
               }
             });
     System.out.println("---------Cuda Initialized----------------");
     return true;
   }
 }
 /** Test for the issue 129. Repeatedly runs tasks and check for isDone() status after get(). */
 @Test
 public void testIsDoneMethod2() throws Exception {
   ExecutorService executor = createSingleNodeExecutorService("isDoneMethod2");
   for (int i = 0; i < COUNT; i++) {
     Callable<String> task1 = new BasicTestTask();
     Callable<String> task2 = new BasicTestTask();
     Future future1 = executor.submit(task1);
     Future future2 = executor.submit(task2);
     assertEquals(future2.get(), BasicTestTask.RESULT);
     assertTrue(future2.isDone());
     assertEquals(future1.get(), BasicTestTask.RESULT);
     assertTrue(future1.isDone());
   }
 }
示例#26
0
 public void cuCtxSynchronize() {
   try {
     exe.submit(
             new Callable<Void>() {
               @Override
               public Void call() throws Exception {
                 JCudaDriver.cuCtxSynchronize();
                 return null;
               }
             })
         .get();
   } catch (InterruptedException | ExecutionException e) {
     e.printStackTrace();
   }
 }
  public static void main(String[] args) {
    ExecutorService executorService = Executors.newFixedThreadPool(4);
    List<Future<String>> results = new ArrayList<>();
    String[] URLs = {
      "http://www.weather.gov",
      "http://www.espn.com",
      "http://www.marketwatch.com",
      "http://www.fandango.com"
    };

    for (String URL : URLs) {
      Callable callable =
          new Callable<String>() {
            @Override
            public String call() throws Exception {
              Thread.sleep(1000);
              System.out.println("Retrieving URL: " + URL);
              return SiteDownloader.get(URL);
            }
          };

      System.out.println("Submitting task for: " + URL);
      results.add(executorService.submit(callable));
    }

    boolean done = false;
    while (!done) {
      System.out.println("Still getting data...");
      done = areAllTrue(results);
      try {
        Thread.sleep(200);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }

    for (Future<String> result : results) {
      try {
        System.out.println("Site data: " + result.get());
      } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
      }
    }
    executorService.shutdown();
  }
 /** Test multiple Future.get() invocation */
 @Test
 public void testMultipleFutureGets() throws Exception {
   Callable<String> task = new BasicTestTask();
   ExecutorService executor = createSingleNodeExecutorService("isTwoGetFromFuture");
   Future<String> future = executor.submit(task);
   String s1 = future.get();
   assertEquals(s1, BasicTestTask.RESULT);
   assertTrue(future.isDone());
   String s2 = future.get();
   assertEquals(s2, BasicTestTask.RESULT);
   assertTrue(future.isDone());
   String s3 = future.get();
   assertEquals(s3, BasicTestTask.RESULT);
   assertTrue(future.isDone());
   String s4 = future.get();
   assertEquals(s4, BasicTestTask.RESULT);
   assertTrue(future.isDone());
 }
示例#29
0
  @Override
  public boolean isAuthorized(
      @WebParam(name = "authenticationData", targetNamespace = "")
          List<SecretAuthenticationKey> authenticationData,
      @WebParam(name = "action", targetNamespace = "") Action action)
      throws SNAAExceptionException {

    if (authenticationData == null || action == null) {
      throw createSNAAException("Arguments must not be null!");
    }

    Map<String, Set<SecretAuthenticationKey>> intersectionPrefixSet =
        getIntersectionPrefixSetSAK(authenticationData);

    Set<Future<Boolean>> futures = new HashSet<Future<Boolean>>();

    for (String urnPrefix : intersectionPrefixSet.keySet()) {
      IsAuthorizedCallable authenticationCallable =
          new IsAuthorizedCallable(
              getWsnUrlFromUrnPrefix(urnPrefix),
              new ArrayList<SecretAuthenticationKey>(intersectionPrefixSet.get(urnPrefix)),
              action);
      Future<Boolean> future = executorService.submit(authenticationCallable);
      futures.add(future);
    }

    for (Future<Boolean> future : futures) {
      try {
        if (!future.get()) {
          return false;
        }
      } catch (InterruptedException e) {
        throw createSNAAException(e.getMessage());
      } catch (ExecutionException e) {
        throw createSNAAException(e.getMessage());
      }
    }

    return true;
  }
示例#30
0
  @Override
  public List<SecretAuthenticationKey> authenticate(
      @WebParam(name = "authenticationData", targetNamespace = "")
          List<AuthenticationTriple> authenticationData)
      throws AuthenticationExceptionException, SNAAExceptionException {

    Map<String, Set<AuthenticationTriple>> intersectionPrefixSet =
        getIntersectionPrefixSetAT(authenticationData);

    Set<Future<List<SecretAuthenticationKey>>> futures =
        new HashSet<Future<List<SecretAuthenticationKey>>>();

    for (String wsEndpointUrl : intersectionPrefixSet.keySet()) {
      AuthenticationCallable authenticationCallable =
          new AuthenticationCallable(
              wsEndpointUrl,
              new ArrayList<AuthenticationTriple>(intersectionPrefixSet.get(wsEndpointUrl)));
      Future<List<SecretAuthenticationKey>> future = executorService.submit(authenticationCallable);
      futures.add(future);
    }

    List<SecretAuthenticationKey> resultSet = new LinkedList<SecretAuthenticationKey>();

    for (Future<List<SecretAuthenticationKey>> future : futures) {
      try {
        resultSet.addAll(future.get());
      } catch (InterruptedException e) {
        SNAAException exception = new SNAAException();
        exception.setMessage(e.getMessage());
        throw new SNAAExceptionException(e.getMessage(), exception, e);
      } catch (ExecutionException e) {
        SNAAException exception = new SNAAException();
        exception.setMessage(e.getMessage());
        throw new SNAAExceptionException(e.getMessage(), exception, e);
      }
    }

    return resultSet;
  }