Here you can find the source of executeForever(final Runnable runnable)
Parameter | Description |
---|---|
runnable | a parameter |
@Deprecated public static Future<?> executeForever(final Runnable runnable)
//package com.java2s; //License from project: Apache License 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; import java.util.concurrent.atomic.AtomicBoolean; public class Main { /**/*from ww w . j av a 2 s.c o m*/ * @deprecated Should be using worker threads for everything really * @param runnable * @return */ @Deprecated public static Future<?> executeForever(final Runnable runnable) { final AtomicBoolean keepRunning = new AtomicBoolean(true); final Future<?> future = Executors.newFixedThreadPool(1).submit(new Runnable() { public void run() { while (keepRunning.get()) { runnable.run(); } } }); Future<?> realFuture = new Future<Object>() { public boolean cancel(boolean mayInterruptIfRunning) { keepRunning.set(false); return future.cancel(mayInterruptIfRunning); } public boolean isCancelled() { return future.isCancelled(); } public boolean isDone() { return future.isDone(); } public Object get() throws InterruptedException, ExecutionException { return future.get(); } public Object get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { return get(timeout, unit); } }; return realFuture; } }