InterLocked synchronisation with Multithreading
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace InterlockedClassDemo
{
public partial class Form1 : Form
{
int Number = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Number =0; //whenever a button is clicked the Number is reassigned to 0
int totalThread = 10000; //the number of threads thats need to be created is 10000
Thread[] interLockedThread = new Thread[totalThread]; // Array of types Thread is created
for (int i = 0; i < totalThread; i++) // Declared For Loop to creat 10000 thread for performing the increment operation
{
interLockedThread[i] = new Thread(new ThreadStart(UpdateValue)); //parameterised threads are created and listbox is updated
interLockedThread[i].IsBackground = true; // Threads is moved to background
interLockedThread[i].Priority = ThreadPriority.Lowest; //priority is set to low
interLockedThread[i].Start(); //started the execution of the thread
}
for (int i = 0; i < totalThread; i++)
{
interLockedThread[i].Join(); // Main thread is stopped till the completion of all Threads
}
label5.Text = Number.ToString();
}
private void UpdateValue()
{
listBox1.BeginInvoke(new ParameterizedThreadStart(updateListBox1), "ThreadID ::" + Thread.CurrentThread.ManagedThreadId + "-B=" + Number.ToString());
Number ++;
listBox1.BeginInvoke(new ParameterizedThreadStart(updateListBox1), "ThreadID ::" + Thread.CurrentThread.ManagedThreadId + "-A=" + Number.ToString());
}
public void updateListBox1(object objrslt)
{
listBox1.Items.Add(objrslt.ToString());
}
private void button2_Click(object sender, EventArgs e)
{
Number = 0; //whenever a button is clicked the Number is reassigned to 0
int totalThread = 10000; //the number of threads thats need to be created is 10000
Thread[] interLockedThread = new Thread[totalThread]; // Array of types Thread is created
for (int i = 0; i < totalThread; i++) // Declared For Loop to creat 10000 thread for performing the increment operation
{
interLockedThread[i] = new Thread(new ThreadStart(UpdateValueInterlock)); //parameterised threads are created and listbox is updated
interLockedThread[i].IsBackground = true; // Threads is moved to background
interLockedThread[i].Priority = ThreadPriority.Lowest; //priority is set to low
interLockedThread[i].Start(); //started the execution of the thread
}
for (int i = 0; i < totalThread; i++)
{
interLockedThread[i].Join(); // Main thread is stopped till the completion of all Threads
}
label6.Text = Number.ToString(); //At the out putLabel the number is displayed
}
private void UpdateValueInterlock()
{
listBox2.BeginInvoke(new ParameterizedThreadStart(updateListBox2), "ThreadID ::" + Thread.CurrentThread.ManagedThreadId + "-B=" + Number.ToString()); //Using delegate method to start the thread
Interlocked.Increment(ref Number); //Increment the nuber by interlocked method
listBox2.BeginInvoke(new ParameterizedThreadStart(updateListBox2), "ThreadID ::" + Thread.CurrentThread.ManagedThreadId + "-A=" + Number.ToString()); //Using delegate method to start the thread
}
public void updateListBox2(object objrslt)
{
listBox2.Items.Add(objrslt.ToString()); //Update ing item in the list box
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace InterlockedClassDemo
{
public partial class Form1 : Form
{
int Number = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Number =0; //whenever a button is clicked the Number is reassigned to 0
int totalThread = 10000; //the number of threads thats need to be created is 10000
Thread[] interLockedThread = new Thread[totalThread]; // Array of types Thread is created
for (int i = 0; i < totalThread; i++) // Declared For Loop to creat 10000 thread for performing the increment operation
{
interLockedThread[i] = new Thread(new ThreadStart(UpdateValue)); //parameterised threads are created and listbox is updated
interLockedThread[i].IsBackground = true; // Threads is moved to background
interLockedThread[i].Priority = ThreadPriority.Lowest; //priority is set to low
interLockedThread[i].Start(); //started the execution of the thread
}
for (int i = 0; i < totalThread; i++)
{
interLockedThread[i].Join(); // Main thread is stopped till the completion of all Threads
}
label5.Text = Number.ToString();
}
private void UpdateValue()
{
listBox1.BeginInvoke(new ParameterizedThreadStart(updateListBox1), "ThreadID ::" + Thread.CurrentThread.ManagedThreadId + "-B=" + Number.ToString());
Number ++;
listBox1.BeginInvoke(new ParameterizedThreadStart(updateListBox1), "ThreadID ::" + Thread.CurrentThread.ManagedThreadId + "-A=" + Number.ToString());
}
public void updateListBox1(object objrslt)
{
listBox1.Items.Add(objrslt.ToString());
}
private void button2_Click(object sender, EventArgs e)
{
Number = 0; //whenever a button is clicked the Number is reassigned to 0
int totalThread = 10000; //the number of threads thats need to be created is 10000
Thread[] interLockedThread = new Thread[totalThread]; // Array of types Thread is created
for (int i = 0; i < totalThread; i++) // Declared For Loop to creat 10000 thread for performing the increment operation
{
interLockedThread[i] = new Thread(new ThreadStart(UpdateValueInterlock)); //parameterised threads are created and listbox is updated
interLockedThread[i].IsBackground = true; // Threads is moved to background
interLockedThread[i].Priority = ThreadPriority.Lowest; //priority is set to low
interLockedThread[i].Start(); //started the execution of the thread
}
for (int i = 0; i < totalThread; i++)
{
interLockedThread[i].Join(); // Main thread is stopped till the completion of all Threads
}
label6.Text = Number.ToString(); //At the out putLabel the number is displayed
}
private void UpdateValueInterlock()
{
listBox2.BeginInvoke(new ParameterizedThreadStart(updateListBox2), "ThreadID ::" + Thread.CurrentThread.ManagedThreadId + "-B=" + Number.ToString()); //Using delegate method to start the thread
Interlocked.Increment(ref Number); //Increment the nuber by interlocked method
listBox2.BeginInvoke(new ParameterizedThreadStart(updateListBox2), "ThreadID ::" + Thread.CurrentThread.ManagedThreadId + "-A=" + Number.ToString()); //Using delegate method to start the thread
}
public void updateListBox2(object objrslt)
{
listBox2.Items.Add(objrslt.ToString()); //Update ing item in the list box
}
}
}
Comments
Post a Comment