List of usage examples for java.util.concurrent FutureTask isDone
public boolean isDone()
From source file:ubic.gemma.core.loader.util.fetcher.AbstractFetcher.java
/** * @param future future task// www . j ava2s. co m * @param expectedSize expected size * @param outputFile output file * @return true if it finished normally, false if it was cancelled. */ protected boolean waitForDownload(FutureTask<Boolean> future, long expectedSize, File outputFile) { int i = 0; long previousSize = 0; StopWatch idleTimer = new StopWatch(); while (!future.isDone() && !future.isCancelled()) { try { Thread.sleep(AbstractFetcher.INFO_UPDATE_INTERVAL); } catch (InterruptedException ie) { if (AbstractFetcher.log != null) { AbstractFetcher.log.info("Cancelling download"); } boolean cancelled = future.cancel(true); if (cancelled) { return false; } // double check... if (future.isCancelled() || future.isDone()) { return false; } if (AbstractFetcher.log != null) { AbstractFetcher.log.error( "Cancellation of actual download might not have happened? Task says it was not cancelled: " + future); } return false; } /* * Avoid logging too much. If we're waiting for a long download, reduce frequency of updates. */ if (outputFile.length() < expectedSize && (i < AbstractFetcher.NUMBER_OF_TIMES_TO_LOG_WAITING_BEFORE_REDUCING_VERBOSITY || i % AbstractFetcher.NUMBER_OF_TIMES_TO_LOG_WAITING_BEFORE_REDUCING_VERBOSITY == 0)) { double percent = 100.00 * outputFile.length() / expectedSize; // can cause npe error, breaking hot deploy if (AbstractFetcher.log != null && AbstractFetcher.log.isInfoEnabled()) { AbstractFetcher.log.info((outputFile.length() + (expectedSize > 0 ? "/" + expectedSize : "") + " bytes read (" + String.format("%.1f", percent) + "%)")); } if (previousSize == outputFile.length()) { /* * Possibly consider bailing after a while. */ if (idleTimer.getTime() > AbstractFetcher.STALLED_BAIL_TIME_LIMIT) { if (AbstractFetcher.log != null) { AbstractFetcher.log.warn("Download does not seem to be happening, bailing"); } return false; } if (idleTimer.getTime() == 0) idleTimer.start(); } else { idleTimer.reset(); idleTimer.start(); } } // if ( outputFile.length() >= expectedSize ) { // // no special action, it will finish soon enough. // } previousSize = outputFile.length(); i++; } if (i == 0) if (AbstractFetcher.log != null) { AbstractFetcher.log.info("File with size " + outputFile.length() + " bytes."); } return true; }
From source file:android.support.test.espresso.base.ThreadPoolExecutorExtractor.java
private <T> FutureTask<T> runOnMainThread(final FutureTask<T> futureToRun) { if (Looper.myLooper() != Looper.getMainLooper()) { final CountDownLatch latch = new CountDownLatch(1); mainHandler.post(new Runnable() { @Override/*from ww w. ja va 2 s. c o m*/ public void run() { try { futureToRun.run(); } finally { latch.countDown(); } } }); try { latch.await(); } catch (InterruptedException ie) { if (!futureToRun.isDone()) { throw new RuntimeException("Interrupted while waiting for task to complete."); } } } else { futureToRun.run(); } return futureToRun; }
From source file:com.datatorrent.stram.StramRecoveryTest.java
/** * Test serialization of the container manager with mock execution layer. * @throws Exception//from ww w . j ava 2 s .c o m */ private void testContainerManager(StorageAgent agent) throws Exception { dag.setAttribute(OperatorContext.STORAGE_AGENT, agent); StatsListeningOperator o1 = dag.addOperator("o1", StatsListeningOperator.class); FSRecoveryHandler recoveryHandler = new FSRecoveryHandler(dag.assertAppPath(), new Configuration(false)); StreamingContainerManager scm = StreamingContainerManager.getInstance(recoveryHandler, dag, false); File expFile = new File(recoveryHandler.getDir(), FSRecoveryHandler.FILE_SNAPSHOT); Assert.assertTrue("snapshot file " + expFile, expFile.exists()); PhysicalPlan plan = scm.getPhysicalPlan(); assertEquals("number required containers", 1, plan.getContainers().size()); PTOperator o1p1 = plan.getOperators(dag.getMeta(o1)).get(0); @SuppressWarnings("UnusedAssignment") /* sneaky: the constructor does some changes to the container */ MockContainer mc = new MockContainer(scm, o1p1.getContainer()); PTContainer originalContainer = o1p1.getContainer(); Assert.assertNotNull(o1p1.getContainer().bufferServerAddress); assertEquals(PTContainer.State.ACTIVE, o1p1.getContainer().getState()); assertEquals("state " + o1p1, PTOperator.State.PENDING_DEPLOY, o1p1.getState()); // test restore initial snapshot + log dag = StramTestSupport.createDAG(testMeta); scm = StreamingContainerManager .getInstance(new FSRecoveryHandler(dag.assertAppPath(), new Configuration(false)), dag, false); dag = scm.getLogicalPlan(); plan = scm.getPhysicalPlan(); o1p1 = plan.getOperators(dag.getOperatorMeta("o1")).get(0); assertEquals("post restore state " + o1p1, PTOperator.State.PENDING_DEPLOY, o1p1.getState()); o1 = (StatsListeningOperator) o1p1.getOperatorMeta().getOperator(); assertEquals("containerId", originalContainer.getExternalId(), o1p1.getContainer().getExternalId()); assertEquals("stats listener", 1, o1p1.statsListeners.size()); assertEquals("number stats calls", 0, o1.processStatsCnt); // stats are not logged assertEquals("post restore 1", PTContainer.State.ALLOCATED, o1p1.getContainer().getState()); assertEquals("post restore 1", originalContainer.bufferServerAddress, o1p1.getContainer().bufferServerAddress); StreamingContainerAgent sca = scm.getContainerAgent(originalContainer.getExternalId()); Assert.assertNotNull("allocated container restored " + originalContainer, sca); assertEquals("memory usage allocated container", (int) OperatorContext.MEMORY_MB.defaultValue, sca.container.getAllocatedMemoryMB()); // YARN-1490 - simulate container terminated on AM recovery scm.scheduleContainerRestart(originalContainer.getExternalId()); assertEquals("memory usage of failed container", 0, sca.container.getAllocatedMemoryMB()); Checkpoint firstCheckpoint = new Checkpoint(3, 0, 0); mc = new MockContainer(scm, o1p1.getContainer()); checkpoint(scm, o1p1, firstCheckpoint); mc.stats(o1p1.getId()).deployState(OperatorHeartbeat.DeployState.ACTIVE).currentWindowId(3) .checkpointWindowId(3); mc.sendHeartbeat(); assertEquals("state " + o1p1, PTOperator.State.ACTIVE, o1p1.getState()); // logical plan modification triggers snapshot CreateOperatorRequest cor = new CreateOperatorRequest(); cor.setOperatorFQCN(GenericTestOperator.class.getName()); cor.setOperatorName("o2"); CreateStreamRequest csr = new CreateStreamRequest(); csr.setSourceOperatorName("o1"); csr.setSourceOperatorPortName("outport"); csr.setSinkOperatorName("o2"); csr.setSinkOperatorPortName("inport1"); FutureTask<?> lpmf = scm.logicalPlanModification(Lists.newArrayList(cor, csr)); while (!lpmf.isDone()) { scm.monitorHeartbeat(); } Assert.assertNull(lpmf.get()); // unmask exception, if any Assert.assertSame("dag references", dag, scm.getLogicalPlan()); assertEquals("number operators after plan modification", 2, dag.getAllOperators().size()); // set operator state triggers journal write o1p1.setState(PTOperator.State.INACTIVE); Checkpoint offlineCheckpoint = new Checkpoint(10, 0, 0); // write checkpoint while AM is out, // it needs to be picked up as part of restore checkpoint(scm, o1p1, offlineCheckpoint); // test restore dag = StramTestSupport.createDAG(testMeta); scm = StreamingContainerManager .getInstance(new FSRecoveryHandler(dag.assertAppPath(), new Configuration(false)), dag, false); Assert.assertNotSame("dag references", dag, scm.getLogicalPlan()); assertEquals("number operators after restore", 2, scm.getLogicalPlan().getAllOperators().size()); dag = scm.getLogicalPlan(); plan = scm.getPhysicalPlan(); o1p1 = plan.getOperators(dag.getOperatorMeta("o1")).get(0); assertEquals("post restore state " + o1p1, PTOperator.State.INACTIVE, o1p1.getState()); o1 = (StatsListeningOperator) o1p1.getOperatorMeta().getOperator(); assertEquals("stats listener", 1, o1p1.statsListeners.size()); assertEquals("number stats calls post restore", 1, o1.processStatsCnt); assertEquals("post restore 1", PTContainer.State.ACTIVE, o1p1.getContainer().getState()); assertEquals("post restore 1", originalContainer.bufferServerAddress, o1p1.getContainer().bufferServerAddress); // offline checkpoint detection assertEquals("checkpoints after recovery", Lists.newArrayList(firstCheckpoint, offlineCheckpoint), o1p1.checkpoints); }
From source file:ubic.basecode.math.linalg.SingularValueDecomposition.java
/** * @param dm/*from w ww . j a v a2s . c o m*/ */ private void computeSVD(final DoubleMatrix2D dm) { /* * This fails to converge some times, we have to bail. */ FutureTask<cern.colt.matrix.linalg.SingularValueDecomposition> svdFuture = new FutureTask<>( new Callable<cern.colt.matrix.linalg.SingularValueDecomposition>() { @Override public cern.colt.matrix.linalg.SingularValueDecomposition call() { return new cern.colt.matrix.linalg.SingularValueDecomposition(dm); } }); StopWatch timer = new StopWatch(); timer.start(); Executors.newSingleThreadExecutor().execute(svdFuture); while (!svdFuture.isDone() && !svdFuture.isCancelled()) { try { Thread.sleep(100); } catch (InterruptedException ie) { throw new RuntimeException("SVD cancelled"); } if (timer.getTime() > MAX_COMPUTE_TIME) { svdFuture.cancel(true); throw new RuntimeException("SVD failed to converge within " + MAX_COMPUTE_TIME + "ms, bailing"); } } timer.stop(); try { this.svd = svdFuture.get(); } catch (InterruptedException e) { throw new RuntimeException(e); } catch (ExecutionException e) { throw new RuntimeException(e); } assert this.svd != null; }
From source file:com.test.test.MultipleRequestsForServerTest.java
@Test public void testMultiClientsForServer() throws Exception { // Number of threads final int size = 30; LOG.debug("clientSimple1:" + clientSimple); List<IServiceSimple> serviceSimpleList = new ArrayList<IServiceSimple>(); for (int i = 0; i < size; i++) { IServiceSimple proxyService = clientSimple.getProxy(IServiceSimple.class); LOG.debug("proxyService:" + proxyService); serviceSimpleList.add(proxyService); }/* w w w .j a v a 2s .c om*/ List<ClientCallableForServer> clientCallableList = new ArrayList<ClientCallableForServer>(); for (int i = 0; i < size; i++) { clientCallableList.add(new ClientCallableForServer(serviceSimpleList.get(i), i)); } List<FutureTask<String>> futureTaskList = new ArrayList<FutureTask<String>>(); for (ClientCallableForServer clientCallable : clientCallableList) { futureTaskList.add(new FutureTask<String>(clientCallable)); } long beginTime = System.currentTimeMillis(); ExecutorService executor = Executors.newFixedThreadPool(futureTaskList.size()); for (FutureTask<String> futureTask : futureTaskList) { executor.execute(futureTask); } boolean ready = false; int[] dones = new int[futureTaskList.size()]; String[] writes = new String[futureTaskList.size()]; int indexValue = 0; while (!ready) { int count = 0; indexValue = 0; for (FutureTask<String> futureTask : futureTaskList) { if (futureTask.isDone() & dones[indexValue] == 0) { writes[indexValue] = futureTask.get(); dones[indexValue] = 1; } indexValue++; } for (int k = 0; k < dones.length; k++) { if (dones[k] == 1) { count++; } } if (count == futureTaskList.size()) { ready = true; } // Thread.sleep(500); } LOG.debug("\n\n\n ====== DONE ====== "); LOG.debug(" time:" + (System.currentTimeMillis() - beginTime) + "ms\n\n"); executor.shutdown(); for (int i = 0; i < writes.length; i++) { LOG.debug("- " + writes[i]); } LOG.debug("\n\n\n ====== DONE ====== \n\n"); Thread.sleep(20000); LOG.debug("\n\n\n\n+++++++++++++++++++++++++"); LOG.debug("New system:"); IServiceSimple proxyService2 = clientSimple.getProxy(IServiceSimple.class); proxyService2.functionNumber1("1", "1"); }
From source file:com.test.test.MultipleRequestsForServerTest.java
@Test public void testSingleClientsForServer() throws Exception { // Number of threads final int size = 20; LOG.debug("clientSimple1:" + clientSimple); IServiceSimple proxyService = clientSimple.getProxy(IServiceSimple.class); List<ClientCallableForServer> clientCallableList = new ArrayList<ClientCallableForServer>(); for (int i = 0; i < size; i++) { clientCallableList.add(new ClientCallableForServer(proxyService, i)); }/*www . j a v a 2 s. co m*/ List<FutureTask<String>> futureTaskList = new ArrayList<FutureTask<String>>(); for (ClientCallableForServer clientCallable : clientCallableList) { futureTaskList.add(new FutureTask<String>(clientCallable)); } long beginTime = System.currentTimeMillis(); ExecutorService executor = Executors.newFixedThreadPool(futureTaskList.size()); for (FutureTask<String> futureTask : futureTaskList) { executor.execute(futureTask); } boolean ready = false; int[] dones = new int[futureTaskList.size()]; String[] writes = new String[futureTaskList.size()]; int indexValue = 0; while (!ready) { int count = 0; indexValue = 0; for (FutureTask<String> futureTask : futureTaskList) { if (futureTask.isDone() & dones[indexValue] == 0) { writes[indexValue] = futureTask.get(); dones[indexValue] = 1; } indexValue++; } for (int k = 0; k < dones.length; k++) { if (dones[k] == 1) { count++; } } if (count == futureTaskList.size()) { ready = true; } // Thread.sleep(500); } LOG.debug("\n\n\n ====== DONE ====== "); LOG.debug(" time:" + (System.currentTimeMillis() - beginTime) + "ms\n\n"); executor.shutdown(); for (int i = 0; i < writes.length; i++) { LOG.debug("- " + writes[i]); } LOG.debug("\n\n\n ====== DONE ====== \n\n"); Thread.sleep(20000); LOG.debug("\n\n\n\n+++++++++++++++++++++++++"); LOG.debug("New system:"); IServiceSimple proxyService2 = clientSimple.getProxy(IServiceSimple.class); proxyService2.functionNumber1("1", "1"); LOG.debug("\n\n\n\n==========================="); LOG.debug("And just sleep for empty pool"); Thread.sleep(40000); IServiceSimple proxyService3 = clientSimple.getProxy(IServiceSimple.class); proxyService3.functionNumber1("1", "1"); }
From source file:com.test.test.ClientServerTest.java
@Test public void testMutliThreaded() throws Exception { // Number of threads final int size = 10; LOG.debug("clientSimple1:" + clientSimple); IServiceSimple proxyService = clientSimple.getProxy(IServiceSimple.class); LOG.debug("proxyService:" + proxyService); List<ClientCallable> clientCallableList = new ArrayList<ClientCallable>(); for (int i = 0; i < size; i++) { clientCallableList.add(new ClientCallable(proxyService, i)); }// w w w . jav a 2s. c o m List<FutureTask<String>> futureTaskList = new ArrayList<FutureTask<String>>(); for (ClientCallable clientCallable : clientCallableList) { futureTaskList.add(new FutureTask<String>(clientCallable)); } long beginTime = System.currentTimeMillis(); ExecutorService executor = Executors.newFixedThreadPool(futureTaskList.size()); for (FutureTask<String> futureTask : futureTaskList) { executor.execute(futureTask); } boolean ready = false; int[] dones = new int[futureTaskList.size()]; String[] writes = new String[futureTaskList.size()]; int indexValue = 0; while (!ready) { int count = 0; indexValue = 0; for (FutureTask<String> futureTask : futureTaskList) { if (futureTask.isDone() & dones[indexValue] == 0) { writes[indexValue] = futureTask.get(); dones[indexValue] = 1; } indexValue++; } for (int k = 0; k < dones.length; k++) { if (dones[k] == 1) { count++; } } if (count == futureTaskList.size()) { ready = true; } // Thread.sleep(500); } LOG.debug("\n\n\n ====== DONE ====== "); LOG.debug(" time:" + (System.currentTimeMillis() - beginTime) + "ms\n\n"); executor.shutdown(); for (int i = 0; i < writes.length; i++) { LOG.debug("- " + writes[i]); } LOG.debug("\n\n\n ====== DONE ====== \n\n"); }
From source file:com.test.test.ClientServerTest.java
@Test public void testMutliThreadProxyClient() throws Exception { // Number of threads final int size = 20; LOG.debug("clientSimple1:" + clientSimple); List<IServiceSimple> serviceSimpleList = new ArrayList<IServiceSimple>(); for (int i = 0; i < size; i++) { IServiceSimple proxyService = clientSimple.getProxy(IServiceSimple.class); LOG.debug("proxyService:" + proxyService); serviceSimpleList.add(proxyService); }/*from w w w. j a va 2s .co m*/ List<ClientCallable> clientCallableList = new ArrayList<ClientCallable>(); for (int i = 0; i < size; i++) { clientCallableList.add(new ClientCallable(serviceSimpleList.get(i), i)); } List<FutureTask<String>> futureTaskList = new ArrayList<FutureTask<String>>(); for (ClientCallable clientCallable : clientCallableList) { futureTaskList.add(new FutureTask<String>(clientCallable)); } long beginTime = System.currentTimeMillis(); ExecutorService executor = Executors.newFixedThreadPool(futureTaskList.size()); for (FutureTask<String> futureTask : futureTaskList) { executor.execute(futureTask); } boolean ready = false; int[] dones = new int[futureTaskList.size()]; String[] writes = new String[futureTaskList.size()]; int indexValue = 0; while (!ready) { int count = 0; indexValue = 0; for (FutureTask<String> futureTask : futureTaskList) { if (futureTask.isDone() & dones[indexValue] == 0) { writes[indexValue] = futureTask.get(); dones[indexValue] = 1; } indexValue++; } for (int k = 0; k < dones.length; k++) { if (dones[k] == 1) { count++; } } if (count == futureTaskList.size()) { ready = true; } // Thread.sleep(500); } LOG.debug("\n\n\n ====== DONE ====== "); LOG.debug(" time:" + (System.currentTimeMillis() - beginTime) + "ms\n\n"); executor.shutdown(); for (int i = 0; i < writes.length; i++) { LOG.debug("- " + writes[i]); } LOG.debug("\n\n\n ====== DONE ====== \n\n"); Thread.sleep(20000); LOG.debug("\n\n\n\n+++++++++++++++++++++++++"); LOG.debug("New system:"); IServiceSimple proxyService2 = clientSimple.getProxy(IServiceSimple.class); proxyService2.functionNumber1("1", "1"); }
From source file:org.alfresco.repo.rendition.executer.AbstractTransformationRenderingEngine.java
@Override protected void render(RenderingContext context) { ContentReader contentReader = context.makeContentReader(); // There will have been an exception if there is no content data so contentReader is not null. String sourceUrl = contentReader.getContentUrl(); String sourceMimeType = contentReader.getMimetype(); String targetMimeType = getTargetMimeType(context); // The child NodeRef gets created here TransformationOptions options = getTransformOptions(context); // Log the following getTransform() as trace so we can see the wood for the trees ContentTransformer transformer;/*from w ww . ja v a 2s. co m*/ boolean orig = TransformerDebug.setDebugOutput(false); try { transformer = this.contentService.getTransformer(sourceUrl, sourceMimeType, contentReader.getSize(), targetMimeType, options); } finally { TransformerDebug.setDebugOutput(orig); } if (null == transformer) { // There's no transformer available for the requested rendition! throw new RenditionServiceException( String.format(TRANSFORMER_NOT_EXISTS_MESSAGE_PATTERN, sourceMimeType, targetMimeType)); } if (!transformer.isTransformable(sourceMimeType, contentReader.getSize(), targetMimeType, options)) { throw new RenditionServiceException( String.format(NOT_TRANSFORMABLE_MESSAGE_PATTERN, sourceMimeType, targetMimeType)); } long startTime = new Date().getTime(); boolean actionCancelled = false; boolean actionCompleted = false; // Cache the execution summary to get details later ExecutionSummary executionSummary = null; try { executionSummary = getExecutionSummary(context); } catch (ActionServiceException e) { if (logger.isInfoEnabled()) { logger.info("Cancelling of multiple concurrent action instances " + "currently unsupported, this action can't be cancelled"); } } // Call the transform in a different thread so we can move on if cancelled FutureTask<ContentWriter> transformTask = new FutureTask<ContentWriter>(new TransformationCallable( contentReader, targetMimeType, options, context, AuthenticationUtil.getFullyAuthenticatedUser())); getExecutorService().execute(transformTask); // Start checking for cancellation or timeout while (true) { try { Thread.sleep(CANCELLED_ACTION_POLLING_INTERVAL); if (transformTask.isDone()) { actionCompleted = true; break; } // Check timeout in case transformer doesn't obey it if (options.getTimeoutMs() > 0 && new Date().getTime() - startTime > (options.getTimeoutMs() + CANCELLED_ACTION_POLLING_INTERVAL)) { // We hit a timeout, let the transform thread continue but results will be ignored if (logger.isDebugEnabled()) { logger.debug( "Transformation did not obey timeout limit, " + "rendition action is moving on"); } break; } if (executionSummary != null) { ExecutionDetails executionDetails = actionTrackingService.getExecutionDetails(executionSummary); if (executionDetails != null) { actionCancelled = executionDetails.isCancelRequested(); if (actionCancelled) { if (logger.isDebugEnabled()) { logger.debug("Cancelling transformation"); } transformTask.cancel(true); break; } } } } catch (InterruptedException e) { // entire thread was asked to stop actionCancelled = true; transformTask.cancel(true); break; } } if (actionCancelled) { throw new RenditionCancelledException("Rendition action cancelled"); } if (!actionCompleted && !actionCancelled) { throw new RenditionServiceException("Transformation failed to obey timeout limit"); } if (actionCompleted) { // Copy content from temp writer to real writer ContentWriter writer = context.makeContentWriter(); try { // We should not need another timeout here, things should be ready for us ContentWriter tempTarget = transformTask.get(); if (tempTarget == null) { // We should never be in this state, but just in case throw new RenditionServiceException("Target of transformation not present"); } writer.putContent(tempTarget.getReader().getContentInputStream()); } catch (ExecutionException e) { // Unwrap our cause and throw that Throwable transformException = e.getCause(); if (transformException instanceof RuntimeException) { throw (RuntimeException) e.getCause(); } throw new RenditionServiceException(TRANSFORMING_ERROR_MESSAGE + e.getCause().getMessage(), e.getCause()); } catch (InterruptedException e) { // We were asked to stop transformTask.cancel(true); } } }
From source file:org.springframework.batch.core.step.tasklet.SystemCommandTasklet.java
/** * Execute system command and map its exit code to {@link ExitStatus} using * {@link SystemProcessExitCodeMapper}.//from w w w . j av a2 s .c o m */ @Override public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { FutureTask<Integer> systemCommandTask = new FutureTask<Integer>(new Callable<Integer>() { @Override public Integer call() throws Exception { Process process = Runtime.getRuntime().exec(command, environmentParams, workingDirectory); return process.waitFor(); } }); long t0 = System.currentTimeMillis(); taskExecutor.execute(systemCommandTask); while (true) { Thread.sleep(checkInterval);//moved to the end of the logic if (stoppable) { JobExecution jobExecution = jobExplorer .getJobExecution(chunkContext.getStepContext().getStepExecution().getJobExecutionId()); if (jobExecution.isStopping()) { stopped = true; } } if (systemCommandTask.isDone()) { contribution.setExitStatus(systemProcessExitCodeMapper.getExitStatus(systemCommandTask.get())); return RepeatStatus.FINISHED; } else if (System.currentTimeMillis() - t0 > timeout) { systemCommandTask.cancel(interruptOnCancel); throw new SystemCommandException("Execution of system command did not finish within the timeout"); } else if (execution.isTerminateOnly()) { systemCommandTask.cancel(interruptOnCancel); throw new JobInterruptedException( "Job interrupted while executing system command '" + command + "'"); } else if (stopped) { systemCommandTask.cancel(interruptOnCancel); contribution.setExitStatus(ExitStatus.STOPPED); return RepeatStatus.FINISHED; } } }