Search in sources :

Example 76 with FutureTask

use of java.util.concurrent.FutureTask in project spring-framework by spring-projects.

the class SimpleConfigTests method testFooService.

@Test
public void testFooService() throws Exception {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(getConfigLocations(), getClass());
    FooService fooService = ctx.getBean("fooServiceImpl", FooService.class);
    ServiceInvocationCounter serviceInvocationCounter = ctx.getBean("serviceInvocationCounter", ServiceInvocationCounter.class);
    String value = fooService.foo(1);
    assertEquals("bar", value);
    Future<?> future = fooService.asyncFoo(1);
    assertTrue(future instanceof FutureTask);
    assertEquals("bar", future.get());
    assertEquals(2, serviceInvocationCounter.getCount());
    fooService.foo(1);
    assertEquals(3, serviceInvocationCounter.getCount());
}
Also used : FooService(example.scannable.FooService) ServiceInvocationCounter(example.scannable.ServiceInvocationCounter) FutureTask(java.util.concurrent.FutureTask) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Test(org.junit.Test)

Example 77 with FutureTask

use of java.util.concurrent.FutureTask in project spring-framework by spring-projects.

the class ThreadPoolExecutorFactoryBeanTests method defaultExecutor.

@Test
public void defaultExecutor() throws Exception {
    ApplicationContext context = new AnnotationConfigApplicationContext(ExecutorConfig.class);
    ExecutorService executor = context.getBean("executor", ExecutorService.class);
    FutureTask<String> task = new FutureTask<>(new Callable<String>() {

        @Override
        public String call() throws Exception {
            return "foo";
        }
    });
    executor.execute(task);
    assertEquals("foo", task.get());
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) FutureTask(java.util.concurrent.FutureTask) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 78 with FutureTask

use of java.util.concurrent.FutureTask in project spring-framework by spring-projects.

the class TaskExecutorAdapter method submit.

@Override
public Future<?> submit(Runnable task) {
    try {
        if (this.taskDecorator == null && this.concurrentExecutor instanceof ExecutorService) {
            return ((ExecutorService) this.concurrentExecutor).submit(task);
        } else {
            FutureTask<Object> future = new FutureTask<>(task, null);
            doExecute(this.concurrentExecutor, this.taskDecorator, future);
            return future;
        }
    } catch (RejectedExecutionException ex) {
        throw new TaskRejectedException("Executor [" + this.concurrentExecutor + "] did not accept task: " + task, ex);
    }
}
Also used : TaskRejectedException(org.springframework.core.task.TaskRejectedException) ListenableFutureTask(org.springframework.util.concurrent.ListenableFutureTask) FutureTask(java.util.concurrent.FutureTask) ExecutorService(java.util.concurrent.ExecutorService) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 79 with FutureTask

use of java.util.concurrent.FutureTask in project siena by mandubian.

the class BaseMultiThreadTest method testMultiThreadSimple.

public void testMultiThreadSimple() {
    int count = 0;
    while (count < 1000) {
        count++;
        logger.info("Launching " + count + "-th operation");
        final int c = count;
        FutureTask task = new FutureTask<Object>(new Thread() {

            public void run() {
                Model.getByKey(PersonLongAutoIDModel.class, PERSON_LONGAUTOID_TESLA.id);
                logger.info("Executed " + c + "-th operation");
            }
        }, null);
        Thread thread = new Thread(task);
        thread.start();
        try {
            task.get(1l, TimeUnit.HOURS);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Also used : PersonLongAutoIDModel(siena.base.test.model.PersonLongAutoIDModel) FutureTask(java.util.concurrent.FutureTask) SienaException(siena.SienaException)

Example 80 with FutureTask

use of java.util.concurrent.FutureTask in project Denizen-For-Bukkit by DenizenScript.

the class ChatTrigger method asyncChatTrigger.

@EventHandler
public void asyncChatTrigger(final AsyncPlayerChatEvent event) {
    if (HyperDebug) {
        dB.log("Chat trigger seen, cancelled: " + event.isCancelled() + ", chatasync: " + Settings.chatAsynchronous());
    }
    if (event.isCancelled()) {
        return;
    }
    // Return if "Use asynchronous event" is false in config file
    if (!Settings.chatAsynchronous()) {
        return;
    }
    if (!event.isAsynchronous()) {
        syncChatTrigger(new PlayerChatEvent(event.getPlayer(), event.getMessage(), event.getFormat(), event.getRecipients()));
        return;
    }
    FutureTask<ChatContext> futureTask = new FutureTask<ChatContext>(new Callable<ChatContext>() {

        @Override
        public ChatContext call() {
            return process(event.getPlayer(), event.getMessage());
        }
    });
    Bukkit.getScheduler().runTask(DenizenAPI.getCurrentInstance(), futureTask);
    try {
        ChatContext context = futureTask.get();
        if (context.wasTriggered()) {
            event.setCancelled(true);
        }
        if (context.hasChanges()) {
            event.setMessage(context.getChanges());
        }
    } catch (Exception e) {
        dB.echoError(e);
    }
}
Also used : AsyncPlayerChatEvent(org.bukkit.event.player.AsyncPlayerChatEvent) PlayerChatEvent(org.bukkit.event.player.PlayerChatEvent) FutureTask(java.util.concurrent.FutureTask) EventHandler(org.bukkit.event.EventHandler)

Aggregations

FutureTask (java.util.concurrent.FutureTask)126 ExecutionException (java.util.concurrent.ExecutionException)44 Test (org.junit.Test)32 IOException (java.io.IOException)28 ExecutorService (java.util.concurrent.ExecutorService)23 Callable (java.util.concurrent.Callable)22 CountDownLatch (java.util.concurrent.CountDownLatch)20 TimeoutException (java.util.concurrent.TimeoutException)14 Handler (android.os.Handler)12 ArrayList (java.util.ArrayList)12 CancellationException (java.util.concurrent.CancellationException)10 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 Future (java.util.concurrent.Future)8 AccessibleObject (java.lang.reflect.AccessibleObject)6 List (java.util.List)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 FileNotFoundException (java.io.FileNotFoundException)5 InputStream (java.io.InputStream)5 Iterator (java.util.Iterator)5 CancellationSignal (android.os.CancellationSignal)4