可扩展性与响应性 [英] Scalability versus responsiveness

查看:86
本文介绍了可扩展性与响应性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

书籍考试参考文献70-483 C#编程,Wouter de Kort,O'Reilly,2013

Book Exam Ref 70-483 Programming in C#, Wouter de Kort, O´Reilly, 2013

autor想说这个例子和描述是什么?

SleepAsyncA方法在休眠时使用线程池中的线程。然而,第二种方法具有完全不同的实现,在等待定时器运行时不占用线程。第二种方法为您提供了可伸缩性。

The SleepAsyncA method uses a thread from the thread pool while sleeping. The second method, however, which has a completely different implementation, does not occupy a thread while waiting for the timer to run. The second method gives you scalability.

public static Task SleepAsyncA(int millisecondsTimeout)

        {

            return Task.Run(()=> Thread.Sleep(millisecondsTimeout));

        }


  public static Task SleepAsyncB(int millisecondsTimeout)

        {            

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; TaskCompletionSource< BOOL> tcs = null;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; var t = new Timer(delegate {tcs.TrySetResult(true);},null,-1,-1);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; tcs = new TaskCompletionSource< bool>(t);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; t.Change(millisecondsTimeout,-1);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; return tcs.Task;

  &NBSP; &NBSP; &NBSP; }

public static Task SleepAsyncA(int millisecondsTimeout)
        {
            return Task.Run(() => Thread.Sleep(millisecondsTimeout));
        }

 public static Task SleepAsyncB(int millisecondsTimeout)
        {            
            TaskCompletionSource<bool> tcs = null;
            var t = new Timer(delegate { tcs.TrySetResult(true); }, null, -1, -1);
            tcs = new TaskCompletionSource<bool>(t);
            t.Change(millisecondsTimeout, -1);
            return tcs.Task;
        }

这个2 metod有什么不同?

我可以用什么更实际的例子来理解本质?


推荐答案

一个代码行有更多代码,方法必须实例化一个对象。另一个没有。如果他们都在做同样的事情,那么由开发人员自行决定使用哪一个。 
One has more lines of code with the method having to instance an object. The other one doesn't. If they are both doing the same thing, then it's at the developer's discretion as to which one to use. 


这篇关于可扩展性与响应性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆