小编Kak*_*ira的帖子

如何操作数组以获得最大数量?

假设你有一个正整数数组,操纵它们,以便结果数组的整数串联是可能的最大数.例如:{9,1,95,17,5},结果:9955171

家庭作业警察:这是一个谷歌电话采访问题,并没有签署任何NDAs;).

arrays algorithm

33
推荐指数
3
解决办法
1万
查看次数

使用任务重试异步功能-哪种方法更有效?

我想知道哪种方法通常在内存和资源使用方面会更有效。

尤其是方法1,我很难想象将如何创建任务对象和线程旋转?有人可以解释一下幕后发生的事情吗?

如果两者之间没有区别,我想使用#1(以免引起异步气泡)。对于#2,我知道编译器将在下面生成状态机并产生收益。OTOH,#1在概念上似乎是递归的,但是在传统意义上会像在一个堆栈帧中等待另一个一样递归吗?

方法1:

internal static Task ExecuteAsyncWithRetry(Func<Task> methodToExecute, Func<bool> shouldRetry)
    {
        var tcs = new TaskCompletionSource<object>();

        try
        {
            return methodToExecute().ContinueWith<Task>((t) =>
            {
                if (t.IsFaulted || t.IsCanceled)
                {
                    if (shouldRetry())
                    {
                        return ExecuteAsyncWithRetry(methodToExecute, shouldRetry);
                    }
                    else
                    {
                        tcs.SetException(t.Exception);
                    }
                }
                else
                {
                    tcs.SetResult(null);

                }

                return tcs.Task;
            }, TaskContinuationOptions.ExecuteSynchronously).Unwrap();
        }
        catch(Exception ex)
        {
            tcs.SetException(ex);
        }

        return tcs.Task;
    }
Run Code Online (Sandbox Code Playgroud)

方法2(忽略两者之间异常传播的差异):

internal static async Task ExecuteWithRetry(Func<Task> methodToExecute, Func<bool> shouldRetry)
    {
        while (true)
        {
            try
            {
                await methodToExecute();
            }
            catch(Exception ex)
            {
                if(!shouldRetry())
                { …
Run Code Online (Sandbox Code Playgroud)

c# asynchronous task-parallel-library async-await

5
推荐指数
1
解决办法
2548
查看次数