Saturday, June 6, 2015

Multi-Threading

MULTITHREADING

Multithreading allows an application to divide tasks such that they work independently of each other to make the most efficient use of the processor and the user’s time. Threading is not always the right choice for all applications as they sometimes slows down the processing speed of the application.

Thread

Every application runs with at least one thread. A thread is nothing more than a process. On the computer, a thread is a process moving through time. The process performs sets of sequential steps, each step executing a line of code. The steps are sequential and take a given amount of time. The time it takes to complete a series of steps is the sum of the time it takes to perform each programming step.

Multithreading applications

For a long time, most programming applications were single threaded. That means there was only thread in the entire application. You could never do computation A until completing computation B. A multithreaded application allows you to run several threads, each thread running in its own process. So theoretically you can run computation A in one thread, and at the same time Computation B in another thread. At the same time you could run computation C, D in their own threads. Hence computation A, B, C and D would run concurrently. Theoretically, if all four computations took about the same time, you could finish you program in a quarter of the time it takes to run a single thread. 

ThreadStart() and Thread() class

using System.Threading;
public class dummy
{
public static void prt()
{
System.Console.WriteLine("Hi");
}
}
public class thread
{
public static void Main()
{
ThreadStart ts = new ThreadStart(dummy.prt);
Thread t = new Thread(ts);
System.Console.WriteLine("Before Start");
t.Start();
}
}

Start() method

using System.Threading;
public class dummy
{
public void prt()
{
System.Console.WriteLine("Hi");
}
}
public class thread
{
public static void Main()
{
dummy obj = new dummy();
Thread t = new Thread (new ThreadStart(obj.prt));
t.Start();
}
}

2 threads running

using System.Threading;
public class dummy
{
public void prt()
{
for (int i=0; i<=3; i++) { System.Console.WriteLine(i + " "); } } public void disp() { for(int i= 0; i<=3; i++) { System.Console.WriteLine(i + "..."); } } } public class thread { public static void Main() { dummy obj = new dummy(); Thread t = new Thread (new ThreadStart(obj.prt)); Thread t1 = new Thread(new ThreadStart(obj.prt)); t.Start(); t1.Start(); } }

Sleep() method

using System.Threading;
public class dummy
{
public void prt()
{
for (int i=0; i<=3; i++) { System.Console.WriteLine(i + " "); Thread.Sleep(500); } } public void disp() { for(int i= 0; i<=3; i++) { System.Console.WriteLine(i + "..."); Thread.Sleep(500); } } } public class thread { public static void Main() { dummy obj = new dummy(); Thread t = new Thread (new ThreadStart(obj.prt)); Thread t1 = new Thread(new ThreadStart(obj.prt)); t.Start(); t1.Start(); } }

Name property

using System.Threading;
public class dummy
{
public void prt()
{
for (int i=0; i<=3; i++) { Thread t2 = Thread.CurrentThread; System.Console.WriteLine(i + "," + t2.Name + " "); Thread.Sleep(500); } } } public class thread { public static void Main() { dummy obj = new dummy(); Thread t = new Thread (new ThreadStart(obj.prt)); Thread t1 = new Thread(new ThreadStart(obj.prt)); t.Name = "One"; t1.Name = "Two"; t.Start(); t1.Start(); } }

Abort(), IsAlive() methods

using System.Threading;
public class dummy
{
public void prt()
{
System.Console.WriteLine("Hi!");
Thread.Sleep(10);
}
}
public class thread
{
public static void Main()
{
dummy obj = new dummy();
Thread t = new Thread (new ThreadStart(obj.prt));
System.Console.WriteLine(t.IsAlive);
t.Start();
System.Console.WriteLine(t.IsAlive);
t.Abort();
System.Console.WriteLine(t.IsAlive);
}
}

Running a thread till it is alive

Example 1
using System.Threading;
public class dummy
{
public void prt()
{
System.Console.WriteLine("Hi!");
}
}
public class thread
{
public static void Main()
{
dummy obj = new dummy();
Thread t = new Thread (new ThreadStart(obj.prt));
t.Start();
while (!t.IsAlive)
System.Console.WriteLine("Hi");
}
}
Example 2
using System.Threading;
public class dummy
{
public void prt()
{
for (int i=0; i<=3; i++) System.Console.WriteLine("Hi!" + i + " "); } } public class thread { public static void Main() { dummy obj = new dummy(); Thread t = new Thread (new ThreadStart(obj.prt)); t.Start(); System.Console.WriteLine("Over"); } }

Thread: Start(), Join()

Example 1
using System.Threading;
public class dummy
{
public void prt()
{
for (int i=0; i<=3; i++) System.Console.WriteLine("Hi!" + i + " "); } } public class thread { public static void Main() { dummy obj = new dummy(); Thread t = new Thread (new ThreadStart(obj.prt)); t.Start(); t.Join(); System.Console.WriteLine("Over"); } }
Example 2
using System.Threading;
public class dummy
{
public void prt()
{
for ( ; ; ) ;
}
}
public class thread
{
public static void Main()
{
dummy obj = new dummy();
Thread t = new Thread (new ThreadStart(obj.prt));
t.Start();
bool b = t.Join(10000);
System.Console.WriteLine("Over " + b) ;
}
}
Example 3
using System.Threading;
public class dummy
{
public void prt()
{
System.Console.WriteLine("function running");
}
}
public class thread
{
public static void Main()
{
dummy obj = new dummy();
Thread t = new Thread (new ThreadStart(obj.prt));
t.Start();
Thread.Sleep(200);
t.Abort();
System.Console.WriteLine("Over ") ;
Thread.Sleep(200);
try
{
t.Start();
}
catch(ThreadStateException e)
{
System.Console.WriteLine("In Exception " + e);
}
}
}

ApartmentState

using System.Threading;
public class dummy
{
public void prt()
{
System.Console.WriteLine("function running");
}
}
public class thread
{
public static void Main()
{
dummy obj = new dummy();
Thread t = new Thread (new ThreadStart(obj.prt));
System.Console.WriteLine(t.ApartmentState);
t.ApartmentState = ApartmentState.STA;
t.Start();
System.Console.WriteLine(t.ApartmentState);
}
}

IsBackground property

using System.Threading;
public class dummy
{
public void prt()
{
System.Console.WriteLine( "prt() " + Thread.CurrentThread.IsBackground);
}
}
public class thread
{
public static void Main()
{
dummy obj = new dummy();
Thread t = new Thread (new ThreadStart(obj.prt));
System.Console.WriteLine("Main " + Thread.CurrentThread.IsBackground);
t.Start();
}
}

Creating a background thread

using System.Threading;
public class dummy
{
public void prt()
{
for(int i=0; i<=100; i++) { System.Console.WriteLine( "prt() " + i); Thread.Sleep(100); } } } public class thread { public static void Main() { dummy obj = new dummy(); Thread t = new Thread (new ThreadStart(obj.prt)); System.Console.WriteLine("Main " + Thread.CurrentThread.IsBackground); t.IsBackground = true; t.Start(); Thread.Sleep(100); } }

ThreadPriority enum

Example 1
using System.Threading;
public class dummy
{
public void prt()
{
for(int i=0; i<=10; i++) { System.Console.WriteLine(i + " "); Thread.Sleep(100); } } public void display() { for(int i=0; i<=10; i++) { System.Console.WriteLine(i + "..."); Thread.Sleep(100); } } } public class thread { public static void Main() { dummy obj = new dummy(); Thread t = new Thread (new ThreadStart(obj.prt)); Thread t1 = new Thread(new ThreadStart(obj.display)); System.Console.WriteLine(t.Priority); t.Priority = ThreadPriority.Highest; t1.Priority = ThreadPriority.Lowest; t.Start(); t1.Start(); } }
Example 2
using System.Threading;
public class dummy
{
public void prt()
{
try
{
System.Console.WriteLine("in prt()");
for(; ; );
}
catch (System.Exception e)
{
System.Console.WriteLine("In abc Exception " + e.ToString());
}
finally
{
System.Console.WriteLine("in abc Finally");
}
}
}

public class thread
{
public static void Main()
{
dummy obj = new dummy();
Thread t = new Thread (new ThreadStart(obj.prt));
t.Start();
Thread.Sleep(10);
t.Abort();
}
}
Example 3: 

using System.Threading;
public class dummy
{
public void prt()
{
try
{
System.Console.WriteLine("in prt()");
for(; ; );
}
catch (System.Exception e)
{
System.Console.WriteLine("In abc Exception " + e.ToString());
}
finally
{
System.Console.WriteLine("in abc Finally");
}
}
}
public class thread
{
public static void Main()
{
dummy obj = new dummy();
Thread t = new Thread (new ThreadStart(obj.prt));
t.Abort();
t.Start();
}
}

Suspend(), Resume() methods using System.Threading;

public class dummy
{
public void prt()
{
for(int i=1; i<=10; i++) { System.Console.Write(i + " "); Thread.Sleep(100); } } } public class thread { public static void Main() { dummy obj = new dummy(); Thread t = new Thread (new ThreadStart(obj.prt)); t.Start(); Thread.Sleep(10); t.Suspend(); System.Console.WriteLine("\n After Suspend"); Thread.Sleep(10); System.Console.WriteLine("Before Resume"); t.Resume(); } }

Mutex

When two or more threads need to access a shared resource at the same time, the system needs to be in synchronization mechanism to ensure that only one thread at a time uses the resource. Mutex is a synchronization primitive that grants exclusive access to the shared resource to only one thread. If a thread acquires a mutex, the second thread that wants to acquire that mutex is suspended until the first thread releases the mutex. using System;
using System.Threading;
class Test
{
private static Mutex mut = new Mutex();
private const int numIterations = 1;
private const int numThreads = 3;
static void Main()
{
for (int i = 0; i < numThreads; i++) { Thread myThread = new Thread(new ThreadStart(MyThreadProc)); myThread.Name = String.Format("Thread{0}", i + 1); myThread.Start(); } } private static void MyThreadProc() { for (int i = 0; i < numIterations; i++) { UseResource(); } } private static void UseResource() { mut.WaitOne(); Console.WriteLine("{0} has entered the protected area",Thread.CurrentThread.Name); Thread.Sleep(500); Console.WriteLine("{0} is leaving the protected area\r\n",Thread.CurrentThread.Name); mut.ReleaseMutex(); } }

Mutex - Infinite wait

using System.Threading;
public class thread
{
static Mutex m;
public static void Main()
{
m = new Mutex(true, "aptech");
thread obj = new thread();
Thread t = new Thread(new ThreadStart(thread.prt));
t.Start();
}
public static void prt()
{
System.Console.WriteLine("thread start");
m.WaitOne();
System.Console.WriteLine("thread end");
}
}
Mutexes using System.Threading;
public class thread
{
static Mutex m;
public static void Main()
{
m = new Mutex(false, "aptech");
thread obj = new thread();
System.Console.WriteLine("thread start");
m.WaitOne();
System.Console.WriteLine("thread end");
}
}

Locking

using System;
using System.Threading;
class threadSafety
{
public static object Compute = new object();
public static int ValOne, ValTwo;
static void Divide()
{
ValOne = 45;
ValTwo = 8;
lock (Compute)
{
if (ValOne != 0)
Console.WriteLine(ValOne / ValTwo);
ValTwo = 0;
}
}
public static void Main()
{
Divide();
}
}

No comments:

Post a Comment