Java中异步回调

1、先定义一个异步工作接口

public interface Worker {
    String action(Object object);
}

2、再定义一个回调监听器,将Worker的执行结果 放到result的参数里

public interface Listener {
    void result(Object result);
}

3、再定义包装器,将Worker和Listener包装一下

public class Wrapper {
    private Object param;
    private Worker worker;
    private Listener listener;

    public Object getParam() {
        return param;
    }

    public void setParam(Object param) {
        this.param = param;
    }

    public Worker getWorker() {
        return worker;
    }

    public void setWorker(Worker worker) {
        this.worker = worker;
    }

    public Listener getListener() {
        return listener;
    }

    public void addListener(Listener listener) {
        this.listener = listener;
    }
}

4、测试回调

public class CallBackTest {
    public static void main(String[] args) {
        CallBackTest callBackTest = new CallBackTest();

        Worker worker = callBackTest.newWorker();

        Wrapper wrapper = new Wrapper();
        wrapper.setWorker(worker);
        wrapper.setParam("I am a Param");

        callBackTest.doWork(wrapper).addListener(new Listener() {
            @Override
            public void result(Object result) { // 耗时线程执行完毕后,回调这个方法
                System.out.println(Thread.currentThread().getName()); // Thread-0
                System.out.println(result); // I am a Param action result
            }
        });

        System.out.println(Thread.currentThread().getName()); // main

    }

    private Wrapper doWork(Wrapper wrapper) {
        new Thread(() -> {
            Worker worker = wrapper.getWorker();
            String result = worker.action(wrapper.getParam());
            wrapper.getListener().result(result);
        }).start();

        return wrapper;
    }

    private Worker newWorker() {
        return new Worker() {
            @Override
            public String action(Object object) { // 这里object就是param参数
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return object + " action result";
            }
        };
    }
}

5、测试回调带超时

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CallBackTimeOutTest {
    public static void main(String[] args) {

        CallBackTimeOutTest callBackTimeOutTest = new CallBackTimeOutTest();

        Worker worker = callBackTimeOutTest.newWorker();

        Wrapper wrapper = new Wrapper();
        wrapper.setWorker(worker);
        wrapper.setParam("I am Param");
        //添加结果回调器
        wrapper.addListener(new Listener() {
            @Override
            public void result(Object result) {
                System.out.println(result); // time out exception
            }
        });

        CompletableFuture future = CompletableFuture.supplyAsync(() -> callBackTimeOutTest.doWork(wrapper));
        try {
            future.get(800, TimeUnit.MILLISECONDS); // 超过800ms就算超时
        } catch (InterruptedException | TimeoutException | ExecutionException e) {
            //超时了
            wrapper.getListener().result("time out exception");
        }

        System.out.println(Thread.currentThread().getName()); // main

    }

    private Wrapper doWork(Wrapper wrapper) {
        Worker worker = wrapper.getWorker();
        String result = worker.action(wrapper.getParam());
        wrapper.getListener().result(result);

        return wrapper;
    }

    private Worker newWorker() {
        return new Worker() {
            @Override
            public String action(Object object) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return object + " action result";
            }
        };
    }

}

你可能感兴趣的:(Java,异步调用,回调,java,开发语言)