Parallelism vs Async / Non- Blocking (Introduction)

When to use threads vs async?

Threads vs cores in CPU

Threads in parallelism

using System.Threading;
Thread newWorkerThread = new Thread(() => SyncDataToCloud());
newWorkerThread.Start();

Multi threading Vs Performance

C# Tasks Parallel Library (TPL)

using System.Threading.Tasks;
Task.Run(() => SyncDataToCloud());
ThreadPool.SetMaxThreads(4, 4);
ThreadPool.SetMinThreads(1, 1);

Threads Safety

int bankBalance;void CreditAccount(int amount)
{
bankBalance = bankBalance + amount;
saveBalance(bankBalance);
}
void DebitAccount(int amount)
{
bankBalance = bankBalance - amount;
saveBalance(bankBalance);
}
void saveBalance(int balance)
{}
Task.Run(() => CreditAccount(1000));//Below statement can be trigger from a different eventTask.Run(() => DebitAccount(1000));

How to use a lock

static readonly object _object = new object();void CreditAccount(int amount)
{
lock (_object)
{
bankBalance = bankBalance + amount;
saveBalance(bankBalance);
}}void DebitAccount(int amount)
{
lock (_object)
{
bankBalance = bankBalance - amount;
saveBalance(bankBalance);
}}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store