简体   繁体   中英

How to add concurrency to synchronous program by C# async/await

I want to learn how to add concurrency to some synchronous program, and take Fibonacci algorithm as example. I wrote these code, but I found that it doesn't have any concurrency at all. All the code running in a single thread until it finish. Could anybody can explain to me why it doesn't reflect the async?

    async static Task<int> Fibonacci(int n)
    {
        if (n == 0) { return 0; }
        else if (n == 1) { return 1; }
        else
        {
            var t1 = Fibonacci(n - 1);
            var t2 = Fibonacci(n - 2);
            return (await t1) + (await t2);
        }
    }

    static int Main(string[] args)
    {
        var fib = Fibonacci(25);
        fib.Wait();
        Console.WriteLine( fib.Result );
        Console.ReadKey();
        return 0;
    }

Under Michael's prompt, I try create Task in the async function, and it works. But I noticed that an async function returns a Task type value, it is the same as Task.Run(). Both of the tasks will run immediately, but the t1 will not run into a new thread automatically. So could anyone to tell me, what's the different between these two tasks. Can I make the async function run into new thread automatically?

    async static Task<string> Async1()
    {
        return DateTime.Now.ToString();
    }
    static void Main(string[] args)
    {
        Task<string> t1 = Async1();
        Task<string> t2 = Task.Run<string>(() => { return DateTime.Now.ToString(); });
    }

First, you're using an experimental CTP for the async/await model of .NET 4.5. You should expect things to behave awkwardly in such an environment.

Second, are you aware that you're sleeping indefinitely at the end?

Third, your application is essentially synchronous. You make an asynchronous task and then wait for it immediately afterward. The async/await model does not add concurrency, only asynchrony. It simply allows your application to be responsive while doing multiple things; it does not automatically parallelize. This is why your applications is acting as if it's synchronous.

There are two reasons for this behavior.

First, async methods run synchronously until they must evaluate an await statement. You are recursing before awaiting, so all of the recursion will be finished synchronously before anything is awaited.

Second, if the awaited task is already completed then they await may run synchronously. Your code never creates a task that isn't already completed. It either synchronously wraps and returns 0 or 1 in a task or synchronously adds two tasks that were completed synchronously and returns that result.

You need to introduce a non-synchronous aspect somewhere. For example:

async static Task<int> Fibonacci(int n)
{
    if (n == 0) { return 0; }
    else if (n == 1) { return 1; }
    else
    {
        // run one of the recursions concurrently
        var t1 = Task.Factory.StartNew(() => Fibonacci(n - 1));
        var t2 = Fibonacci(n - 2);
        return (await (await t1)) + (await t2);
    }
}

You are not starting tasks on the thread-pool at all. await does not introduce parallelism by default.

Change it to:

        var t1 = Task.Factory.StartNew(() => Fibonacci(n - 1)).Unwrap();
        var t2 = Task.Factory.StartNew(() => Fibonacci(n - 2)).Unwrap();

This is not as efficient as it can get, but it will get you started.

    private async Task<int> ComputeFibAsync(int n)
    {
        return await Task.Run(
            async () =>
            {
                if (n < 2)
                {
                    return 1;
                }
                else
                {
                    var result = await Task.WhenAll<int>(ComputeFibAsync(n - 1), ComputeFibAsync(n - 2));
                    return result[0] + result[1];
                }
            }
            );
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM