Java Code Examples for java.util.concurrent.ExecutorService#isTerminated()

The following examples show how to use java.util.concurrent.ExecutorService#isTerminated() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MoreExecutors.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Shuts down the given executor service gradually, first disabling new submissions and later, if
 * necessary, cancelling remaining tasks.
 *
 * <p>The method takes the following steps:
 * <ol>
 * <li>calls {@link ExecutorService#shutdown()}, disabling acceptance of new submitted tasks.
 * <li>awaits executor service termination for half of the specified timeout.
 * <li>if the timeout expires, it calls {@link ExecutorService#shutdownNow()}, cancelling pending
 * tasks and interrupting running tasks.
 * <li>awaits executor service termination for the other half of the specified timeout.
 * </ol>
 *
 * <p>If, at any step of the process, the calling thread is interrupted, the method calls
 * {@link ExecutorService#shutdownNow()} and returns.
 *
 * @param service the {@code ExecutorService} to shut down
 * @param timeout the maximum time to wait for the {@code ExecutorService} to terminate
 * @param unit the time unit of the timeout argument
 * @return {@code true} if the {@code ExecutorService} was terminated successfully, {@code false}
 *     if the call timed out or was interrupted
 * @since 17.0
 */

@Beta
@CanIgnoreReturnValue
@GwtIncompatible // concurrency
public static boolean shutdownAndAwaitTermination(ExecutorService service, long timeout, TimeUnit unit) {
  long halfTimeoutNanos = unit.toNanos(timeout) / 2;
  // Disable new tasks from being submitted
  service.shutdown();
  try {
    // Wait for half the duration of the timeout for existing tasks to terminate
    if (!service.awaitTermination(halfTimeoutNanos, TimeUnit.NANOSECONDS)) {
      // Cancel currently executing tasks
      service.shutdownNow();
      // Wait the other half of the timeout for tasks to respond to being cancelled
      service.awaitTermination(halfTimeoutNanos, TimeUnit.NANOSECONDS);
    }
  } catch (InterruptedException ie) {
    // Preserve interrupt status
    Thread.currentThread().interrupt();
    // (Re-)Cancel if current thread also interrupted
    service.shutdownNow();
  }
  return service.isTerminated();
}
 
Example 2
Source File: BlockingCacheTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void testBlockingCache() {
  ExecutorService defaultThreadPool = Executors.newFixedThreadPool(2);

  long init = System.currentTimeMillis();

  for (int i = 0; i < 2; i++) {
    defaultThreadPool.execute(new Runnable() {

      @Override
      public void run() {
        accessDB();
      }
    });
  }

  defaultThreadPool.shutdown();

  while (!defaultThreadPool.isTerminated()) {
  }

  long totalTime = System.currentTimeMillis() - init;
  Assert.assertTrue(totalTime > 1000);
}
 
Example 3
Source File: Chapter07Concurrency03.java    From Java-11-Cookbook-Second-Edition with MIT License 6 votes vote down vote up
private static void shutdownAndCancelTasks(ExecutorService execService, int shutdownDelaySec, List<Future<Result>> futures) {
    try {
        execService.shutdown();
        System.out.println("Waiting for " + shutdownDelaySec + " sec before shutting down service...");
        execService.awaitTermination(shutdownDelaySec, TimeUnit.SECONDS);
    } catch (Exception ex) {
        System.out.println("Caught around execService.awaitTermination(): " + ex.getClass().getName());
    } finally {
        if (!execService.isTerminated()) {
            System.out.println("Terminating remaining running tasks...");
            for (Future<Result> future : futures) {
                if (future.isDone() && !future.isCancelled()) {
                    System.out.println("Cancelling task...");
                    future.cancel(true);
                }
            }
        }
        System.out.println("Calling execService.shutdownNow()...");
        List<Runnable> l = execService.shutdownNow();
        System.out.println(l.size() + " tasks were waiting to be executed. Service stopped.");
    }
}
 
Example 4
Source File: Chapter07Concurrency03.java    From Java-11-Cookbook-Second-Edition with MIT License 6 votes vote down vote up
private static void demo1_Executor1() {
    System.out.println();
    int shutdownDelaySec = 1;
    ExecutorService execService = Executors.newSingleThreadExecutor();
    Runnable runnable = () -> System.out.println("Worker One did the job.");
    execService.execute(runnable);
    runnable = () -> System.out.println("Worker Two did the job.");
    Future future = execService.submit(runnable);
    try {
        execService.shutdown();
        execService.awaitTermination(shutdownDelaySec, TimeUnit.SECONDS);
    } catch (Exception ex) {
        System.out.println("Caught around execService.awaitTermination(): " + ex.getClass().getName());
    } finally {
        if (!execService.isTerminated()) {
            if (future != null && !future.isDone() && !future.isCancelled()) {
                System.out.println("Cancelling the task...");
                future.cancel(true);
            }
        }
        List<Runnable> l = execService.shutdownNow();
        System.out.println(l.size() + " tasks were waiting to be executed. Service stopped.");
    }
}
 
Example 5
Source File: InternalResourceManager.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
void stopExecutor(ExecutorService executor) {
  if (executor == null) {
    return;
  }
  executor.shutdown();
  final int secToWait = Integer.getInteger("gemfire.prrecovery-close-timeout", 120).intValue();
  try {
    executor.awaitTermination(secToWait, TimeUnit.SECONDS);
  }
  catch (InterruptedException x) {
    Thread.currentThread().interrupt();
    this.cache.getLoggerI18n().fine(
    "Failed in interrupting the Resource Manager Thread due to interrupt");
  }
  if (! executor.isTerminated()) {
      this.cache.getLoggerI18n().warning(LocalizedStrings.
          ResourceManager_FAILED_TO_STOP_RESOURCE_MANAGER_THREADS,
          new Object[]{secToWait});
  }
}
 
Example 6
Source File: SemaphoreTest.java    From jdk-source-analysis with Apache License 2.0 5 votes vote down vote up
@Test
public void test() {
    ExecutorService executorService = Executors.newFixedThreadPool(20);
    Semaphore semaphore = new Semaphore(5);
    for (int i = 0; i < 20; i++) {
        executorService.execute(new Task(semaphore));
    }
    executorService.shutdown();
    while (!executorService.isTerminated()) {
    }
    System.out.println("Ok...");
}
 
Example 7
Source File: ProgMainLambdaThreadExecServ03.java    From javase with MIT License 5 votes vote down vote up
public static void main(String[] args) {

		ExecutorService executor = Executors.newSingleThreadExecutor();
		executor.submit(() -> {
		    String threadName = Thread.currentThread().getName();
		    System.out.println("Hello " + threadName);
		});

		// => Hello pool-1-thread-1

		try {
		    System.out.println("attempt to shutdown executor");
		    executor.shutdown();
		    executor.awaitTermination(5, TimeUnit.SECONDS);
		}
		catch (InterruptedException e) {
		    System.err.println("tasks interrupted");
		}
		finally {
		    if (!executor.isTerminated()) {
			System.err.println("cancel non-finished tasks");
		    }
		    executor.shutdownNow();
		    System.out.println("shutdown finished");
		}

		System.out.println("Done!");

	}
 
Example 8
Source File: ConcurrentUtils.java    From javacore with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
public static void stop(ExecutorService executor) {
    try {
        executor.shutdown();
        executor.awaitTermination(60, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        System.err.println("termination interrupted");
    } finally {
        if (!executor.isTerminated()) {
            System.err.println("killing non-finished tasks");
        }
        executor.shutdownNow();
    }
}
 
Example 9
Source File: Simulation.java    From mars-sim with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Start the simulation instance.
 */
public void startSimThread(boolean useDefaultName) {
	// Start the simulation.
	ExecutorService e = getSimExecutor();
	if (e == null || (e != null && (e.isTerminated() || e.isShutdown())))
		startSimExecutor();
	e.submit(new StartTask(useDefaultName));
}
 
Example 10
Source File: Main.java    From SortingAlgorithmAnimations with MIT License 5 votes vote down vote up
public void beginAnimation(String animationName, int[] list) {
	try {
		
		this.animationName = animationName;
		repaint();
		Thread.sleep(2000);
		this.animationName = "";
		repaint();
		for (int i = 0; i < sortPanels.length; i++) {
			sortPanels[i].setList(list);
			sortPanels[i].setVisible(true);
		}
		Thread.sleep(1000);
		ExecutorService executor = Executors.newFixedThreadPool(sortPanels.length);
		for (int i = 0; i < sortPanels.length; i++) {
			executor.execute(sortPanels[i]);
		}
		executor.shutdown();
		while(!executor.isTerminated()) {
			Thread.sleep(100);
		}
		Thread.sleep(1000);
		for (int i = 0; i < sortPanels.length; i++) {
			sortPanels[i].setVisible(false);
		}
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
}
 
Example 11
Source File: CASFileCache.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
private void joinThreads(ExecutorService pool, String message, long timeout, TimeUnit unit)
    throws InterruptedException {
  pool.shutdown();
  while (!pool.isTerminated()) {
    logger.log(Level.INFO, message);
    pool.awaitTermination(timeout, unit);
  }
}
 
Example 12
Source File: IPv6InMemoryAuthTicketsHelperConcurrencyTest.java    From p4ic4idea with Apache License 2.0 4 votes vote down vote up
/**
 * Test in-memory tickets - saving auth tickets
 */
@Test
public void testInMemorySaveAuthTicketsConcurrently() {
	String address = "[fc01:5034:a05:1e:ad94:403f:ae19:1aa9]:1666";
	String value = "ABCDEF123123";
	String user = "bruno";

	String ticketsFilePath = null;

	try {
		// Create the first ticket
		AuthTicketsHelper.saveTicket(user, address, value, ticketsFilePath);

		// Run concurrent reads and writes
		ExecutorService executor = Executors.newFixedThreadPool(10);
		for (int i = 0; i < 25; i++) {
			String addr = address + i;
			String val = value + i;
			String usr = user + i;

			Runnable task = null;

			if ((i % 2) == 0) {
				task = new AuthTicketsWriter(usr, addr, val,
						ticketsFilePath);
			} else {
				task = new AuthTicketsReader(ticketsFilePath);
			}

			executor.execute(task);
		}

		executor.shutdown();

		while (!executor.isTerminated()) {
			System.out.println("Threads are still running...");
			Thread.sleep(2000);
		}

		System.out.println("Finished all threads");

		// Check the size of the in-memory map - 501
		AuthTicket[] tickets = AuthTicketsHelper.getTickets(ticketsFilePath);
		assertNotNull(tickets);
		assertTrue(tickets.length > 10);
		
	} catch (Exception exc) {
		fail("Unexpected exception: " + exc.getLocalizedMessage());
	}
}
 
Example 13
Source File: AuthTicketsHelperConcurrencyTest.java    From p4ic4idea with Apache License 2.0 4 votes vote down vote up
/**
 * Test saving auth tickets
 */
@Test
public void testSaveAuthTicketsConcurrently() throws Exception {
  int randNo = getRandomInt();

  String address = "server:1666";
  String value = "ABCDEF123123";
  String user = "bruno";

  String ticketsFilePath = System.getProperty("user.dir");
  assertThat(ticketsFilePath, notNullValue());
  ticketsFilePath += File.separator + "realticketsfile" + randNo;

  try {
    // Create the first tickets file
    AuthTicketsHelper.saveTicket(user, address, value, ticketsFilePath);

    // Run concurrent reads and writes
    ExecutorService executor = Executors.newFixedThreadPool(10);
    for (int i = 0; i < 25; i++) {
      String addr = address + i;
      String val = value + i;
      String usr = user + i;

      Runnable task;

      if ((i % 2) == 0) {
        task = new AuthTicketsWriter(
            usr,
            addr,
            val,
            ticketsFilePath);
      } else {
        task = new AuthTicketsReader(ticketsFilePath);
      }

      executor.execute(task);
    }

    executor.shutdown();

    while (!executor.isTerminated()) {
      System.out.println("Threads are still running...");
      Thread.sleep(2000);
    }

    System.out.println("Finished all threads");

  } finally {
    File ticketsFile = new File(ticketsFilePath);
    boolean deleted = ticketsFile.delete();
    assertThat(deleted, is(true));
  }
}
 
Example 14
Source File: ConcurrentAuthTicketsTest.java    From p4ic4idea with Apache License 2.0 4 votes vote down vote up
/**
 * Test save auth tickets with multiple threads.
 */
@Test
public void testSaveAuthTicketsConcurrently() throws Exception {
    String address = "server:1666";
    String value = "ABCDEF1231234";
    String user = "bruno2";

    String ticketsFilePath = System.getProperty("user.dir");
    assertThat(ticketsFilePath, notNullValue());
    ticketsFilePath += File.separator + "realticketsfile4" + System.currentTimeMillis();

    List<AuthTicketsWriter> ticketsWriters = new ArrayList<AuthTicketsWriter>();
    try {
        // Run concurrent reads and writes
        int x=0;
        ExecutorService executor = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 20; i++) {
            String addr = address + i;
            String val = value + i;
            String usr = user + i;
            
            Runnable task;
            if ((i % 2) == 0) {
                task = new AuthTicketsWriter(usr, addr, val, ticketsFilePath);
                ticketsWriters.add((AuthTicketsWriter) task);
            } else {
                task = new AuthTicketsReader(ticketsFilePath);
            }
            
            executor.execute(task);
        }

        executor.shutdown();

        while (!executor.isTerminated()) {
            // System.out.println("Threads are still running..." +
            // executor.toString());
        }

        System.out.println("Finished all threads");

        // Check the number of tickets in the file
        AuthTicket[] tickets = AuthTicketsHelper.getTickets(ticketsFilePath);
        assertThat(tickets, notNullValue());

        // If some thread after try no more than 'max lockTry', it will give
        // up
        int numTickets = 0;
        for(AuthTicketsWriter w : ticketsWriters) {
            numTickets += w.getTotalSuccessSaveTicket();
        }
        assertThat(tickets.length, is(numTickets));
    } finally {
        filesHelper.setWritable(ticketsFilePath, true);
        boolean deleted = Files.deleteIfExists(Paths.get(ticketsFilePath + ".lck"));
        assertThat(deleted, is(false));
        deleted = Files.deleteIfExists(Paths.get(ticketsFilePath));
        assertThat(deleted, is(true));
    }
}
 
Example 15
Source File: IPv6InMemoryFingerprintsHelperConcurrencyTest.java    From p4ic4idea with Apache License 2.0 4 votes vote down vote up
/**
 * Test saving fingerprints
 */
@Test
public void testSaveFingerprintsConcurrently() {
	String address = "[fc01:5034:a05:1e:ad94:403f:ae19:1aa9]:1666";
	String value = "ABCDEF123123";
	String user = "**++**";

	String fingerprintsFilePath = null;

	try {
		// Create the first fingerprints file
		FingerprintsHelper.saveFingerprint(user, address, value,
				fingerprintsFilePath);

		// Run concurrent reads and writes
		ExecutorService executor = Executors.newFixedThreadPool(10);
		for (int i = 0; i < 25; i++) {
			String addr = address + i;
			String val = value + i;
			String usr = user + i;

			Runnable task = null;

			if ((i % 2) == 0) {
				task = new FingerprintsWriter(usr, addr, val,
						fingerprintsFilePath);
			} else {
				task = new FingerprintsReader(fingerprintsFilePath);
			}

			executor.execute(task);
		}

		executor.shutdown();

		while (!executor.isTerminated()) {
			System.out.println("Threads are still running...");
			Thread.sleep(2000);
		}

		System.out.println("Finished all threads");

	} catch (Exception exc) {
		fail("Unexpected exception: " + exc.getLocalizedMessage());
	}
}
 
Example 16
Source File: ConcurrentAuthTicketsTest.java    From p4ic4idea with Apache License 2.0 4 votes vote down vote up
/**
 * Test save auth tickets with multiple threads.
 */
@Test
public void testSaveAuthTicketsConcurrently() throws Exception {
    String address = "server:1666";
    String value = "ABCDEF1231234";
    String user = "bruno2";

    String ticketsFilePath = System.getProperty("user.dir");
    assertThat(ticketsFilePath, notNullValue());
    ticketsFilePath += File.separator + "realticketsfile4" + System.currentTimeMillis();

    List<AuthTicketsWriter> ticketsWriters = newArrayList();
    try {
        // Run concurrent reads and writes
        int x=0;
        ExecutorService executor = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 20; i++) {
            String addr = address + i;
            String val = value + i;
            String usr = user + i;
            
            Runnable task;
            if ((i % 2) == 0) {
                task = new AuthTicketsWriter(usr, addr, val, ticketsFilePath);
                ticketsWriters.add((AuthTicketsWriter) task);
            } else {
                task = new AuthTicketsReader(ticketsFilePath);
            }
            
            executor.execute(task);
        }

        executor.shutdown();

        while (!executor.isTerminated()) {
            // System.out.println("Threads are still running..." +
            // executor.toString());
        }

        System.out.println("Finished all threads");

        // Check the number of tickets in the file
        AuthTicket[] tickets = AuthTicketsHelper.getTickets(ticketsFilePath);
        assertThat(tickets, notNullValue());

        // If some thread after try no more than 'max lockTry', it will give
        // up
        int numTickets = ticketsWriters.stream()
                .mapToInt(AuthTicketsWriter::getTotalSuccessSaveTicket).sum();
        assertThat(tickets.length, is(numTickets));
    } finally {
        filesHelper.setWritable(ticketsFilePath, true);
        boolean deleted = Files.deleteIfExists(Paths.get(ticketsFilePath + ".lck"));
        assertThat(deleted, is(false));
        deleted = Files.deleteIfExists(Paths.get(ticketsFilePath));
        assertThat(deleted, is(true));
    }
}
 
Example 17
Source File: AuthTicketsHelperConcurrencyTest.java    From p4ic4idea with Apache License 2.0 4 votes vote down vote up
/**
 * Test saving auth tickets
 */
@Test
public void testSaveAuthTicketsConcurrently() throws Exception {
  int randNo = getRandomInt();

  String address = "server:1666";
  String value = "ABCDEF123123";
  String user = "bruno";

  String ticketsFilePath = System.getProperty("user.dir");
  assertThat(ticketsFilePath, notNullValue());
  ticketsFilePath += File.separator + "realticketsfile" + randNo;

  try {
    // Create the first tickets file
    AuthTicketsHelper.saveTicket(user, address, value, ticketsFilePath);

    // Run concurrent reads and writes
    ExecutorService executor = Executors.newFixedThreadPool(10);
    for (int i = 0; i < 25; i++) {
      String addr = address + i;
      String val = value + i;
      String usr = user + i;

      Runnable task;

      if ((i % 2) == 0) {
        task = new AuthTicketsWriter(
            usr,
            addr,
            val,
            ticketsFilePath);
      } else {
        task = new AuthTicketsReader(ticketsFilePath);
      }

      executor.execute(task);
    }

    executor.shutdown();

    while (!executor.isTerminated()) {
      System.out.println("Threads are still running...");
      Thread.sleep(2000);
    }

    System.out.println("Finished all threads");

  } finally {
    File ticketsFile = new File(ticketsFilePath);
    boolean deleted = ticketsFile.delete();
    assertThat(deleted, is(true));
  }
}
 
Example 18
Source File: InMemoryAuthTicketsHelperConcurrencyTest.java    From p4ic4idea with Apache License 2.0 4 votes vote down vote up
/**
 * Test in-memory tickets - saving auth tickets
 */
@Test
public void testInMemorySaveAuthTicketsConcurrently() {
	String address = "server:1666";
	String value = "ABCDEF123123";
	String user = "bruno";

	String ticketsFilePath = null;

	try {
		// Create the first ticket
		AuthTicketsHelper.saveTicket(user, address, value, ticketsFilePath);

		// Run concurrent reads and writes
		ExecutorService executor = Executors.newFixedThreadPool(10);
		for (int i = 0; i < 25; i++) {
			String addr = address + i;
			String val = value + i;
			String usr = user + i;

			Runnable task = null;

			if ((i % 2) == 0) {
				task = new AuthTicketsWriter(usr, addr, val,
						ticketsFilePath);
			} else {
				task = new AuthTicketsReader(ticketsFilePath);
			}

			executor.execute(task);
		}

		executor.shutdown();

		while (!executor.isTerminated()) {
			System.out.println("Threads are still running...");
			Thread.sleep(2000);
		}

		System.out.println("Finished all threads");

		// Check the size of the in-memory map - 501
		AuthTicket[] tickets = AuthTicketsHelper.getTickets(ticketsFilePath);
		assertNotNull(tickets);
		assertTrue(tickets.length > 10);
		
	} catch (Exception exc) {
		fail("Unexpected exception: " + exc.getLocalizedMessage());
	}
}
 
Example 19
Source File: CustomMatrixZoomData.java    From Juicebox with MIT License 4 votes vote down vote up
private List<Block> addNormalizedBlocksToListByGenomeCoordinates(int gx1, int gy1, int gx2, int gy2,
                                                                 final NormalizationType no) {
    List<Block> blockList = Collections.synchronizedList(new ArrayList<Block>());
    Map<MatrixZoomData, Map<RegionPair, List<Integer>>> blocksNumsToLoadForZd = new ConcurrentHashMap<>();
    // remember these are pseudo genome coordinates

    // x window
    //net.sf.jsi.Rectangle currentWindow = new net.sf.jsi.Rectangle(gx1, gx1, gx2, gx2);
    List<Pair<MotifAnchor, MotifAnchor>> xAxisRegions = rTreeHandler.getIntersectingFeatures(chr1.getName(), gx1, gx2);

    // y window
    //currentWindow = new net.sf.jsi.Rectangle(gy1, gy1, gy2, gy2);
    List<Pair<MotifAnchor, MotifAnchor>> yAxisRegions = rTreeHandler.getIntersectingFeatures(chr2.getName(), gy1, gy2);

    if (isImportant) {
        if (HiCGlobals.printVerboseComments)
            System.out.println("num x regions " + xAxisRegions.size() + " num y regions " + yAxisRegions.size());
    }

    if (xAxisRegions.size() < 1) {
        System.err.println("no x?");
    }
    if (yAxisRegions.size() < 1) {
        System.err.println("no y?");
    }

    ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    // todo change to be by chromosome?
    for (Pair<MotifAnchor, MotifAnchor> xRegion : xAxisRegions) {
        for (Pair<MotifAnchor, MotifAnchor> yRegion : yAxisRegions) {
            Runnable worker = new Runnable() {
                @Override
                public void run() {
                    RegionPair rp = RegionPair.generateRegionPair(xRegion, yRegion, handler);
                    MatrixZoomData zd = zoomDatasForDifferentRegions.get(Matrix.generateKey(rp.xI, rp.yI));
                    if (zd == null || rp == null) return;

                    synchronized (blocksNumsToLoadForZd) {
                        if (!blocksNumsToLoadForZd.containsKey(zd)) {
                            blocksNumsToLoadForZd.put(zd, new HashMap<RegionPair, List<Integer>>());
                        }

                        if (!blocksNumsToLoadForZd.get(zd).containsKey(rp)) {
                            blocksNumsToLoadForZd.get(zd).put(rp, new ArrayList<Integer>());
                        }
                    }

                    List<Integer> tempBlockNumbers = zd.getBlockNumbersForRegionFromGenomePosition(rp.getOriginalGenomeRegion());
                    synchronized (blocksNumsToLoadForZd) {
                        for (int blockNumber : tempBlockNumbers) {
                            String key = zd.getBlockKey(blockNumber, no);
                            if (HiCGlobals.useCache
                                    && allBlockCaches.containsKey(zd)
                                    && allBlockCaches.get(zd).containsKey(rp)
                                    && allBlockCaches.get(zd).get(rp).containsKey(key)) {
                                synchronized (blockList) {
                                    blockList.add(allBlockCaches.get(zd).get(rp).get(key));
                                }
                            } else if (blocksNumsToLoadForZd.containsKey(zd) && blocksNumsToLoadForZd.get(zd).containsKey(rp)) {
                                blocksNumsToLoadForZd.get(zd).get(rp).add(blockNumber);
                            } else {
                                System.err.println("Something went wrong CZDErr3 " + zd.getDescription() +
                                        " rp " + rp.getDescription() + " block num " + blockNumber);
                            }
                        }
                    }
                }
            };
            executor.execute(worker);
        }
    }
    executor.shutdown();

    // Wait until all threads finish
    while (!executor.isTerminated()) {
    }

    // Actually load new blocks
    actuallyLoadGivenBlocks(blockList, no, blocksNumsToLoadForZd);
    //System.out.println("num blocks post "+blockList.size());

    if (blockList.size() < 1) {
        if (HiCGlobals.printVerboseComments)
            System.err.println("no blocks?? for num x regions " + xAxisRegions.size() + " num y regions " + yAxisRegions.size());
    }

    return blockList;
}
 
Example 20
Source File: PRQueryProcessor.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
private void executeWithThreadPool(Collection<Collection> resultCollector)
  throws QueryException, InterruptedException, ForceReattemptException {
  if (Thread.interrupted()) throw new InterruptedException();
    
  java.util.List callableTasks = buildCallableTaskList(resultCollector);
  ExecutorService execService = PRQueryExecutor.getExecutorService();

  boolean reattemptNeeded = false;
  ForceReattemptException fre = null;
  
  if (callableTasks != null && !callableTasks.isEmpty()) {
    List futures = null;
    try {
      futures = execService.invokeAll(callableTasks, 300, TimeUnit.SECONDS);
    }
    catch (RejectedExecutionException rejectedExecutionEx) {
      //this._prds.partitionedRegion.checkReadiness();
      throw rejectedExecutionEx;
    }
    
    if (futures != null) {
      Iterator itr = futures.iterator();
      while (itr.hasNext() && !execService.isShutdown() && !execService.isTerminated()) {
        //this._prds.partitionedRegion.checkReadiness();
        Future fut = (Future)itr.next();
        QueryTask.BucketQueryResult bqr = null;
        
        try {
          bqr = (QueryTask.BucketQueryResult)fut.get(BUCKET_QUERY_TIMEOUT, TimeUnit.SECONDS);
          //if (retry.booleanValue()) {
          //  reattemptNeeded = true;
            //fre = (ForceReattemptException)bqr.getException();
          //} else {
            bqr.handleAndThrowException(); // handles an exception if there was one,
            //  otherwise, the results have already been added to the resultQueue
          //}
          if (bqr.retry) {
            reattemptNeeded = true;
          }
          
        } catch (TimeoutException e) {
          throw new InternalGemFireException(LocalizedStrings.PRQueryProcessor_TIMED_OUT_WHILE_EXECUTING_QUERY_TIME_EXCEEDED_0.toLocalizedString(
              Integer.valueOf(BUCKET_QUERY_TIMEOUT)), e);
        } catch (ExecutionException ee) {
          Throwable cause = ee.getCause();
          if (cause instanceof QueryException) {
            throw (QueryException)cause;
          }
          else {
            throw new InternalGemFireException(LocalizedStrings.PRQueryProcessor_GOT_UNEXPECTED_EXCEPTION_WHILE_EXECUTING_QUERY_ON_PARTITIONED_REGION_BUCKET.toLocalizedString(), 
            cause);
          }
        }
      }
    }
  }
  
  if (execService == null || execService.isShutdown()
      || execService.isTerminated()) {
    this._prds.partitionedRegion.checkReadiness();
  }
  
  if (reattemptNeeded) {
    throw fre;
  }
  
}