Implementation of callable Interface Based on Java Multithreading

Source: Internet
Author: User
Import Java. util. concurrent. callable; import Java. util. concurrent. executorservice; import Java. util. concurrent. executors; import Java. util. concurrent. future;/*** callable and future interfaces * callable are similar to runnable interfaces. classes that implement callable interfaces and runnable classes are tasks that can be executed by other threads. * There are several differences between callable and runnable: * (1) callable specifies the call () method, while runnable specifies the run () method (). * (2) callable tasks can return values after execution, while runnable tasks cannot return values. * (3) the call () method can throw an exception, while the run () method cannot throw an exception. * (4) run the callable task to get a future object. Future indicates the result of asynchronous calculation. * It provides a method to check whether the computation is complete, waiting for the computation to complete and retrieving the computation results. * The future object can be used to understand the task execution status, cancel the task execution, and obtain the task execution result. */Public class callableandfuture {/*** customizes a task class and implements the callable interface */public static class mycallableclass implements callable {// flag private int flag = 0; public mycallableclass (INT flag) {This. flag = flag;} Public String call () throws exception {If (this. flag = 0) {// if the flag value is 0, return immediately "flag = 0";} If (this. flag = 1) {// if the flag value is 1, create an infinite loop try {While (true) {system. out. println ("looping ...... "); threa D. sleep (2000) ;}} catch (interruptedexception e) {system. out. println ("interrupted");} return "false";} else {// falg is not 0 or 1, Throw throw new exception ("Bad flag value! ") ;}} Public static void main (string [] ARGs) {// defines three callable tasks: mycallableclass task1 = new mycallableclass (0 ); mycallableclass task2 = new mycallableclass (1); mycallableclass task3 = new mycallableclass (2); // create a job execution service executorservice es = executors. newfixedthreadpool (3); try {// submit and execute the task. When the task is started, a future object is returned, // if you want to obtain the task execution result or an exception, you can operate on this future object. Future future1 = es. submit (task1); // obtain the result of the first task. If you call the get The current thread will wait until the task is executed. out. println ("task1:" + future1.get (); Future future2 = es. submit (task2); // wait for 5 seconds before stopping the second task. Because the second task executes an infinite loop thread. sleep (1, 5000); system. out. println ("task2 cancel:" + future2.cancel (true); // gets the output of the third task, because the execution of the third task will cause an exception // The following statement will cause an exception and throw future future3 = es. submit (task3); system. out. println ("task3:" + future3.get ();} catch (exception e) {system. out. println (E. tostring ();} // stop the task execution service es. shutdownnow ();}}

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.