Java Thread Executor Execute execute(String name, Runnable runnable)

Here you can find the source of execute(String name, Runnable runnable)

Description

execute

License

Apache License

Parameter

Parameter Description
runnable a parameter

Declaration

@Deprecated
public static Future<?> execute(String name, Runnable runnable) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class Main {
    /**/*from ww  w . j a  va  2  s . c  o m*/
     * @deprecated Should be using worker threads for everything really
     * @param runnable
     * @return
     */
    @Deprecated
    public static Future<?> execute(String name, Runnable runnable) {
        final Thread thread = new Thread(runnable, name);

        Future<?> future = new Future<Void>() {
            public boolean cancel(boolean mayInterruptIfRunning) {
                return false;
            }

            public boolean isCancelled() {
                return false;
            }

            public boolean isDone() {
                return !thread.isAlive();
            }

            public Void get() throws InterruptedException, ExecutionException {
                thread.join();
                return null;
            }

            public Void get(long timeout, TimeUnit unit)
                    throws InterruptedException, ExecutionException, TimeoutException {
                thread.join(unit.toMillis(timeout));
                if (thread.isAlive()) {
                    throw new TimeoutException("Thread is still alive after timeout");
                }
                return null;

            }
        };

        thread.start();

        return future;
    }

    /**
     * @deprecated Should be using worker threads for everything really
     * @param runnable
     * @return
     */
    @Deprecated
    public static <T> Future<T> execute(Callable<T> callable) {
        Future<T> future = Executors.newFixedThreadPool(1).submit(callable);
        return future;
    }
}

Related

  1. execute(Runnable command)
  2. execute(Runnable command)
  3. execute(Runnable runnable)
  4. execute(Runnable task)
  5. execute(Runnable task)
  6. executeAndWait(final Runnable r)
  7. executeAsynchronously(Runnable r)
  8. executeForever(final Runnable runnable)
  9. executeFuture(Callable callable)