-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
353 lines (291 loc) · 12.7 KB
/
Copy pathProgram.cs
File metadata and controls
353 lines (291 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace AsyncMorningRoutineApp
{
class Program
{
//##########################################################
//STEP 1 - Initial setup
///// <summary>
///// Normal Synchronously executed Main
///// </summary>
///// <param name="args"></param>
//static void Main(string[] args)
//{
// Coffee cup = PourCoffee();
// Console.WriteLine("coffee is ready");
// Egg eggs = FryEggs(2);
// Console.WriteLine("eggs are ready");
// Bacon bacon = FryBacon(3);
// Console.WriteLine("bacon is ready");
// Toast toast = ToastBread(2);
// ApplyButter(toast);
// ApplyJam(toast);
// Console.WriteLine("toast is ready");
// Juice oj = PourOj();
// Console.WriteLine("oj is ready");
// Console.WriteLine("Breakfast is ready!");
//}
//##########################################################
//##########################################################
//STEP 2 - Make main responsive
/// <summary>
/// The main thread is responsive but the tasks are executed sequentially.
/// This code doesn't block while the eggs or the bacon are cooking.
/// This code won't start any other tasks though.
/// You'd still put the toast in the toaster and stare at it until it pops.
/// But at least, you'd respond to anyone that wanted your attention.
/// In a restaurant where multiple orders are placed,
/// the cook could start another breakfast while the first is cooking.
/// added await keywords before method and took the .Await()
/// off Task.Delay(3000).Await() in the fry methods
/// </summary>
/// <param name="args"></param>
/// <returns></returns>
//static async Task Main(string[] args)
//{
// numberOfBreakfastMade += 1;
// Coffee cup = PourCoffee();
// Console.WriteLine("coffee is ready");
// Egg eggs = await FryEggsAsync(2);
// Console.WriteLine("eggs are ready");
// Bacon bacon = await FryBaconAsync(3);
// Console.WriteLine("bacon is ready");
// Toast toast = await ToastBreadAsync(2);
// ApplyButter(toast);
// ApplyJam(toast);
// Console.WriteLine("toast is ready");
// Juice oj = PourOj();
// Console.WriteLine("oj is ready");
// Console.WriteLine($"Breakfast {numberOfBreakfastMade} ready!");
//}
//##########################################################
////##########################################################
////STEP 3 - Transform Main to execute the task parallel by storing the tasks when they start instead of awaiting them
////You start all the asynchronous tasks at once. You await each task only when you need the results.
//static async Task Main(string[] args)
//{
// Stopwatch watch = new Stopwatch();
// watch.Start();
// numberOfBreakfastMade += 1;
// Coffee cup = PourCoffee();
// Console.WriteLine("coffee is ready");
// Task<Egg> eggsTask = FryEggsAsync(2);
// Task<Bacon> baconTask = FryBaconAsync(3);
// Task<Toast> toastTask = ToastBreadAsync(2);
// //if any work in the toast making process is async then the whole process is async
// Toast toast = await toastTask;
// ApplyButter(toast);
// ApplyJam(toast);
// Console.WriteLine("toast is ready");
// Juice oj = PourOj();
// Console.WriteLine("oj is ready");
// Egg eggs = await eggsTask;
// Console.WriteLine("eggs are ready");
// Bacon bacon = await baconTask;
// Console.WriteLine("bacon is ready");
// Console.WriteLine("Breakfast is ready!");
// watch.Stop();
// Console.WriteLine(watch.Elapsed.TotalSeconds);
//}
////##########################################################
////##########################################################
////STEP 4 - extracting toast task to compose a method of toast related stuff
//static async Task Main(string[] args)
//{
// Stopwatch watch = new Stopwatch();
// watch.Start();
// Coffee cup = PourCoffee();
// Console.WriteLine("coffee is ready");
// Task<Egg> eggsTask = FryEggsAsync(2);
// Task<Bacon> baconTask = FryBaconAsync(3);
// Task<Toast> toastTask = ToastWithButterAndJamToastBreadAsync(2);
// Juice oj = PourOj();
// Console.WriteLine("oj is ready");
// Toast toast = await toastTask;
// Console.WriteLine("toast is ready");
// Egg eggs = await eggsTask;
// Console.WriteLine("eggs are ready");
// Bacon bacon = await baconTask;
// Console.WriteLine("bacon is ready");
// Console.WriteLine("Breakfast is ready!");
// watch.Stop();
// Console.WriteLine(watch.Elapsed.TotalSeconds);
//}
////##########################################################
//##########################################################
//STEP 5a - Task.WhenAll(IEnumerable<Task> task) - Creates a task that will complete when all of the System.Threading.Tasks.Task objects in an array have completed.
//static async Task Main(string[] args)
//{
// Coffee cup = PourCoffee();
// Console.WriteLine("coffee is ready");
// Task<Egg> eggsTask = FryEggsAsync(2);
// Task<Bacon> baconTask = FryBaconAsync(3);
// Task<Toast> toastTask = ToastWithButterAndJamToastBreadAsync(2);
// Juice oj = PourOj();
// Console.WriteLine("oj is ready");
// await Task.WhenAll(eggsTask, baconTask, toastTask);
// Console.WriteLine("Breakfast is ready!");
//}
//##########################################################
//##########################################################
//STEP 5a - Task.WhenAny()
// 5b - Task.WhenAll(IEnumerable<Task> task) - Creates a task that will complete when all of the System.Threading.Tasks.Task objects in an array have completed.
static async Task Main(string[] args)
{
await BreakFast_WhenAny();
await BreakFast_WhenAll();
}
//##########################################################
private static async Task BreakFast_WhenAll()
{
Coffee cup = PourCoffee();
Console.WriteLine("coffee is ready");
Task<Egg> eggsTask = FryEggsAsync(2);
Task<Bacon> baconTask = FryBaconAsync(3);
Task<Toast> toastTask = ToastWithButterAndJamToastBreadAsync(2);
Juice oj = PourOj();
Console.WriteLine("oj is ready");
await Task.WhenAll(eggsTask, baconTask, toastTask);
Console.WriteLine("Breakfast is ready!");
}
private static async Task BreakFast_WhenAny()
{
Coffee cup = PourCoffee();
Console.WriteLine("coffee is ready");
Task<Egg> eggsTask = FryEggsAsync(2);
Task<Bacon> baconTask = FryBaconAsync(3);
Task<Toast> toastTask = ToastWithButterAndJamToastBreadAsync(2);
Juice oj = PourOj();
Console.WriteLine("oj is ready");
List<Task> breakFastTask = new List<Task> {eggsTask, baconTask, toastTask};
while (breakFastTask.Count > 0)
{
Task finishedTask = await Task.WhenAny(breakFastTask);
if (finishedTask == eggsTask)
{
Console.WriteLine("egg is ready");
}
if (finishedTask == baconTask)
{
Console.WriteLine("bacon is ready");
}
if (finishedTask == toastTask)
{
Console.WriteLine("toast is ready");
}
breakFastTask.Remove(finishedTask);
}
Console.WriteLine("Breakfast is ready!");
}
//##########################################################
private static async Task<Toast> ToastWithButterAndJamToastBreadAsync(int toasts)
{
//if any work in the toast making process is async then the whole process is async
Toast toast = await ToastBreadAsync(toasts);
ApplyButter(toast);
ApplyJam(toast);
return toast;
}
private static async Task<Egg> FryEggsAsync(int i)
{
Console.WriteLine("Warming the egg pan...");
await Task.Delay(3000);
Console.WriteLine($"cracking {i} eggs");
Console.WriteLine("cooking the eggs ...");
await Task.Delay(3000);
Console.WriteLine("Put eggs on plate");
return new Egg();
}
private static async Task<Bacon> FryBaconAsync(int i)
{
Console.WriteLine($"putting {i} slices of bacon in the pan");
Console.WriteLine("cooking first side of bacon...");
await Task.Delay(3000);
for (int slice = 0; slice < i; slice++)
{
Console.WriteLine("flipping a slice of bacon");
}
Console.WriteLine("cooking the second side of bacon...");
await Task.Delay(3000);
Console.WriteLine("Put bacon on plate");
return new Bacon();
}
private static async Task<Toast> ToastBreadAsync(int i)
{
for (int slice = 0; slice < i; slice++)
{
Console.WriteLine("Putting a slice of bread in the toaster");
}
Console.WriteLine("Start toasting...");
await Task.Delay(3000);
Console.WriteLine("Remove toast from toaster");
return new Toast();
}
private static Juice PourOj()
{
Console.WriteLine("Pouring orange juice");
return new Juice();
}
private static void ApplyJam(Toast toast) =>
Console.WriteLine("Putting jam on the toast");
private static void ApplyButter(Toast toast) =>
Console.WriteLine("Putting butter on the toast");
//private static Toast ToastBread(int slices)
//{
// for (int slice = 0; slice < slices; slice++)
// {
// Console.WriteLine("Putting a slice of bread in the toaster");
// }
// Console.WriteLine("Start toasting...");
// Task.Delay(3000).Wait();
// Console.WriteLine("Remove toast from toaster");
// return new Toast();
//}
//private static Bacon FryBacon(int slices)
//{
// Console.WriteLine($"putting {slices} slices of bacon in the pan");
// Console.WriteLine("cooking first side of bacon...");
// Task.Delay(3000).Wait();
// for (int slice = 0; slice < slices; slice++)
// {
// Console.WriteLine("flipping a slice of bacon");
// }
// Console.WriteLine("cooking the second side of bacon...");
// Task.Delay(3000).Wait();
// Console.WriteLine("Put bacon on plate");
// return new Bacon();
//}
//private static Egg FryEggs(int howMany)
//{
// Console.WriteLine("Warming the egg pan...");
// Task.Delay(3000).Wait();
// Console.WriteLine($"cracking {howMany} eggs");
// Console.WriteLine("cooking the eggs ...");
// Task.Delay(3000).Wait();
// Console.WriteLine("Put eggs on plate");
// return new Egg();
//}
private static Coffee PourCoffee()
{
Console.WriteLine("Pouring coffee");
return new Coffee();
}
}
internal class Juice
{
}
internal class Toast
{
}
internal class Bacon
{
}
internal class Egg
{
}
internal class Coffee
{
}
}