Skip to content

Instantly share code, notes, and snippets.

@b2977053
Last active December 19, 2019 08:47
Show Gist options
  • Save b2977053/c57ce62b77ea3955bf42de021106c467 to your computer and use it in GitHub Desktop.
Save b2977053/c57ce62b77ea3955bf42de021106c467 to your computer and use it in GitHub Desktop.
internal class 使用非同步製作Pizza: 使用同步製作Pizza
{
private static int Pizza總數 = 0;
private static object tsLock = new object();
internal void 開始()
{
var watch = Stopwatch.StartNew();
Console.WriteLine("開始進行製作披薩...");
// 製作10個披薩 120 時間
List<Task> Tasks = new List<Task>();
for (int i = 0; i < 10; i++)
{
Task a = Task.Run(async () =>
{
製作一個披薩();
});
Tasks.Add(a);
}
Task.WaitAll(Tasks.ToArray());
watch.Stop();
Console.WriteLine($"\n\nPizza總數:{Pizza總數}\n共花費:{watch.Elapsed.TotalMilliseconds.ToString("0")} 時間。");
}
private static void 製作一個披薩()
{
//揉麵團();
揉麵團Async();
//發酵();
發酵Async();
桿面皮();
放上佐料();
//烤披薩();
烤披薩Async();
lock (tsLock)
{
Pizza總數++;
}
}
static async void 揉麵團Async()
{
await Task.Run(() =>
{
string status = "揉麵團中...";
Thread.Sleep(30);
status = "揉麵團 完成";
});
}
static async void 發酵Async()
{
await Task.Run(() =>
{
string status = "發酵中...";
Thread.Sleep(40);
status = "發酵 完成";
});
}
static async void 桿面皮Async()
{
await Task.Run(() =>
{
string status = "桿面皮中...";
Thread.Sleep(2);
status = "桿面皮 完成";
});
}
static async void 烤披薩Async()
{
await Task.Run(() =>
{
string status = "烤披薩中...";
Thread.Sleep(20);
status = "烤披薩 完成";
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment