Future Design Pattern of Java Multithreading

  • 2021-12-05 06:01:59
  • OfStack

Directory Future- > Represents the future 1 credential AsynFuture- > Future concrete implementation class FutureService - > Bridging Future and FutureTask FutureTask - > Isolate your calling logic

Future - > Represents 1 credential for the future


public interface Future<T> {
    T get() throws InterruptedException;
}

AsynFuture - > Future concrete implementation class


public class AsynFuture<T> implements Future<T> {

    private volatile boolean done = false;

    private T result;

    public void done(T result){
        synchronized (this){
            this.result = result;
            this.done = true;
            this.notifyAll();
        }
    }
    /**
     *  Polling   Waiting not completed 
     */
    @Override
    public T get() throws InterruptedException {
        synchronized (this) {
            while (!done) {
                this.wait();
            }
        }
        return result;
    }
}

FutureService - > Bridging Future and FutureTask


public class FutureService {

    /**
     *  Process waiting is required 
     */
    public <T> Future<T> submit(final FutureTask<T> task) {

        AsynFuture<T> asynFuture = new AsynFuture<>();

        new Thread(() -> {

            T result = task.call();
            asynFuture.done(result);

        }).start();
        return asynFuture;
    }

    /**
     *  Finish running   Automatic callback 
     *  No process waiting 
     */
    public <T> Future<T> submit(final FutureTask<T> task, final Consumer<T> consumer) {

        AsynFuture<T> asynFuture = new AsynFuture<>();
        new Thread(() -> {
            T result = task.call();
            asynFuture.done(result);
            consumer.accept(result);
        }).start();
        return asynFuture;
    }
}

FutureTask - > Isolate your calling logic


public interface FutureTask<T> {

    T call();
}

Callback when needed:


/**
 * Future        ->  It represents the future 1 Credentials 
 * FutureTask    ->  Isolate your calling logic 
 * FutureService ->  Bridging Future And FutureTask
 */
public class SyncInvoker {

    public static void main(String[] args) throws InterruptedException {

        FutureService futureService = new FutureService();
        Future<String> future = futureService.submit(() -> {
            try {
                Thread.sleep(10001);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "FINISH";
        });

        System.out.println("==============");
        System.out.println("do other thing.");
        Thread.sleep(1000);

        System.out.println("==============");

        /**
         *  The call also forms a block 
         */
        System.out.println(future.get());
    }
}

Running:

==============
do other thing.
==============
FINISH

After running the automatic callback:


//**
 * Future        ->  It represents the future 1 Credentials 
 * FutureTask    ->  Isolate your calling logic 
 * FutureService ->  Bridging Future And FutureTask
 */
public class SyncInvoker {

    public static void main(String[] args) throws InterruptedException {

        FutureService futureService = new FutureService();
        futureService.submit(() -> {
            try {
                Thread.sleep(10001);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "FINISH";
        },System.out::println);

        System.out.println("==============");
        System.out.println("do other thing.");
        Thread.sleep(1000);
        System.out.println("==============");
    }
}

Related articles: