top of page

C#

Public·1 member

Thread


A thread and a task are related terms/concepts in programming.


The meaning of a task in a very literal sense is as explained below.

A task means a part of our code, and generally speaking, we would have many tasks to do before our code runs to completion.

We can execute a task or run a thread, and both would end up executing some part of our code. Also, note that a thread could sequentially execute many tasks if only a single thread is running.



What is a Task in a programmatic sense?

A Task is a programming object that represents a method/code in our C# or VB.NET code that can be run separately from other parts of the code. We call the Run method on a Task object to run the code that is represented by a Task.

The task object is a new kid on the block, and it wasn't there in the early days of programming in C# and VB.NET.

The namespace System.Threading.Tasks includes all classes we would need for writing code that uses task objects.

Calling a web service, sending an email, getting the contents of a file, getting some data from a database or running some CPU-intensive calculations are all tasks.

The Run method on a Task object always uses a thread pool thread rather than creating a new thread.


What is a Thread in a programmatic sense?

A Thread is a programming object that represents a method/code, and it's the smallest unit of code execution. We call the Start method on a Thread object to run a method. Any code that executes must run on a Thread object.

The namespace System.Threading includes all classes we would need for writing code that uses thread objects.

The thread object was always there in C# and VB.NET since the early times. We can have different parts of code execute on different threads (i.e. the code executes as multiple tasks) or just as a single thread (i.e. code executes as a single task).

Each thread is allocated resources like CPU, memory, time, etc by the operating system.


What is concurrency?

Concurrency is a concept that means running more than one thread at the same time or in a time-sliced manner. A time-sliced manner, meaning the operating system gives a certain time to each thread and keeps context-switching between threads, which gives the illusion of running many threads at the same time, when actually only one thread at a time is running on a single core processor.

Concurrency makes the application responsive (i.e. application is not frozen), and it's not meant to increase performance.

Also, concurrency can happen on a single or multi-core processor system.

The popular async-await pattern in newer .NET is an example of concurrency. Even executing a task on a background thread, meaning a thread pool thread, is an example of concurrency.

What is parallelism?

Parallelism is a concept that means multiple threads run at the same time as opposed to sequentially or in a time-sliced manner, and it's only possible on multi-core processor systems.

Parallelism is a subset of concurrency.

We can call static method on Parallel class to implement parallelism under the name namespace System.Threading.Tasks. These static methods are ForEach and For that execute code in repeatedly manner.

Parallelism makes the application better in performance and that is its goal.

Can two threads of software code execute simultaneously?

Yes, if there are multiple cores available for processing. It cannot happen on a single-core processor system.


23 Views
bottom of page