Java 异步里面执行回调

Posted wb54979

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 异步里面执行回调相关的知识,希望对你有一定的参考价值。


public class A 

    static abstract class CallBack
        public void execu(Object str)
        
    

    static class CallBackA1 extends CallBack
        @Override
        public void execu(Object str)
            System.out.println("AAA---"+str);
        
    


    static class CallBackA2 extends CallBack
        @Override
        public void execu(Object str)
            System.out.println("BBBB---"+str);
        
    

    static class TestTask extends TimerTask
        private CallBack callBack;
        private Object obj;
        public TestTask(CallBack callBack,
                        Object ob)
            this.callBack = callBack;
            this.obj = ob;
        

        @Override
        public void run() 
            this.callBack.execu(this.obj);
        
    

    public static void main(String[] args) 
        CallBackA1 a1 = new CallBackA1();
        TestTask task = new TestTask(a1,"AAA");
        Timer timer = new Timer();
        timer.schedule(task,3000);
        System.out.println("-----");

        CallBackA2 a2 = new CallBackA2();
        TestTask task1 = new TestTask(a2,"BBB");
        Timer timer1 = new Timer();
        timer1.schedule(task1,4000);
        System.out.println("-----");
    

以上是关于Java 异步里面执行回调的主要内容,如果未能解决你的问题,请参考以下文章

Java 异步里面执行回调

13_CompletableFuture异步回调

13_CompletableFuture异步回调

java(java8 CompletableFuture)异步执行之后获取回调

异步回调函数

netty的Future异步回调难理解?手写个带回调异步框架就懂了附源码