IntroductionSemaphore is a synchronization technique where we can control number of threads to access a resource. In lock/Mutex, only one thread can access resources at a time. But Semaphore allows multiple threads to access the same resource at a time. We can limit the number of threads that can access the same resources. In this article, I have shown three different ways to access resources. No-1. No Synchronization
No-2. Synchronization with Monitor
No-3. Synchronization with SemaphoreUsing the Code
No SynchronizationWith no synchronization, all threads run simultaneously and execute the same piece of code simultaneously. There is no restriction on how many threads can access it. Following is the code: private void btnNoSync_Click(object sender, EventArgs e)
{
listBox1.Items.Add("== No Synchronization ===========");
int TotalThread = 5;
Thread[] Threads = new Thread[TotalThread];
for (int i = 0; i < TotalThread; i++)
{
Threads[i] = new Thread(new ThreadStart(AccessCode));
Threads[i].IsBackground = true;
Threads[i].Start();
}
}public void AccessCode()
{
listBox1.BeginInvoke(new ParameterizedThreadStart(UpdateUI), new object[]
{"Thread ID : " + Thread.CurrentThread.ManagedThreadId.ToString() + ": Entered" }
);
Thread.Sleep(500);
listBox1.BeginInvoke(new ParameterizedThreadStart(UpdateUI), new object[]
{ "Thread ID : " + Thread.CurrentThread.ManagedThreadId.ToString() + " : Exit" }
);
}
// Following code is used to update UI
public void UpdateUI(object objOutput)
{
listBox1.Items.Add(objOutput.ToString());
}Synchronization with Monitor Synchronization with monitor class, only one thread can access the same resource at a time. Thread is run simultaneously but it can access the block of code one at a time. There is a restriction on thread so that only a single thread can access a particular code block. private void buttonMonitor_Click(object sender, EventArgs e)
{
listBox1.Items.Add("== Using Monitor =============");
int TotalThread = 5;
Thread[] Threads = new Thread[TotalThread];
for (int i = 0; i < TotalThread; i++)
{
Threads[i] = new Thread(new ThreadStart(AccessCodeWithMonitor));
Threads[i].IsBackground = true;
Threads[i].Start();
}
} private void AccessCodeWithMonitor()
{
Monitor.Enter(this);
try
{
listBox1.BeginInvoke(new ParameterizedThreadStart(UpdateUI), new object[]
{ "Thread ID : " +
Thread.CurrentThread.ManagedThreadId.ToString() + " : Entered" });
Thread.Sleep(500);
listBox1.BeginInvoke(new ParameterizedThreadStart(UpdateUI), new object[]
{ "Thread ID : " +
Thread.CurrentThread.ManagedThreadId.ToString() + " : Exit" });
}
finally
{
Monitor.Exit(this);
}
}
private void UpdateUI(object objOutput)
{
listBox1.Items.Add(objOutput.ToString());
}
Read more: Codeproject
QR:
No-2. Synchronization with Monitor
No-3. Synchronization with SemaphoreUsing the Code
No SynchronizationWith no synchronization, all threads run simultaneously and execute the same piece of code simultaneously. There is no restriction on how many threads can access it. Following is the code: private void btnNoSync_Click(object sender, EventArgs e)
{
listBox1.Items.Add("== No Synchronization ===========");
int TotalThread = 5;
Thread[] Threads = new Thread[TotalThread];
for (int i = 0; i < TotalThread; i++)
{
Threads[i] = new Thread(new ThreadStart(AccessCode));
Threads[i].IsBackground = true;
Threads[i].Start();
}
}public void AccessCode()
{
listBox1.BeginInvoke(new ParameterizedThreadStart(UpdateUI), new object[]
{"Thread ID : " + Thread.CurrentThread.ManagedThreadId.ToString() + ": Entered" }
);
Thread.Sleep(500);
listBox1.BeginInvoke(new ParameterizedThreadStart(UpdateUI), new object[]
{ "Thread ID : " + Thread.CurrentThread.ManagedThreadId.ToString() + " : Exit" }
);
}
// Following code is used to update UI
public void UpdateUI(object objOutput)
{
listBox1.Items.Add(objOutput.ToString());
}Synchronization with Monitor Synchronization with monitor class, only one thread can access the same resource at a time. Thread is run simultaneously but it can access the block of code one at a time. There is a restriction on thread so that only a single thread can access a particular code block. private void buttonMonitor_Click(object sender, EventArgs e)
{
listBox1.Items.Add("== Using Monitor =============");
int TotalThread = 5;
Thread[] Threads = new Thread[TotalThread];
for (int i = 0; i < TotalThread; i++)
{
Threads[i] = new Thread(new ThreadStart(AccessCodeWithMonitor));
Threads[i].IsBackground = true;
Threads[i].Start();
}
} private void AccessCodeWithMonitor()
{
Monitor.Enter(this);
try
{
listBox1.BeginInvoke(new ParameterizedThreadStart(UpdateUI), new object[]
{ "Thread ID : " +
Thread.CurrentThread.ManagedThreadId.ToString() + " : Entered" });
Thread.Sleep(500);
listBox1.BeginInvoke(new ParameterizedThreadStart(UpdateUI), new object[]
{ "Thread ID : " +
Thread.CurrentThread.ManagedThreadId.ToString() + " : Exit" });
}
finally
{
Monitor.Exit(this);
}
}
private void UpdateUI(object objOutput)
{
listBox1.Items.Add(objOutput.ToString());
}
Read more: Codeproject
QR:
0 comments:
Post a Comment