Example usage for java.util.concurrent FutureTask FutureTask

List of usage examples for java.util.concurrent FutureTask FutureTask

Introduction

In this page you can find the example usage for java.util.concurrent FutureTask FutureTask.

Prototype

public FutureTask(Runnable runnable, V result) 

Source Link

Document

Creates a FutureTask that will, upon running, execute the given Runnable , and arrange that get will return the given result on successful completion.

Usage

From source file:org.gytheio.health.CompositeComponentUnavailableAction.java

protected void execute(final ComponentUnavailableAction action, final Throwable e) throws Throwable {
    FutureTask<Void> task = null;
    try {//from w w  w . j av a 2 s  . c om
        task = new FutureTask<Void>(new Runnable() {
            @Override
            public void run() {
                action.execute(e);
            }
        }, null);

        getExecutorService().execute(task);
        task.get(actionTimeoutMs, TimeUnit.MILLISECONDS);
    } catch (TimeoutException e1) {
        task.cancel(true);
        throw e;
    } catch (InterruptedException e1) {
        // We were asked to stop
        task.cancel(true);
    } catch (ExecutionException e1) {
        // Unwrap our cause and throw that
        Throwable cause = e.getCause();
        throw cause;
    }
}

From source file:io.pivotal.spring.xd.jdbcgpfdist.GPFDistMessageHandler.java

public void start() {

    try {//w w w. j  a v  a2 s .  c o  m

        taskFuture = new TaskFuture();
        //init counter
        meterCount = 0;

        context = new RuntimeContext();

        Environment.initializeIfEmpty().assignErrorJournal();
        processor = RingBufferProcessor.create(false);

        if (rateInterval > 0) {
            meter = new Meter();
        }

        log.info("Creating gpfdist protocol listener on port=" + port);
        gpfdistServer = new GPFDistServer(processor, port, flushCount, flushTime, batchTimeout, batchCount);
        gpfdistServer.start();
        log.info("gpfdist protocol listener running on port=" + gpfdistServer.getLocalPort());
        context.addLocation(NetworkUtils.getGPFDistUri(gpfdistServer.getLocalPort()));

        log.info("Scheduling gpfdist task with batchPeriod=" + batchPeriod);
        sqlTaskScheduler.schedule((new FutureTask<Void>(() -> {
            boolean taskValue = true;
            try {
                while (!taskFuture.interrupted) {
                    try {
                        greenplumLoad.load(context);
                    } catch (Exception e) {
                        log.error("Error in load", e);
                    }
                    Thread.sleep(batchPeriod * 1000);
                }
            } catch (Exception e) {
                taskValue = false;
            }
            taskFuture.set(taskValue);
        }, null)), new Date());

    } catch (Exception e) {
        throw new RuntimeException("Error starting protocol listener", e);
    }

}

From source file:org.jactr.core.module.declarative.four.learning.DefaultDeclarativeLearningModule4.java

public DefaultDeclarativeLearningModule4() {
    super("DeclarativeLearningV4");

    _modelListener = new ModelListenerAdaptor() {

        @Override//from ww  w  .ja v  a  2s.c  om
        public void moduleInstalled(ModelEvent event) {
            tryToAttach();
        }

        @Override
        public void cycleStarted(ModelEvent event) {
            /*
             * we don't need this code to do anything. it will be executed after all
             * the other tasks have been, i.e., all learning is done. if
             * getExecutor() returns the inline, nothing changes
             */
            FutureTask<Object> blockingTask = new FutureTask<Object>(new Runnable() {

                public void run() {
                    // noop once completed, object will be available
                }
            }, new Object());

            getExecutor().execute(blockingTask);

            try {
                blockingTask.get();
            } catch (Exception e) {
                LOGGER.error("Could not block while waiting for learning to finish ", e);
            }
        }
    };
}

From source file:com.googlecode.jsfFlex.shared.tasks.TaskRunnerImpl.java

public void queueFutureTask(final String taskName, final AbstractTask toAdd) {
    final FutureTask<Void> task = new FutureTask<Void>(new Runnable() {
        public void run() {
            toAdd.performTask();//from w  ww . j  av  a2 s. co m
            _queuedTasks.remove(taskName);
            _log.info("Queued taskName has been completed : " + taskName);
        }
    }, null);

    Future<Void> previousTask = _queuedTasks.putIfAbsent(taskName, task);
    if (previousTask != null) {
        throw new RuntimeException(DUPLICATE_QUEUE_TASK_ID_PROVIDED + taskName);
    }
    _queuedService.submit(task);
}

From source file:org.luwrain.app.commander.PanelArea.java

boolean runHookOnFilesToProcess(String hookPrefix, boolean background) {
    NullCheck.notEmpty(hookPrefix, "hookPrefix");
    final Object arg;
    final String hookName;
    if (isLocalDir()) {
        final File[] files = getFilesToProcess();
        if (files.length == 0)
            return false;
        final String[] names = new String[files.length];
        for (int i = 0; i < files.length; i++)
            names[i] = files[i].getAbsolutePath();
        arg = ScriptUtils.createReadOnlyArray(names);
        hookName = hookPrefix + ".local";
    } else/*  w  w  w.j  a v a 2s  . co  m*/
        return false;
    if (!background)
        try {
            if (luwrain.xRunHooks(hookName + ".custom", new Object[] { arg },
                    Luwrain.HookStrategy.CHAIN_OF_RESPONSIBILITY))
                return true;
            return luwrain.xRunHooks(hookName, new Object[] { arg },
                    Luwrain.HookStrategy.CHAIN_OF_RESPONSIBILITY);
        } catch (RuntimeException e) {
            luwrain.crash(e);
            return true;
        }
    luwrain.executeBkg(new FutureTask(() -> {
        try {
            if (luwrain.xRunHooks(hookName + ".custom", new Object[] { arg },
                    Luwrain.HookStrategy.CHAIN_OF_RESPONSIBILITY))
                return;
            luwrain.xRunHooks(hookName, new Object[] { arg }, Luwrain.HookStrategy.CHAIN_OF_RESPONSIBILITY);
            return;
        } catch (RuntimeException e) {
            luwrain.crash(e);
        }
    }, null));
    return true;
}

From source file:org.springframework.xd.greenplum.gpfdist.GPFDistMessageHandler.java

@Override
protected void doStart() {
    try {/* w  ww .j a  v  a 2 s . com*/
        log.info("Creating gpfdist protocol listener on port=" + port);
        gpfdistServer = new GPFDistServer(processor, port, flushCount, flushTime, batchTimeout, batchCount);
        gpfdistServer.start();
        log.info("gpfdist protocol listener running on port=" + gpfdistServer.getLocalPort());
    } catch (Exception e) {
        throw new RuntimeException("Error starting protocol listener", e);
    }

    if (greenplumLoad != null) {
        log.info("Scheduling gpload task with batchPeriod=" + batchPeriod);

        final RuntimeContext context = new RuntimeContext();
        context.addLocation(NetworkUtils.getGPFDistUri(gpfdistServer.getLocalPort()));

        sqlTaskScheduler.schedule((new FutureTask<Void>(new Runnable() {
            @Override
            public void run() {
                boolean taskValue = true;
                try {
                    while (!taskFuture.interrupted) {
                        try {
                            greenplumLoad.load(context);
                        } catch (Exception e) {
                            log.error("Error in load", e);
                        }
                        Thread.sleep(batchPeriod * 1000);
                    }
                } catch (Exception e) {
                    taskValue = false;
                }
                taskFuture.set(taskValue);
            }
        }, null)), new Date());

    } else {
        log.info("Skipping gpload tasks because greenplumLoad is not set");
    }
}

From source file:org.commonreality.sensors.xml.XMLSensor.java

@Override
public void stop() throws Exception {
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("Stopping");

    _isStopping = true;//from  w ww  . j  a va 2 s.c o m
    synchronized (_pendingTimeFrames) {
        _pendingTimeFrames.clear();
    }

    // clear out the trash
    FutureTask<Boolean> waitForMe = new FutureTask<Boolean>(new Runnable() {

        public void run() {
        }
    }, Boolean.TRUE);
    _service.execute(waitForMe);

    /*
     * interrupt the executor.. so that if it is currently waiting, say for the
     * clock..
     */
    _noOp.interrupt();
    _processFrame.interrupt();

    /*
     * and wait for it to finish
     */
    waitForMe.get();
    super.stop();
}

From source file:org.springframework.cloud.stream.module.gpfdist.sink.GpfdistMessageHandler.java

@Override
protected void doStart() {
    try {/* ww  w . jav  a 2 s .  c o m*/
        log.info("Creating gpfdist protocol listener on port=" + port);
        gpfdistServer = new GpfdistServer(processor, port, flushCount, flushTime, batchTimeout, batchCount);
        gpfdistServer.start();
        log.info("gpfdist protocol listener running on port=" + gpfdistServer.getLocalPort());
    } catch (Exception e) {
        throw new RuntimeException("Error starting protocol listener", e);
    }

    if (greenplumLoad != null) {
        log.info("Scheduling gpload task with batchPeriod=" + batchPeriod);

        final RuntimeContext context = new RuntimeContext();
        context.addLocation(NetworkUtils.getGPFDistUri(gpfdistServer.getLocalPort()));

        sqlTaskScheduler.schedule((new FutureTask<Void>(new Runnable() {
            @Override
            public void run() {
                boolean taskValue = true;
                try {
                    while (!taskFuture.interrupted) {
                        try {
                            greenplumLoad.load(context);
                        } catch (Exception e) {
                            log.error("Error in load", e);
                        }
                        Thread.sleep(batchPeriod * 1000);
                    }
                } catch (Exception e) {
                    taskValue = false;
                }
                taskFuture.set(taskValue);
            }
        }, null)), new Date());

    } else {
        log.info("Skipping gpload tasks because greenplumLoad is not set");
    }
}

From source file:org.springframework.cloud.stream.app.gpfdist.sink.GpfdistMessageHandler.java

@Override
protected void doStart() {
    try {//from  w  w w . ja v  a2s  .  c o m
        log.info("Creating gpfdist protocol listener on port=" + port);
        gpfdistServer = new GpfdistServer(processor, port, flushCount, flushTime, batchTimeout, batchCount);
        gpfdistServer.start();
        log.info("gpfdist protocol listener running on port=" + gpfdistServer.getLocalPort());
    } catch (Exception e) {
        throw new RuntimeException("Error starting protocol listener", e);
    }

    if (greenplumLoad != null) {
        log.info("Scheduling gpload task with batchPeriod=" + batchPeriod);

        final RuntimeContext context = new RuntimeContext(NetworkUtils
                .getGPFDistUri(hostInfoDiscovery.getHostInfo().getAddress(), gpfdistServer.getLocalPort()));

        sqlTaskScheduler.schedule((new FutureTask<Void>(new Runnable() {
            @Override
            public void run() {
                boolean taskValue = true;
                try {
                    while (!taskFuture.interrupted) {
                        try {
                            greenplumLoad.load(context);
                        } catch (Exception e) {
                            log.error("Error in load", e);
                        }
                        Thread.sleep(batchPeriod * 1000);
                    }
                } catch (Exception e) {
                    taskValue = false;
                }
                taskFuture.set(taskValue);
            }
        }, null)), new Date());

    } else {
        log.info("Skipping gpload tasks because greenplumLoad is not set");
    }
}

From source file:test.com.azaptree.services.executor.ThreadPoolExecutorTest.java

@Test
public void testPurge() {
    pauseableExecutor.setCorePoolSize(1);
    pauseableExecutor.setMaximumPoolSize(1);
    pauseableExecutor.pause();/*ww w.  j a va  2s . c  o  m*/
    for (int i = 0; i < 100; i++) {
        final FutureTask<Boolean> futureTask = new FutureTask<>(new Task(), Boolean.TRUE);
        pauseableExecutor.execute(futureTask);
        futureTask.cancel(true);
    }
    Assert.assertEquals(pauseableExecutor.getQueue().size(), 99);
    pauseableExecutor.purge();
    Assert.assertEquals(pauseableExecutor.getQueue().size(), 0);
    pauseableExecutor.resume();
}