// Concurrent insertion & then iterator test.
  public static void testNonBlockingIdentityHashMapIterator() throws InterruptedException {
    final int ITEM_COUNT1 = 1000;
    final int THREAD_COUNT = 5;
    final int PER_CNT = ITEM_COUNT1 / THREAD_COUNT;
    final int ITEM_COUNT = PER_CNT * THREAD_COUNT; // fix roundoff for odd thread counts

    NonBlockingIdentityHashMap<Long, TestKey> nbhml =
        new NonBlockingIdentityHashMap<Long, TestKey>();
    // use a barrier to open the gate for all threads at once to avoid rolling
    // start and no actual concurrency
    final CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT);
    final ExecutorService ex = Executors.newFixedThreadPool(THREAD_COUNT);
    final CompletionService<Object> co = new ExecutorCompletionService<Object>(ex);
    for (int i = 0; i < THREAD_COUNT; i++) {
      co.submit(new NBHMLFeeder(nbhml, PER_CNT, barrier, i * PER_CNT));
    }
    for (int retCount = 0; retCount < THREAD_COUNT; retCount++) {
      co.take();
    }
    ex.shutdown();

    assertEquals("values().size()", ITEM_COUNT, nbhml.values().size());
    assertEquals("entrySet().size()", ITEM_COUNT, nbhml.entrySet().size());
    int itemCount = 0;
    for (TestKey K : nbhml.values()) itemCount++;
    assertEquals("values().iterator() count", ITEM_COUNT, itemCount);
  }
  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;
  }
  @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;
    }
  }
Exemple #4
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;
  }
    public NonBlockingIdentityHashMap<Long, TestKey> getMapMultithreaded()
        throws InterruptedException, ExecutionException {
      final int threadCount = _items.keySet().size();
      final NonBlockingIdentityHashMap<Long, TestKey> map =
          new NonBlockingIdentityHashMap<Long, TestKey>();

      // use a barrier to open the gate for all threads at once to avoid rolling start and no actual
      // concurrency
      final CyclicBarrier barrier = new CyclicBarrier(threadCount);
      final ExecutorService ex = Executors.newFixedThreadPool(threadCount);
      final CompletionService<Integer> co = new ExecutorCompletionService<Integer>(ex);
      for (Integer type : _items.keySet()) {
        // A linked-list of things to insert
        List<TestKey> items = _items.get(type);
        TestKeyFeederThread feeder = new TestKeyFeederThread(type, items, map, barrier);
        co.submit(feeder);
      }

      // wait for all threads to return
      int itemCount = 0;
      for (int retCount = 0; retCount < threadCount; retCount++) {
        final Future<Integer> result = co.take();
        itemCount += result.get();
      }
      ex.shutdown();
      return map;
    }
 public <T> Collection<T> getParallelResults(List<Node<T>> nodes) throws InterruptedException {
   ExecutorService exec = Executors.newCachedThreadPool();
   Queue<T> resultQueue = new ConcurrentLinkedQueue<T>();
   parallelRecursive(exec, nodes, resultQueue);
   exec.shutdown();
   exec.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
   return resultQueue;
 }
  @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();
    }
  }
 private synchronized void shutdown() {
   if (!exe.isShutdown()) {
     freeCUObjectsMemory();
   }
   exe.shutdown();
   try {
     System.err.println(
         "cuda device " + Id + " freed ? " + exe.awaitTermination(10, TimeUnit.SECONDS));
   } catch (InterruptedException e) {
     e.printStackTrace();
   }
 }
 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;
   }
 }
Exemple #10
0
 @Override
 @SuppressWarnings({"unchecked"})
 public void shutdown(long time, TimeUnit unit) {
   started = false;
   try {
     lock.tryAcquire(time, unit);
   } catch (InterruptedException e) {
     logger.warn("shutdown", e);
   }
   if (group != null) group.interrupt();
   consumers = new Consumer[0];
   consumerSeqs = new long[0];
   outUse = new AtomicLongArray(0);
   executor.shutdown();
 }
 @Override
 public Board solve(Board in) {
   if (useBacktracking) {
     return solve_mdfs(in);
   } else {
     if (nThreads == 1) {
       return solve_ldfs(in);
     } else {
       ExecutorService pool = Executors.newFixedThreadPool(nThreads);
       Board ret = solve_pndfs(in, pool);
       pool.shutdown();
       return ret;
     }
   }
 }
Exemple #12
0
  public static void execute(Collection<? extends Runnable> tasks, long waitingTimeInMillis) {
    ExecutorService executor = Executors.newFixedThreadPool(tasks.size());

    for (Runnable task : tasks) {
      executor.execute(task);
    }

    try {
      executor.awaitTermination(waitingTimeInMillis, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
      throw new RuntimeException(e);
    } finally {
      executor.shutdown();
    }
  }
  /** {@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();
      }
    }
  }
  /**
   * 'Learns' the factors that are good. More of, just iterate over all possible heuristic weight
   * combinations and you have to observe which ones are good.
   *
   * <p>This learning does not utilise backtracking - it sticks to using one weight only.
   *
   * @param b The board to learn against
   * @param learnClose Whether we are learning for the 'close' weight factors or not.
   */
  public void learn_factors(Board b, boolean learnClose) {
    int[] fl = learnClose ? closefactors : factors;
    int[] best = new int[fl.length];
    int best_score = -1;
    Board best_board = null;
    ExecutorService pool = Executors.newFixedThreadPool(nThreads);

    for (int i = learning_starts[0]; i < 19; i++) {
      for (int j = learning_starts[1]; j < 19; j++) {
        for (int k = learning_starts[2]; k < 8; k++) {
          for (int l = learning_starts[3]; l < 17; l++) {
            fl[0] = i;
            fl[1] = j;
            fl[2] = k;
            fl[3] = l;

            Board n = solve_pndfs(b, pool);
            int score = n.score();
            if (score > best_score) {
              System.arraycopy(factors, 0, best, 0, best.length);
              best_score = score;
              best_board = n;
            }
            System.out.printf("Current score: %d (%d moves)\n", score, n.nMoves());
            for (int v : fl) {
              System.out.printf("%d ", v);
            }
            System.out.println();
          }
          if (best_board != null) {
            System.out.println("Current best:");
            System.out.println(best_board);
            System.out.printf("%d (%d moves)\n", best_board.score(), best_board.nMoves());
          }
          for (int v : best) {
            System.out.printf("%d ", v);
          }
          System.out.println();
        }
      }
    }
    for (int v : best) System.out.printf("%d ", v);
    System.out.println();

    pool.shutdown();
  }
  /**
   * Gets rid of all the resources of the view. No other methods should be invoked on the view
   * afterwards.
   */
  @SuppressWarnings("unchecked")
  @Override
  public void dispose() {
    if (executor != null) {
      executor.shutdown();
      executor = null;
    }

    if (disposables != null) {
      for (Disposable d : (LinkedList<Disposable>) disposables.clone()) {
        d.dispose();
      }
      disposables = null;
    }

    removeAll();
  }
  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();
  }
  @Override
  public void onDestroy() {
    if (BuildConfig.DEBUG) Log.d(TAG, "Service shutting down");
    isRunning = false;
    updateReport();

    stopForeground(true);
    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    nm.cancel(NOTIFICATION_ID);

    downloadCompletionThread.interrupt();
    syncExecutor.shutdown();
    schedExecutor.shutdown();
    feedSyncThread.shutdown();
    cancelNotificationUpdater();
    unregisterReceiver(cancelDownloadReceiver);

    DBTasks.autodownloadUndownloadedItems(getApplicationContext());
  }
 @Override
 public void destroy() throws Exception {
   if (internalExecutorServiceRegistry.isEmpty()) {
     logger.info("开始释放相关资源");
     for (ExecutorService executor : internalExecutorServiceRegistry) {
       if (executor != null) {
         try {
           executor.shutdown();
           executor.awaitTermination(5, TimeUnit.MINUTES);
           executor = null;
         } catch (InterruptedException e) {
           logger.warn("interrupted when shuting down the query executor:\n{}", e);
         }
       }
     }
     getDataSourceSpecificExecutors().clear();
     logger.info("所有相关资源释放完闭");
   }
 }
Exemple #19
0
  public static void main(String[] args) throws Exception {
    HttpServer s1 = null;
    HttpsServer s2 = null;
    ExecutorService executor = null;
    try {
      String root = System.getProperty("test.src") + "/docs";
      System.out.print("Test12: ");
      InetSocketAddress addr = new InetSocketAddress(0);
      s1 = HttpServer.create(addr, 0);
      s2 = HttpsServer.create(addr, 0);
      HttpHandler h = new FileServerHandler(root);
      HttpContext c1 = s1.createContext("/test1", h);
      HttpContext c2 = s2.createContext("/test1", h);
      executor = Executors.newCachedThreadPool();
      s1.setExecutor(executor);
      s2.setExecutor(executor);
      ctx = new SimpleSSLContext(System.getProperty("test.src")).get();
      s2.setHttpsConfigurator(new HttpsConfigurator(ctx));
      s1.start();
      s2.start();

      int port = s1.getAddress().getPort();
      int httpsport = s2.getAddress().getPort();
      Runner r[] = new Runner[8];
      r[0] = new Runner(true, "http", root + "/test1", port, "smallfile.txt", 23);
      r[1] = new Runner(true, "http", root + "/test1", port, "largefile.txt", 2730088);
      r[2] = new Runner(true, "https", root + "/test1", httpsport, "smallfile.txt", 23);
      r[3] = new Runner(true, "https", root + "/test1", httpsport, "largefile.txt", 2730088);
      r[4] = new Runner(false, "http", root + "/test1", port, "smallfile.txt", 23);
      r[5] = new Runner(false, "http", root + "/test1", port, "largefile.txt", 2730088);
      r[6] = new Runner(false, "https", root + "/test1", httpsport, "smallfile.txt", 23);
      r[7] = new Runner(false, "https", root + "/test1", httpsport, "largefile.txt", 2730088);
      start(r);
      join(r);
      System.out.println("OK");
    } finally {
      delay();
      if (s1 != null) s1.stop(2);
      if (s2 != null) s2.stop(2);
      if (executor != null) executor.shutdown();
    }
  }
Exemple #20
0
 private void shutdownAndAwaitTermination(int seconds) {
   // disable submission of new tasks
   executorService.shutdown();
   try {
     // wait for existing tasks to terminate
     if (!executorService.awaitTermination(seconds, TimeUnit.SECONDS)) {
       // cancel lingering tasks
       executorService.shutdownNow();
       // wait for lingering tasks to terminate
       if (!executorService.awaitTermination(seconds, TimeUnit.SECONDS)) {
         System.err.println("executorService did not terminate!");
       }
     }
   } catch (InterruptedException ie) {
     // (re-)cancel if current thread also interrupted
     executorService.shutdownNow();
     // preserve interrupt status
     Thread.currentThread().interrupt();
   }
 }
  public static void main(String[] args) throws Exception {
    int maxThreads = 5;
    if (args.length > 0) maxThreads = Integer.parseInt(args[0]);

    print = true;

    for (int i = 2; i <= maxThreads; i += (i + 1) >>> 1) {
      System.out.print("Threads: " + i);
      try {
        new FutureLoop(i).test();
      } catch (BrokenBarrierException bb) {
        // OK; ignore
      } catch (ExecutionException ee) {
        // OK; ignore
      }
      Thread.sleep(TIMEOUT);
    }
    pool.shutdown();
    if (!pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS)) throw new Error();
  }
  public void join() {
    boolean allFinalized;
    synchronized (syncObject) {
      do {
        allFinalized = true;
        for (Node node : nodes) {
          if (!node.isFinished()) {
            System.out.println(
                "Node "
                    + node.name
                    + " is not finished pending:"
                    + node.pendingJobs
                    + " lastBatch:"
                    + node.lastBatch);
            allFinalized = false;
            break;
          } /*else {
                System.out.println("Node " + node.name + " is finished");
            }*/
        }
        if (!allFinalized) {
          try {
            System.out.println("WAIT");
            syncObject.wait();
            System.out.println("NOTIFY");
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      } while (!allFinalized);
    }

    for (Node node : nodes) {
      node.post();
    }

    executorService.shutdown();
  }
 public void testMustNotBeAbleToShutdownGlobalPool() {
   ExecutorService service = AppExecutorUtil.getAppExecutorService();
   try {
     service.shutdown();
     fail();
   } catch (Exception ignored) {
   }
   try {
     service.shutdownNow();
     fail();
   } catch (Exception ignored) {
   }
   try {
     ((ThreadPoolExecutor) service).setThreadFactory(Thread::new);
     fail();
   } catch (Exception ignored) {
   }
   try {
     ((ThreadPoolExecutor) service).setCorePoolSize(0);
     fail();
   } catch (Exception ignored) {
   }
 }
  public static void performTest(final Map<String, Integer> m) throws Exception {

    out.println("Test started for: " + m.getClass());
    long avgTime = 0;
    for (int i = 0; i < 5; i++) {

      long startTime = System.nanoTime();
      ExecutorService exService = Executors.newFixedThreadPool(POOL_SIZE);

      for (int j = 0; j < POOL_SIZE; j++) {
        exService.execute(
            new Runnable() {
              @SuppressWarnings("unused")
              public void run() {
                for (int i = 0; i < 500000; i++) {
                  Integer randomNum = (int) Math.ceil(Math.random() * 550000);
                  // Retrieve value. We are not using it anywhere
                  Integer value = m.get(String.valueOf(randomNum));
                  // Put value
                  m.put(String.valueOf(randomNum), randomNum);
                }
              }
            });
      }

      // Make sure executor stops
      exService.shutdown();
      // Blocks until all tasks have completed execution after a shutdown request
      exService.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);

      long entTime = System.nanoTime();
      long totalTime = (entTime - startTime) / 1000000L;
      avgTime += totalTime;
      out.println("500K entried added/retrieved in " + totalTime + " ms");
    }
    out.println("For " + m.getClass() + " the average time is " + avgTime / 5 + " ms\n");
  }
Exemple #25
0
  static void realMain(String[] args) throws Throwable {
    // jmap doesn't work on Windows
    if (System.getProperty("os.name").startsWith("Windows")) return;

    final String childClassName = Job.class.getName();
    final String classToCheckForLeaks = Job.classToCheckForLeaks();
    final String uniqueID = String.valueOf(new Random().nextInt(Integer.MAX_VALUE));

    final String[] jobCmd = {
      java,
      "-Xmx8m",
      "-classpath",
      System.getProperty("test.classes", "."),
      childClassName,
      uniqueID
    };
    final Process p = new ProcessBuilder(jobCmd).start();

    final String childPid =
        match(
            commandOutputOf(jps, "-m"),
            "(?m)^ *([0-9]+) +\\Q" + childClassName + "\\E *" + uniqueID + "$",
            1);

    final int n0 = objectsInUse(p, childPid, classToCheckForLeaks);
    final int n1 = objectsInUse(p, childPid, classToCheckForLeaks);
    equal(p.waitFor(), 0);
    equal(p.exitValue(), 0);
    failed += p.exitValue();

    // Check that no objects were leaked.
    System.out.printf("%d -> %d%n", n0, n1);
    check(Math.abs(n1 - n0) < 2); // Almost always n0 == n1
    check(n1 < 20);
    drainers.shutdown();
  }
Exemple #26
0
 public static void main(String[] args) throws Exception {
   try {
     lock = new Object();
     if (args.length > 0) {
       NUM = Integer.parseInt(args[0]);
     }
     execs = Executors.newFixedThreadPool(5);
     httpServer = createHttpServer(execs);
     port = httpServer.getAddress().getPort();
     pool = Executors.newFixedThreadPool(10);
     httpServer.start();
     for (int i = 0; i < NUM; i++) {
       pool.execute(new Client());
       if (error) {
         throw new Exception("error in test");
       }
     }
     System.out.println("Main thread waiting");
     pool.shutdown();
     long latest = System.currentTimeMillis() + 200 * 1000;
     while (System.currentTimeMillis() < latest) {
       if (pool.awaitTermination(2000L, TimeUnit.MILLISECONDS)) {
         System.out.println("Main thread done!");
         return;
       }
       if (error) {
         throw new Exception("error in test");
       }
     }
     throw new Exception("error in test: timed out");
   } finally {
     httpServer.stop(0);
     pool.shutdownNow();
     execs.shutdownNow();
   }
 }
 /** Shutdown-related method behaviour when the cluster is running */
 @Test
 public void testShutdownBehaviour() throws Exception {
   ExecutorService executor = createSingleNodeExecutorService("testShutdownBehaviour");
   // Fresh instance, is not shutting down
   assertFalse(executor.isShutdown());
   assertFalse(executor.isTerminated());
   executor.shutdown();
   assertTrue(executor.isShutdown());
   assertTrue(executor.isTerminated());
   // shutdownNow() should return an empty list and be ignored
   List<Runnable> pending = executor.shutdownNow();
   assertTrue(pending.isEmpty());
   assertTrue(executor.isShutdown());
   assertTrue(executor.isTerminated());
   // awaitTermination() should return immediately false
   try {
     boolean terminated = executor.awaitTermination(60L, TimeUnit.SECONDS);
     assertFalse(terminated);
   } catch (InterruptedException ie) {
     fail("InterruptedException");
   }
   assertTrue(executor.isShutdown());
   assertTrue(executor.isTerminated());
 }
  public static void main(String[] args) {
    // TODO main
    OptionParser parser = new OptionParser();
    parser.acceptsAll(Arrays.asList("h", "help"), "Show this help dialog.");
    OptionSpec<String> serverOption =
        parser
            .acceptsAll(Arrays.asList("s", "server"), "Server to join.")
            .withRequiredArg()
            .describedAs("server-address[:port]");
    OptionSpec<String> proxyOption =
        parser
            .acceptsAll(
                Arrays.asList("P", "proxy"),
                "SOCKS proxy to use. Ignored in presence of 'socks-proxy-list'.")
            .withRequiredArg()
            .describedAs("proxy-address");
    OptionSpec<String> ownerOption =
        parser
            .acceptsAll(
                Arrays.asList("o", "owner"), "Owner of the bot (username of in-game control).")
            .withRequiredArg()
            .describedAs("username");
    OptionSpec<?> offlineOption =
        parser.acceptsAll(
            Arrays.asList("O", "offline"),
            "Offline-mode. Ignores 'password' and 'account-list' (will "
                + "generate random usernames if 'username' is not supplied).");
    OptionSpec<?> autoRejoinOption =
        parser.acceptsAll(Arrays.asList("a", "auto-rejoin"), "Auto-rejoin a server on disconnect.");
    OptionSpec<Integer> loginDelayOption =
        parser
            .acceptsAll(
                Arrays.asList("d", "login-delay"),
                "Delay between bot joins, in milliseconds. 5000 is "
                    + "recommended if not using socks proxies.")
            .withRequiredArg()
            .describedAs("delay")
            .ofType(Integer.class);
    OptionSpec<Integer> botAmountOption =
        parser
            .acceptsAll(
                Arrays.asList("b", "bot-amount"),
                "Amount of bots to join. Must be <= amount of accounts.")
            .withRequiredArg()
            .describedAs("amount")
            .ofType(Integer.class);

    OptionSpec<String> accountListOption =
        parser
            .accepts(
                "account-list",
                "File containing a list of accounts, in username/email:password format.")
            .withRequiredArg()
            .describedAs("file");
    OptionSpec<String> socksProxyListOption =
        parser
            .accepts(
                "socks-proxy-list",
                "File containing a list of SOCKS proxies, in address:port format.")
            .withRequiredArg()
            .describedAs("file");
    OptionSpec<String> httpProxyListOption =
        parser
            .accepts(
                "http-proxy-list",
                "File containing a list of HTTP proxies, in address:port format.")
            .withRequiredArg()
            .describedAs("file");

    OptionSet options;
    try {
      options = parser.parse(args);
    } catch (OptionException exception) {
      try {
        parser.printHelpOn(System.out);
      } catch (Exception exception1) {
        exception1.printStackTrace();
      }
      return;
    }

    if (options.has("help")) {
      printHelp(parser);
      return;
    }

    final boolean offline = options.has(offlineOption);
    final boolean autoRejoin = options.has(autoRejoinOption);

    final List<String> accounts;
    if (options.has(accountListOption)) {
      accounts = loadAccounts(options.valueOf(accountListOption));
    } else if (!offline) {
      System.out.println("Option 'accounts' must be supplied in " + "absence of option 'offline'.");
      printHelp(parser);
      return;
    } else accounts = null;

    final String server;
    if (!options.has(serverOption)) {
      System.out.println("Option 'server' required.");
      printHelp(parser);
      return;
    } else server = options.valueOf(serverOption);

    final String owner;
    if (!options.has(ownerOption)) {
      System.out.println("Option 'owner' required.");
      printHelp(parser);
      return;
    } else owner = options.valueOf(ownerOption);

    final List<String> socksProxies;
    if (options.has(socksProxyListOption))
      socksProxies = loadProxies(options.valueOf(socksProxyListOption));
    else socksProxies = null;
    final boolean useProxy = socksProxies != null;

    final List<String> httpProxies;
    if (options.has(httpProxyListOption))
      httpProxies = loadLoginProxies(options.valueOf(httpProxyListOption));
    else if (!offline && accounts != null) {
      System.out.println(
          "Option 'http-proxy-list' required if " + "option 'account-list' is supplied.");
      printHelp(parser);
      return;
    } else httpProxies = null;

    final int loginDelay;
    if (options.has(loginDelayOption)) loginDelay = options.valueOf(loginDelayOption);
    else loginDelay = 0;

    final int botAmount;
    if (!options.has(botAmountOption)) {
      System.out.println("Option 'bot-amount' required.");
      printHelp(parser);
      return;
    } else botAmount = options.valueOf(botAmountOption);

    initGui();
    while (!sessions.get()) {
      synchronized (sessions) {
        try {
          sessions.wait(5000);
        } catch (InterruptedException exception) {
        }
      }
    }

    final Queue<Runnable> lockQueue = new ArrayDeque<Runnable>();

    ExecutorService service = Executors.newFixedThreadPool(botAmount + (loginDelay > 0 ? 1 : 0));
    final Object firstWait = new Object();
    if (loginDelay > 0) {
      service.execute(
          new Runnable() {
            @Override
            public void run() {
              synchronized (firstWait) {
                try {
                  firstWait.wait();
                } catch (InterruptedException exception) {
                }
              }
              while (true) {
                if (die) return;
                while (slotsTaken.get() >= 2) {
                  synchronized (slotsTaken) {
                    try {
                      slotsTaken.wait(500);
                    } catch (InterruptedException exception) {
                    }
                  }
                }
                synchronized (lockQueue) {
                  if (lockQueue.size() > 0) {
                    Runnable thread = lockQueue.poll();
                    synchronized (thread) {
                      thread.notifyAll();
                    }
                    lockQueue.offer(thread);
                  } else continue;
                }
                try {
                  Thread.sleep(loginDelay);
                } catch (InterruptedException exception) {
                }
                while (!sessions.get()) {
                  synchronized (sessions) {
                    try {
                      sessions.wait(5000);
                    } catch (InterruptedException exception) {
                    }
                  }
                }
              }
            }
          });
    }
    final List<String> accountsInUse = new ArrayList<String>();
    for (int i = 0; i < botAmount; i++) {
      final int botNumber = i;
      Runnable runnable =
          new Runnable() {
            @Override
            public void run() {
              if (loginDelay > 0)
                synchronized (lockQueue) {
                  lockQueue.add(this);
                }
              Random random = new Random();

              if (!offline) {
                boolean authenticated = false;
                user:
                while (true) {
                  if (authenticated) {
                    authenticated = false;
                    sessionCount.decrementAndGet();
                  }
                  Session session = null;
                  String loginProxy;
                  String account = accounts.get(random.nextInt(accounts.size()));
                  synchronized (accountsInUse) {
                    if (accountsInUse.size() == accounts.size()) System.exit(0);
                    while (accountsInUse.contains(account))
                      account = accounts.get(random.nextInt(accounts.size()));
                    accountsInUse.add(account);
                  }
                  String[] accountParts = account.split(":");
                  while (true) {
                    while (!sessions.get()) {
                      synchronized (sessions) {
                        try {
                          sessions.wait(5000);
                        } catch (InterruptedException exception) {
                        }
                      }
                    }
                    loginProxy = httpProxies.get(random.nextInt(httpProxies.size()));
                    try {
                      session = Util.retrieveSession(accountParts[0], accountParts[1], loginProxy);
                      // addAccount(session);
                      sessionCount.incrementAndGet();
                      authenticated = true;
                      break;
                    } catch (AuthenticationException exception) {
                      System.err.println("[Bot" + botNumber + "] " + exception);
                      if (!exception.getMessage().startsWith("Exception"))
                        // && !exception.getMessage().equals(
                        // "Too many failed logins"))
                        continue user;
                    }
                  }
                  System.out.println(
                      "["
                          + session.getUsername()
                          + "] Password: "******", Session ID: "
                          + session.getSessionId());
                  while (!joins.get()) {
                    synchronized (joins) {
                      try {
                        joins.wait(5000);
                      } catch (InterruptedException exception) {
                      }
                    }
                  }
                  if (loginDelay > 0) {
                    synchronized (this) {
                      try {
                        synchronized (firstWait) {
                          firstWait.notifyAll();
                        }
                        wait();
                      } catch (InterruptedException exception) {
                      }
                    }
                  }

                  while (true) {
                    String proxy =
                        useProxy ? socksProxies.get(random.nextInt(socksProxies.size())) : null;
                    try {
                      new DarkBotMCSpambot(
                          DARK_BOT,
                          server,
                          session.getUsername(),
                          session.getPassword(),
                          session.getSessionId(),
                          null,
                          proxy,
                          owner);
                      if (die) break user;
                      else if (!autoRejoin) break;
                    } catch (Exception exception) {
                      exception.printStackTrace();
                      System.out.println(
                          "["
                              + session.getUsername()
                              + "] Error connecting: "
                              + exception.getCause().toString());
                    }
                  }
                  System.out.println("[" + session.getUsername() + "] Account failed");
                }
              } else {
                while (true) {
                  String proxy =
                      useProxy ? socksProxies.get(random.nextInt(socksProxies.size())) : null;
                  try {
                    String username = "";
                    if (accounts != null) {
                      username = accounts.get(random.nextInt(accounts.size())).split(":")[0];
                      synchronized (accountsInUse) {
                        while (accountsInUse.contains(username))
                          username = accounts.get(random.nextInt(accounts.size()));
                        accountsInUse.add(username);
                      }
                    } else
                      for (int i = 0; i < 10 + random.nextInt(6); i++)
                        username += alphas[random.nextInt(alphas.length)];
                    if (loginDelay > 0) {
                      synchronized (this) {
                        try {
                          synchronized (firstWait) {
                            firstWait.notifyAll();
                          }
                          wait();
                        } catch (InterruptedException exception) {
                        }
                      }
                    }
                    new DarkBotMCSpambot(DARK_BOT, server, username, "", "", null, proxy, owner);
                    if (die || !autoRejoin) break;
                    else continue;
                  } catch (Exception exception) {
                    System.out.println(
                        "[Bot" + botNumber + "] Error connecting: " + exception.toString());
                  }
                }
              }
            }
          };
      service.execute(runnable);
    }
    service.shutdown();
    while (!service.isTerminated()) {
      try {
        service.awaitTermination(9000, TimeUnit.DAYS);
      } catch (InterruptedException exception) {
        exception.printStackTrace();
      }
    }
    System.exit(0);
  }
Exemple #29
0
  private SpecificationTestCasesListener<T> run(
      final URL[] classpath, final Collection<TestCase> failures, ClassLoader unitTestClassLoader) {
    final SpecificationTestCasesListener<T> listener =
        new SpecificationTestCasesListener<>(runtimeValues);

    // create the classpath for JPF
    String stringClassPath = createClassPath(classpath);

    String mainClass = "nopol.repair.NopolTestRunner";
    // TestExecutorProcessor.createMainTestClass(spoon, mainClass);

    List<TestCase> passedTest = new ArrayList<>(failures.size());
    Iterator<TestCase> iterator = failures.iterator();
    while (iterator.hasNext()) {
      TestCase testCase = iterator.next();
      logger.debug("SYMBOLIC EXECUTION on " + sourceLocation + " Test " + testCase);
      String[] args = new String[1];
      args[0] = testCase.className() + "." + testCase.testName();

      Config conf =
          JPFUtil.createConfig(
              args, mainClass, stringClassPath, outputSourceFile.getAbsolutePath());
      final JPF jpf = new JPF(conf);

      // executes JPF
      JPFListener jpfListener = new JPFListener();
      jpf.addSearchListener(jpfListener);

      ExecutorService executor = Executors.newFixedThreadPool(1);

      Future<?> future =
          executor.submit(
              new Runnable() {
                @Override
                public void run() {
                  jpf.run();
                }
              });

      executor.shutdown();
      try {
        future.get(60, TimeUnit.SECONDS);
      } catch (InterruptedException e) {
        continue;
      } catch (ExecutionException e) {
        e.printStackTrace();
        continue;
      } catch (TimeoutException e) {
        future.cancel(true);
        continue;
      }

      // get the JPF result
      Object result = jpfListener.getResult();
      if (result == null) {
        continue;
      }
      logger.debug(
          "SYMBOLIC VALUE on " + sourceLocation + " for Test " + testCase + " Value: " + result);
      // collect runtime
      boolean passed =
          executeTestAndCollectRuntimeValues(result, testCase, unitTestClassLoader, listener);
      if (passed) {
        this.find = true;
        TestSuiteExecution.runTestCases(failures, unitTestClassLoader, listener);
        if (!passedTest.contains(testCase)) {
          passedTest.add(testCase);
        }
        if (passedTest.size() == failures.size()) {
          break;
        }
      }
    }

    return listener;
  }
  /**
   * Solves a board game using a depth-limited depth first search. Also makes use of some
   * backtracking to further optimise the result. Will also attempt to run multithreadedly.
   *
   * @param s The tile sequence
   * @param b The board to search
   * @return The final best board state that it can find.
   */
  private Board solve_mdfs(Board b) {
    Ringbuffer<Board> rb = new Ringbuffer<>(6);
    Board current = b, choke_best = null;
    char fc = 0, foff = 0;
    // VTEC just kicked in yo
    ExecutorService pool = Executors.newFixedThreadPool(nThreads);

    fbest_score = -1;
    fbest = null;
    while (current != null && !current.finished()) {
      rb.push(current);
      current = solve_pdfs(current, pool);

      if (choke_best != null && choke_best != fbest) {
        log_info(
            "Recovery!: [%d:%s] %d(%d) --> %d(%d)",
            (int) fc,
            Arrays.toString(currentfactors),
            choke_best.score(),
            choke_best.nMoves(),
            fbest.score(),
            fbest.nMoves());
        choke_best = null;
        // Test: Is it always best to stick to 18,2,2,9 where possible?
        // Maybe not, but some factors shouldn't be used for extended periods of time.
        if (fc > 3) {
          log_info(
              "Volatile weights were used; switching back to %s",
              Arrays.toString(choicefactors[0]));
          fc = 0;
          currentfactors = choicefactors[0];
        }
      }

      if (current != null) {
        log_info(current);
      } else if (fbest != null && fbest.nMoves() < tileSequence.length) {
        current = rb.pop();

        if (choke_best != fbest) {
          choke_best = fbest;
          foff = 1;
          fc = (char) ((fc + 1) % choicefactors.length);
          currentfactors = choicefactors[fc];
          log_info("Dead-end, back-tracking two steps and trying with different weights!");
          log_info(
              "Starting index: %d (current score %d/%d)",
              (int) fc, current.score(), current.nMoves());
        } else if (foff++ < choicefactors.length - 1) {
          fc = (char) ((fc + 1) % choicefactors.length);
          currentfactors = choicefactors[fc];
          log_info("No improvement, trying factor index %d...", (int) fc);
        } else {
          current = null;
          log_info("Factor exhaustion");
        }
      }
    }

    pool.shutdown();
    return fbest == null ? b : fbest;
  }