C # Task and Java Future

Source: Internet
Author: User

I haven't looked at C # For a long time since I switched to Java in the project, but to be honest, I am in Cao camp. I have long known that. NET4.5 has added async and await, but it has never been used. Today I have seen this article to understand it. I suddenly found that Task is not the concept of Future in Java? The C # code in Jesse Liu's article: static void Main (string [] args) {Console. writeLine ("Main Thread Id: {0} \ r \ n", Thread. currentThread. managedThreadId); Test (); Console. readLine ();} static async Task Test () {Console. writeLine ("Before calling GetName, Thread Id: {0} \ r \ n", Thread. currentThread. managedThreadId); var name = GetName (); Console. writeLine ("End calling GetName. \ r \ n "); Console. writeLine ("Get resul T from GetName: {0} ", await name);} static async Task GetName () {// here is the main thread Console. writeLine ("Before calling Task. run, current thread Id is: {0} ", Thread. currentThread. managedThreadId); return await Task. run () => {Thread. sleep (1, 5000); Console. writeLine ("'getname' Thread Id: {0}", Thread. currentThread. managedThreadId); return "zhanjindong";}) ;}check whether the Java code "equivalent" is "identical "? Static ExecutorService service = Executors. newFixedThreadPool (10);/*** @ param args * @ throws ExecutionException * @ throws InterruptedException */public static void main (String [] args) throws InterruptedException, ExecutionException {System. out. println ("Main Thread Id:" + Thread. currentThread (). getId (); test ();} static void test () throws InterruptedException, ExecutionException {System. out. println ("Before calling getName, Thread Id:" + Thread. currentThread (). getId (); Future name = getName (); System. out. println ("End calling getName. "); System. out. println ("Get result from getName:" + name. get ();} static Future getName () {System. out. println ("Before calling ExecutorService. submit, current thread Id is: "+ Thread. currentThread (). getId (); return service. submit (new Callable () {@ Override public String call () throws Exception {Thread. sleep (1, 5000); System. out. println ("'getname' Thread Id:" + Thread. currentThread (). getId (); return "zhanjindong ";}});}

Related Article

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.