Reader small image

You're reading from  Parallel Programming and Concurrency with C# 10 and .NET 6

Product typeBook
Published inAug 2022
PublisherPackt
ISBN-139781803243672
Edition1st Edition
Right arrow
Author (1)
Alvin Ashcraft
Alvin Ashcraft
author image
Alvin Ashcraft

Alvin Ashcraft is a software engineer and developer community champion with over 25 years of experience in software development. Working primarily with Microsoft Windows, web, and cloud technologies, his career has focused primarily on the healthcare industry. He has been awarded as a Microsoft MVP 11 times, most recently as a Windows Dev MVP. Alvin works in the Philadelphia area for Allscripts, a global healthcare software company, as a principal software engineer. He is also a board member of the TechBash Foundation, where he helps organize the annual TechBash developer conference. He has previously worked for companies such as Oracle, Genzeon, CSC, and ITG Pathfinders. Originally from the Allentown, PA area, Alvin currently resides in West Grove, PA with his wife and three daughters.
Read more about Alvin Ashcraft

Right arrow

Assessments

This section contains answers to questions from all chapters.

Chapter 1, Managed Threading Concepts 

  1. A managed thread is a thread that is created in .NET-managed code with the System.Threading.Thread object. 
  2. Set the Thread.IsBackground property to true before calling Thread.Start()
  3. .NET will throw a ThreadStateException exception. 
  4. .NET prioritizes managed threads mostly based on their Thread.Priority value. 
  5. ThreadPriority.Highest.
  6. Thread.Abort() is not supported by .NET 6. The code will not compile. 
  7. Add an object parameter to the method to be started by the new thread, and pass the data when calling Thread.Start(data)
  8. Pass the delegate to the cancellation token’s Register method. 

Chapter 2, Evolution of Multithreaded Programming 
in .NET 

  1. ThreadPool 
  2. C# 5.0 
  3. .NET Framework 4.5 
  4. .NET Core 3.0 
  5. Task, Task<T>, ValueTask, or ValueTask<T> 
  6. ConcurrentDictionary<TKey, TValue> 
  7. BlockingCollection<T> 
  8. Parallel LINQ (PLINQ

Chapter 3, Best Practices for Managed Threading 

  1. Singleton.
  2. ThreadStatic
  3. A deadlock occurs when multiple threads are all waiting to access a locked resource and cannot proceed. 
  4. Monitor.TryEnter.
  5. Interlocked.
  6. Interlocked.Add.
  7. MaxDegreeOfParallelism.
  8. Use the WithDegreeOfParallelism extension method.
  9. ThreadPool.GetMinThreads().

Chapter 4, User Interface Responsiveness and Threading 

  1. Task or Task<T>.
  2. Task.WhenAll.
  3. Task.Factory.StartNew.
  4. A background thread on ThreadPool.
  5. Application.Current.Dispatcher.Invoke.
  6. this.BeginInvoke.
  7. Check the this.InvokeRequired property.

Chapter 5, Asynchronous Programming with C# 

  1. Task.Result.
  2. Task.WhenAll().
  3. Task.WaitAll().
  4. Task, Task<TResult>, ValueTask, or ValueTask<TResult>.
  5. I/O-bound operations such as a file or network access are best suited for async methods. 
  6. False. It is a best practice to always suffix async methods with Async
  7. Task.Run.

Chapter 6, Parallel Programming Concepts 

  1. Parallel.For.
  2. Parallel.ForEachAsync
  3. Parallel.Invoke
  4. TaskCreationOptions.AttachToParent
  5. TaskCreationOptions.DenyAttach
  6. Task.Run will always deny child tasks from attaching. Also, Task.Run has no overloaded methods to provide TaskCreationOptions
  7. No, regular for and foreach loops can be faster if each loop iteration is fast-running and/or there are only a few iterations of the loop.

Chapter 7, Task Parallel Library (TPL) and Dataflow

The following are the answers to this chapter’s questions:

  1. JoinBlock.
  2. BufferBlock is a propagator block.
  3. BufferBlock.
  4. JoinTo().
  5. Complete().
  6. SendAsync().
  7. ReceiveAsync().

Chapter 8, Parallel Data Structures and Parallel LINQ

  1. AsParallel()
  2. AsSequential()
  3. AsOrdered()
  4. ForAll().
  5. AsOrdered() can significantly decrease performance for a query. 
  6. OrderBy and OrderByDescending. They will default to ParallelMergeOptions.FullyBuffered
  7. No. PLINQ has additional overhead that can cause queries on smaller datasets or simple queries to be slower. 
  8. ParallelMergeOptions.NotBuffered.

Chapter 9, Working with Concurrent Collections in .NET

  1. BlockingCollection<T>
  2. ConcurrentQueue<T>
  3. BlockingCollection<T>
  4. ConcurrentDictionary<TKey, TValue>
  5. Enqueue()
  6. TryAdd() and TryGetValue()
  7. No. Always add your own synchronization mechanisms when using extension methods with concurrent collections, including standard LINQ operators. 

Chapter 10, Debugging Multithreaded Applications with Visual Studio 

  1. Use the Attach to Process window or set multiple startup projects in the solution file. 
  2. They are grouped by process. 
  3. Right-click in the window and select Columns
  4. The Parallel Stacks window. 
  5. .PNG files. 
  6. Four. 
  7. The Debug Location toolbar. 
  8. Click the Flag Just My Code button. 

Chapter 11, Canceling Asynchronous Work 

  1. CancellationToken.IsCancellationRequested 
  2. CancellationTokenSource 
  3. OperationCanceledException 
  4. Register callback 
  5. ManualResetEventSlim 
  6. ManualResetEventSlim.Reset 
  7. CancellationTokenSource.CreateLinkedTokenSource 

Chapter 12, Unit Testing Async, Concurrent, and Parallel Code

  1. Fact 
  2. SpinLock.WaitUntil 
  3. AggregateException 
  4. Exception 
  5. Assert.NotNull 
  6. Test Explorer 
  7. MSTest, NUnit, and xUnit .NET 
  8. ReSharper, Rider, and the dotMemory Unit standalone console runner 

Why subscribe?

  • Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
  • Improve your learning with Skill Plans built especially for you
  • Get a free eBook or video every month
  • Fully searchable for easy access to vital information
  • Copy and paste, print, and bookmark content

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at customercare@packtpub.com for more details.

At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Parallel Programming and Concurrency with C# 10 and .NET 6
Published in: Aug 2022Publisher: PacktISBN-13: 9781803243672
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Alvin Ashcraft

Alvin Ashcraft is a software engineer and developer community champion with over 25 years of experience in software development. Working primarily with Microsoft Windows, web, and cloud technologies, his career has focused primarily on the healthcare industry. He has been awarded as a Microsoft MVP 11 times, most recently as a Windows Dev MVP. Alvin works in the Philadelphia area for Allscripts, a global healthcare software company, as a principal software engineer. He is also a board member of the TechBash Foundation, where he helps organize the annual TechBash developer conference. He has previously worked for companies such as Oracle, Genzeon, CSC, and ITG Pathfinders. Originally from the Allentown, PA area, Alvin currently resides in West Grove, PA with his wife and three daughters.
Read more about Alvin Ashcraft