Public async Task
{
The Debug. WriteLine ($" index: {testindex} ");
Thread.sleep (1000 * 3);//stirng ret=await XXX. XXX (XXX);
Return testindex + "finished";
}
Through the following method call
Var task1=TestAsync (1);
Var task2=TestAsync (2);
Var task3=TestAsync (3);
Await Task. WhenAll (task1, task2, task3);
Looking forward to is the test of multithreading running three at the same time, but the actual tests are found one by one,,
CodePudding user response:
Well, you don't decide, this is the schrodinger's catHow do you judge, you learn from what they done,
Only opened it the moment you know, opened it, even if they are done at the same time, but your output is always a an output right,
In addition you want to test the actual to something meaningful, because of the consumption of meaningful CPU is not exactly the same, so the output has the difference,
Ps: please don't use what sleep, as we have said many times, this is not the blog garden is a great god tell you what the async thread, async is asynchronous, you sleep is blocking the main thread, this test at the same time you also didn't open threads, so running at the same time also don't you think
CodePudding user response:
I wrote three methods, and your own understanding, on the whole garden, the great god that it's just explain those blog forget type one-way output way was the biggest problem is the celestial neter,Said async/await is asynchronous I/o again, the task is the thread pool, each theory, broad and gods to forget to tell you these, must use asynchronous as threads, thread as asynchronous, we didn't also the way
public static async TaskTestAsync (int testindex)
{
Console. WriteLine ($" index: {testindex} ");
Thread.sleep (1000 * 4 - testindex ());//in order to test, I'm going to reverse the output, but the actual positive sequence you will find that he is, you this kind of writing isn't any thread
Console. WriteLine ($index: {testindex} "end");
Return testindex + "finished";
}
Public static async TaskTestAsync1 (int testindex)
{
Console. WriteLine ($" index: {testindex} ");
Await Task. Delay (1000 * 4 - testindex ());//this method can achieve reverse output, because here the task is really opened a thread, and is waiting for the end of this thread
Console. WriteLine ($index: {testindex} "end");
Return testindex + "finished";
}
Public static TaskTestAsync3 (int testindex)
{
Return TaskThe Run (()=& gt;
{
Console. WriteLine ($" index: {testindex} ");
Thread.sleep (1000 * 4 - testindex ());//it can also be output in reverse chronological order, for this task. The run is really opened a thread, so now you sleep is the true way of making auxiliary thread dormancy, rather than let the main thread to sleep
Console. WriteLine ($index: {testindex} "end");
Return testindex + "finished";
});
}
CodePudding user response:
In another to add a method, also is the great gods article is hard to explain, because is the fundamental problem in front of the "async is asynchronous, the task is thread" the fundamental things to selective forget, they no matter how many articles are written the article "invalid output," once you know the fundamental things, you know in an instant (of course, you know in an instant, the cheat god deceives ghost articles will not sell the )public static async TaskTestAsync4 (int testindex)
{
Await Task. The Yield ();//can also reverse, or in front those who say, produced a real thread here, although he gave over thread first, but he was really created a thread
Console. WriteLine (${testindex} "id {Thread. CurrentThread. ManagedThreadId}");
Console. WriteLine ($" index: {testindex} ");
Thread.sleep (1000 * 4 - testindex ());
Console. WriteLine ($index: {testindex} "end");
Return testindex + "finished";
}