Guava-Parallel programming futures detailed _java

Source: Internet
Author: User
Tags getmessage throwable

Guava provides a lot of useful extensions for Java parallel Programming future, its main interface is listenablefuture, and with the help of futures static extension.

Inherit to future Listenablefuture, allow us to add callback function return value or method execution completes immediately when the thread operation completes.

To add a callback function to Listenablefuture:

Futures.addcallback (LISTENABLEFUTURE<V>, Futurecallback<v>, Executor)

Where Futurecallback is an interface that contains onsuccess (V), OnFailure (Throwable).

Use such as:

Futures.addcallback (Listenablefuture, New futurecallback<object> () {public

  void onsuccess (Object result) {
    System.out.printf ("onsuccess with:%s%n", result);
  }

  public void OnFailure (Throwable thrown) {
    System.out.printf ("OnFailure%s%n", Thrown.getmessage ());
  }
);

At the same time in guava futures for future extensions there are:

    1. Transform: Converts the return value of Listenablefuture.
    2. Allaslist: A merge of multiple listenablefuture that returns a list object that returns multiple future return values when all future are successful. Note: When one of the future fails or is canceled, it will go into failure or cancel.
    3. Successfulaslist: Similar to Allaslist, the only difference is null substitution for failed or canceled future return values. Will not go into failure or cancel the process.
    4. Immediatefuture/immediatecancelledfuture: Returns a listenablefuture to return the value immediately.
    5. makechecked: Convert Listenablefuture to Checkedfuture. Checkedfuture is a listenablefuture that contains multiple versions of the Get method, and the method declaration throws a check exception. This makes it easier to create a future that can throw an exception in the execution logic
    6. Jdkfutureadapters.listeninpoolthread (future): Guava also provides an interface function to convert JDK future to listenablefuture.

Below is a test demo for future:

@Test public void Should_test_furture () throws Exception {Listeningexecutorservice service = Moreexecutors.listeningde

  Corator (Executors.newfixedthreadpool (10)); Listenablefuture future1 = service.submit (new callable<integer> () {public Integer call () throws Interruptedexce
      ption {thread.sleep (1000);
      SYSTEM.OUT.PRINTLN ("Call future 1.");
    return 1;

  }
  }); Listenablefuture Future2 = service.submit (new callable<integer> () {public Integer call () throws Interruptedexce
      ption {thread.sleep (1000);
  SYSTEM.OUT.PRINTLN ("Call Future 2.");
      throw new RuntimeException ("----Call Future 2.");
    return 2;

  }
  });

  Final Listenablefuture allfutures = Futures.allaslist (Future1, Future2); Final Listenablefuture transform = Futures.transform (allfutures, new Asyncfunction<list<integer>, Boolean > () {@Override public listenablefuture apply (list<integer> results) throws Exception {return FutuRes.immediatefuture (String.Format ("Success future:%d", Results.size ());

  }
  }); Futures.addcallback (Transform, new futurecallback<object> () {public void onsuccess (Object result) {Syst
      Em.out.println (Result.getclass ());
    System.out.printf ("Success with:%s%n", result);
    public void OnFailure (Throwable thrown) {System.out.printf ("onfailure%s%n", Thrown.getmessage ());

  }
  });
System.out.println (Transform.get ());

 }

Official information home: https://awk.so/@code. google.com!/p/guava-libraries/wiki/listenablefutureexplained

The above is the guava-parallel programming futures data collation, follow-up continue to supplement the relevant information thank you for your support of this site!

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.