Asynchronous callbacks in Java

Source: Internet
Author: User

Asynchronous callback, originally in C # is a very simple and elegant thing, unexpectedly in the Java world, but so cumbersome, first look at the following class diagram:

First define a callbacktask, as the outer face of the project, the main work for start to start an asynchronous operation, but the real work is callbackbody, it is inside the execute is the real thing to deal with, if successful, then trigger Onsucess, Otherwise, the onfailure is triggered.

Callbackapp as the final stage of the run, which has to run a separate thread, to start callbacktask, so that it does not block the subsequent processing.

Callbackbody:

Package Com.cnblogs.yjmyzz.test.callback;public abstract class Callbackbody {    void onsuccess (Object context) {        System.out.println ("onsuccess");    }    void OnFailure (Object context) {        System.out.println ("OnFailure");    }    abstract void execute (Object context) throws Exception;}

Callbacktask:

Package Com.cnblogs.yjmyzz.test.callback;public class Callbacktask {    private callbackbody body;    Public Callbacktask (Callbackbody body) {        this.body = body;    }    protected void Start (final Object context) {        final thread t = new Thread (new Runnable () {public            void run () {                try {                    Body.execute (context);                } catch (Exception e) {                    e.printstacktrace ();                    Body.onfailure (context);                }                Body.onsuccess (context);            }        });        T.start ();    }}

Finally, test a piece:

Callbackapp:

package com.cnblogs.yjmyzz.test.callback;/** * Created by Jimmy on 8/13/15. */        public class Callbackapp {public static void main (string[] args) {System.out.println ("Ready to start performing asynchronous tasks ...");        Final Object context = "contextual information";                New Callbacktask (New Callbackbody () {@Override void execute (Object context) throws Exception {                System.out.println ("\ n performing a time-consuming operation ...");                SYSTEM.OUT.PRINTLN (context);                Thread.Sleep (5000); System.out.println ("\ n execution done!            ");                } void Onsuccess (Object context) {System.out.println ("\ n Successful callback function ...");            SYSTEM.OUT.PRINTLN (context);                } void OnFailure (Object context) {System.out.println ("\ n failed callback function ...");            SYSTEM.OUT.PRINTLN (context);        }}). Start (context);    System.out.println ("\ n Async task has started, please wait for completion ..."); }}

Operation Result:

Ready to start executing asynchronous tasks ...

Performing time-consuming operations ...
Contextual information

Asynchronous task has started, please wait to finish ...
Execution Done!

The callback function after success ...
Contextual information

Asynchronous callbacks in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.