C#的Task和Java的Future

时间:2024-01-19 14:05:14

C#的Task和Java的Future

自从项目中语言换成Java后就很久没有看C#了,但说实话我是身在曹营心在汉啊。早就知道.NET4.5新增了asyncawait但一直没有用过,今天看到这篇文章总算有了点了解,突然发现Task这个玩意不就是Java中Future这个概念吗?

这里冒昧引用下Jesse Liu文中的C#代码:

  1. staticvoidMain(string[] args)
  2. {
  3. Console.WriteLine("Main Thread Id: {0}\r\n",Thread.CurrentThread.ManagedThreadId);
  4. Test();
  5. Console.ReadLine();
  6. }
  7. static async TaskTest()
  8. {
  9. Console.WriteLine("Before calling GetName, Thread Id: {0}\r\n",Thread.CurrentThread.ManagedThreadId);
  10. var name =GetName();
  11. Console.WriteLine("End calling GetName.\r\n");
  12. Console.WriteLine("Get result from GetName: {0}", await name);
  13. }
  14. static async TaskGetName()
  15. {
  16. // 这里还是主线程
  17. Console.WriteLine("Before calling Task.Run, current thread Id is: {0}",Thread.CurrentThread.ManagedThreadId);
  18. return await Task.Run(()=>
  19. {
  20. Thread.Sleep(5000);
  21. Console.WriteLine("'GetName' Thread Id: {0}",Thread.CurrentThread.ManagedThreadId);
  22. return"zhanjindong";
  23. });
  24. }

大家看下"等价"的Java代码是不是“一模一样”?

  1. staticExecutorService service =Executors.newFixedThreadPool(10);
  2. /**
  3. * @param args
  4. * @throws ExecutionException
  5. * @throws InterruptedException
  6. */
  7. publicstaticvoid main(String[] args)throwsInterruptedException,ExecutionException{
  8. System.out.println("Main Thread Id: "+Thread.currentThread().getId());
  9. test();
  10. }
  11. staticvoid test()throwsInterruptedException,ExecutionException{
  12. System.out.println("Before calling getName, Thread Id: "+Thread.currentThread().getId());
  13. Future name = getName();
  14. System.out.println("End calling getName.");
  15. System.out.println("Get result from getName: "+ name.get());
  16. }
  17. staticFuture getName(){
  18. System.out.println("Before calling ExecutorService.submit, current thread Id is: "+Thread.currentThread().getId());
  19. return service.submit(newCallable(){
  20. @Override
  21. publicString call()throwsException{
  22. Thread.sleep(5000);
  23. System.out.println("'getName' Thread Id: "+Thread.currentThread().getId());
  24. return"zhanjindong";
  25. }
  26. });
  27. }

当然上面的代码为了说明都冗余了点,输出的结果自然也是一样的:

C#的Task和Java的Future C#的Task和Java的Future

说“等价”是因为无论是Task还是Future都是为了对异步操作进行封装,Java中Future.get相当于C#中的Task.Resultawait name不过是一个语法糖而已(但这语法糖很重要,async和await能让我们以写同步代码的方式实现异步的逻辑)。做一件正确的事的思路往往是一样的,但是实现的细节总是有差别。这次我倒似乎更喜欢Java中Future这个名词,直观明了。但C#还是一如既往的讳莫如深(不过已经进步很大了),这一点也是经常遭业界同仁所诟病,还好我有IL:

C#的Task和Java的Future

咦?状态机让我立马想到了yield return,怪不得我觉得awaityield return有点神似呢。这也许就是我更喜欢C#的原因:设计上的一致性

简单写点,有时间还是想深入深入啊。