Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Thread pooling is the process of creating a collection of threads during the initialization
of a multithreaded application, and then reusing those threads for new tasks as and
when required, instead of creating new threads. Then every process has some fixed
number of threads depending on the amount of memory available, those threads are the
need of the application but we have freedom to increase the number of threads. Every
thread in the pool has a specific given task. The thread returns to the pool and waits for
the next assignment when the given task is completed.
Usually, the thread pool is required when we have number of threads are created to
perform a number of tasks, in this organized in a queue. Typically, we have more tasks
than threads. As soon as a thread completes its task, it will request the next task from
the queue until all tasks have been completed. The thread can then terminate, or sleep
until there are new tasks available.
By calling ThreadPool.QueueUserWorkItem.
The task parallel library provide the task class for enter the thread pool easy. The task
class is the part of .Net Framework 4.0 .if you're familiar with the older constructs,
consider the nongeneric Task class a replacement for ThreadPool.QueueUserWorkItem,
and the generic Task<TResult> a replacement for asynchronous delegates. The newer
constructs are faster, more convenient, and more flexible than the old.
To use the nongeneric Task class, call Task.Factory.StartNew, ing in a delegate of the
target method:
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;
using System;
class Akshay
{
static void Run()
{
Console.WriteLine("Welcome to the C# corner thread pool!");
}
static void Main() // The Task class is in System.Threading.Tasks
{
Task.Factory.StartNew(Run);
Console.Read();
}
}
Output :
C# Multithreading
Multithreading in C# is a process in which multiple threads work simultaneously. It is a
process to achieve multitasking. It saves time because multiple tasks are being executed
at a time. To create multithreaded application in C#, we need to use System.Threding
namespace.
System.Threading Namespace
The System.Threading namespace contains classes and interfaces to provide the facility
of multithreaded programming. It also provides classes to synchronize the thread
resource. A list of commonly used classes are given below:
o Thread
o Mutex
o Timer
o Monitor
o Semaphore
o ThreadLocal
o ThreadPool
o Volatile etc.
The first thread which is created inside a process is called Main thread. It starts first and
ends at last.
1. using System;
2. using System.Threading;
3. public class ThreadExample
4. {
5. public static void Main(string[] args)
6. {
7. Thread t = Thread.CurrentThread;
8. t.Name = "MainThread";
9. Console.WriteLine(t.Name);
10. }
11. }
Output:
MainThread
The Abort() method is used to terminate the thread. It raises ThreadAbortException if
Abort operation is not done.
1. using System;
2. using System.Threading;
3. public class MyThread
4. {
5. public void Thread1()
6. {
7. for (int i = 0; i < 10; i++)
8. {
9. Console.WriteLine(i);
10. Thread.Sleep(200);
11. }
12. }
13. }
14. public class ThreadExample
15. {
16. public static void Main()
17. {
18. Console.WriteLine("Start of Main");
19. MyThread mt = new MyThread();
20. Thread t1 = new Thread(new ThreadStart(mt.Thread1));
21. Thread t2 = new Thread(new ThreadStart(mt.Thread1));
22.
23. t1.Start();
24. t2.Start();
25. try
26. {
27. t1.Abort();
28. t2.Abort();
29. }
30. catch (ThreadAbortException tae)
31. {
32. Console.WriteLine(tae.ToString());
33. }
34. Console.WriteLine("End of Main");
35. }
36. }
Output:
End of Main
ThreadPriority
1. using System;
2. using System.Threading;
3. public class MyThread
4. {
5. public void Thread1()
6. {
7. Thread t = Thread.CurrentThread;
8. Console.WriteLine(t.Name+" is running");
9. }
10. }
11. public class ThreadExample
12. {
13. public static void Main()
14. {
15. MyThread mt = new MyThread();
16. Thread t1 = new Thread(new ThreadStart(mt.Thread1));
17. Thread t2 = new Thread(new ThreadStart(mt.Thread1));
18. Thread t3 = new Thread(new ThreadStart(mt.Thread1));
19. t1.Name = "Player1";
20. t2.Name = "Player2";
21. t3.Name = "Player3";
22. t3.Priority = ThreadPriority.Highest;
23. t2.Priority = ThreadPriority.Normal;
24. t1.Priority = ThreadPriority.Lowest;
25.
26. t1.Start();
27. t2.Start();
28. t3.Start();
29. }
30. }
In C#, to get the current state of the thread, use ThreadState or IsAlive property provided by
the Thread class.
Synatx:
public ThreadState ThreadState{ get; }
OR
public bool IsAlive { get; }
Thread class provides different types of methods to implement the states of the threads.
method is used to temporarily suspend the current execution of the thread for
Sleep()
specified milliseconds, so that other threads can get the chance to start the execution,
or may get the CPU for execution.
Join() method is used to make all the calling thread to wait until the main thread, i.e.
joined thread complete its work.
Abort() method is used to abort the thread.
Suspend() method is called to suspend the thread.
Resume() method is called to resume the suspended thread.
Start() method is used to send a thread into runnable State.
Example:
filter_none
edit
play_arrow
brightness_4
using System;
using System.Threading;
Console.WriteLine("My Thread");
// Main method
{
// Creating instance for
// mythread() method
Console.WriteLine("ThreadState: {0}",
thr1.ThreadState);
// Running state
thr1.Start();
Console.WriteLine("ThreadState: {0}",
thr1.ThreadState);
// thr1 is in suspended state
thr1.Suspend();
Console.WriteLine("ThreadState: {0}",
thr1.ThreadState);
thr1.Resume();
Console.WriteLine("ThreadState: {0}",
thr1.ThreadState);
Output:
ThreadState: Unstarted
ThreadState: Running
ThreadState: SuspendRequested
ThreadState: Running
My Thread
My Thread
Monitor Class
The Monitor Class Provides a mechanism that synchronizes access to objects. The
Monitor class is a collection of static methods that provides access to the monitor
associated with a particular object, which is specified through the method's first
argument. the class provide following method.
In the following example we have three threads which are trying to write a file
(akshay.txt). Basically each thread will wait until the locker object gets released by the
first thread that achieved a lock, before it attempts to write to our file.
using System;
using System.IO;
using System.Threading;
namespace monitorclass
{
class Program
{
static object locker = new object();
static void ThreadMain()
{
Thread.Sleep(800); // Simulate Some work
WriteToFile(); // Access a shared resource / critical section
}
static void WriteToFile()
{
String ThreadName = Thread.CurrentThread.Name;
Console.WriteLine("{0} using C-sharpcorner.com", ThreadName);
Monitor.Enter(locker);
try
{
using (StreamWriter sw = new StreamWriter(@"D:\akshaydata\akshay.txt",
true))
{
sw.WriteLine(ThreadName);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Monitor.Exit(locker);
Console.WriteLine("{0} releasing C-sharpcorner.com", ThreadName);
}
}
static void Main(string[] args)
{
for (int i = 0; i < 3; i++)
{
Thread thread = new Thread(new ThreadStart(ThreadMain));
thread.Name = String.Concat("Thread - ", i);
thread.Start();
}
Console.Read();
}
}
}
Output
C# Thread Synchronization
Synchronization is a technique that allows only one thread to access the resource for the
particular time. No other thread can interrupt until the assigned thread finishes its task.
In multithreading program, threads are allowed to access any resource for the required
execution time. Threads share resources and executes asynchronously. Accessing shared
resources (data) is critical task that sometimes may halt the system. We deal with it by
making threads synchronized.
C# Lock
We can use C# lock keyword to execute program synchronously. It is used to get lock
for the current thread, execute the task and then release the lock. It ensures that other
thread does not interrupt the execution until the execution finish.
Here, we are creating two examples that executes asynchronously and synchronously.
C# Example: Without Synchronization
In this example, we are not using lock. This example executes asynchronously. In other
words, there is context-switching between the threads.
1. using System;
2. using System.Threading;
3. class Printer
4. {
5. public void PrintTable()
6. {
7. for (int i = 1; i <= 10; i++)
8. {
9. Thread.Sleep(100);
10. Console.WriteLine(i);
11. }
12. }
13. }
14. class Program
15. {
16. public static void Main(string[] args)
17. {
18. Printer p = new Printer();
19. Thread t1 = new Thread(new ThreadStart(p.PrintTable));
20. Thread t2 = new Thread(new ThreadStart(p.PrintTable));
21. t1.Start();
22. t2.Start();
23. }
24. }
1. using System;
2. using System.Threading;
3. class Printer
4. {
5. public void PrintTable()
6. {
7. lock (this)
8. {
9. for (int i = 1; i <= 10; i++)
10. {
11. Thread.Sleep(100);
12. Console.WriteLine(i);
13. }
14. }
15. }
16. }
17. class Program
18. {
19. public static void Main(string[] args)
20. {
21. Printer p = new Printer();
22. Thread t1 = new Thread(new ThreadStart(p.PrintTable));
23. Thread t2 = new Thread(new ThreadStart(p.PrintTable));
24. t1.Start();
25. t2.Start();
26. }
27. }