Java Concurrency Runable,callable,future,futuretask

Source: Internet
Author: User

Reprinted from: http://www.cnblogs.com/dolphin0520/p/3949310.html

 PackageFuture_call;Importjava.util.concurrent.Callable;/*** Created by Luozhitao on 2017/8/10.*/ Public classTaskImplementsCallable<integer> {  //@Override     PublicInteger Call ()throwsException {System.out.println ("Child threads are being evaluated"); Thread.Sleep (10000); System.out.println ("Child Threads Sleep Complete"); intSum=0;  for(inti=0;i<100;i++) {sum+=i; }        returnsum; }}

1. Using Callable+future

 PackageFuture_call;Importjava.util.concurrent.*;/*** Created by Luozhitao on 2017/8/10.*/ Public classTask_test { Public Static voidmain (String [] args) {Executorservice Executorservice=Executors.newcachedthreadpool (); Task Task=NewTask (); Future<Integer> future=NULL; Try{           //Executorservice Future=Executorservice.submit (Task); System.out.println ("Before the thread pool closes");            Executorservice.shutdown (); System.out.println ("After the thread pool is closed"); }Catch(rejectedexecutionexception e) {System.out.println (e);} Try{Thread.Sleep (1000); }Catch(interruptedexception e) {e.printstacktrace ();} System.out.println ("Main thread is in the process of calculation"); Try{System.out.println (Future.get ()); }Catch(Interruptedexception e) {E.printstacktrace ();}Catch(executionexception e) {e.printstacktrace ();} }}

2. Using Callable+futuretask

 PackageFuture_call;Importjava.util.concurrent.*;/*** Created by Luozhitao on 2017/8/10.*/ Public classFuture_task1 { Public Static voidmain (String [] args) {Executorservice Executorservice=Executors.newcachedthreadpool (); Task Task=NewTask (); Futuretask<Integer> futuretask=NewFuturetask<integer>(Task); Try {            /*Executorservice.submit (Futuretask);            Executorservice.shutdown (); */Thread Thread=NewThread (Futuretask);        Thread.Start (); }Catch(rejectedexecutionexception e) {e.printstacktrace ();} Try{Thread.Sleep (1000); System.out.println ("Main thread sleep complete"); }Catch(interruptedexception e) {e.printstacktrace ();} Try{System.out.println (Futuretask.get ()); }Catch(Interruptedexception e) {E.printstacktrace ();}Catch(executionexception e) {e.printstacktrace ();} }}

Java Concurrency Runable,callable,future,futuretask

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.