Advertisement
lock(object)
{
//Statement1
//Statement2
//And more statements to be synchronized
}
//C# Example of Synchronization
using System;
using System.Threading;
class B
{
public void meth()
{
for(int i=0;i<3;i++)
{
Console.WriteLine(Thread.CurrentThread.Name +" is printing " + i);
}
}
}
class ThreadEx
{
Thread thread;
//Constructor of our class
public ThreadEx(String name, B ob)
{
//Creating a new thread, based on the object of ThreadEx class
//Which is referred by 'this' reference
//We are also specifying the entry-point method that will be called to begin the execution of our thread.
thread = new Thread(this.Run);
//Setting the name of a thread by using the Name property of Thread
thread.Name = name;
//Calling the Start() method, which calls the entry-point method defined with an argument
//And passing it an argument i.e. object of the B class
thread.Start(ob);
}
//Entry Point of the new thread.
public void Run(Object ob)
{
//Getting the reference to the currently executing thread.
Thread th = Thread.CurrentThread;
//Casting the value of count(which was internally an int), from Object to int
B obj =(B)ob;
Console.WriteLine(th.Name + " has started its execution");
//Using the object of B class to call the meth() method
obj.meth();
Console.WriteLine(th.Name + " has finished its execution ");
}
public static void Main(String[] ar)
{
//Creating an object of B class
B ob = new B();
//Creating three objects of our ThreadEx class
//where each object is used to construct a separate thread
//And all three objects are using the same object of B class
ThreadEx ob1 = new ThreadEx("My Thread1", ob);
ThreadEx ob2 = new ThreadEx("My Thread2", ob);
ThreadEx ob3 = new ThreadEx("My Thread3", ob);
}
}
My Thread1 has started its execution
My Thread2 has started its execution
My Thread2 is printing 0
My Thread1 is printing 0
My Thread1 is printing 1
My Thread1 is printing 2
My Thread1 has finished its execution
My Thread3 has started its execution
My Thread3 is printing 0
My Thread3 is printing 1
My Thread3 is printing 2
My Thread3 has finished its execution
My Thread2 is printing 1
My Thread2 is printing 2
My Thread2 has finished its execution
Advertisement
//C# Example of Synchronization
using System;
using System.Threading;
class B
{
public void meth()
{
for(int i=0;i<3;i++)
{
Console.WriteLine(Thread.CurrentThread.Name +" is printing " + i);
}
}
}
class ThreadEx
{
Thread thread;
//Constructor of our class
public ThreadEx(String name, B ob)
{
//Creating a new thread, based on the object of ThreadEx class
//Which is referred by 'this' reference
//We are also specifying the entry-point method that will be called to begin the execution of our thread.
thread = new Thread(this.Run);
//Setting the name of a thread by using the Name property of Thread
thread.Name = name;
//Calling the Start() method, which calls the entry-point method defined with an argument
//And passing it an argument i.e. object of the B class
thread.Start(ob);
}
//Entry Point of the new thread.
public void Run(Object ob)
{
//Getting the reference to the currently executing thread.
Thread th = Thread.CurrentThread;
//Casting the object referenced, ob(which was internally an object of B class), from Object to B
B obj =(B)ob;
// Locking the object of B class
//In other words, synchronizing the access to the object of B class
lock(obj)
{
Console.WriteLine(th.Name + " has started its execution");
obj.meth();
Console.WriteLine(th.Name + " has finished its execution ");
}
}
public static void Main(String[] ar)
{
//Creating an object of B class
B ob = new B();
//Creating three objects of our ThreadEx class
//where each object is used to construct a separate thread
//And all three objects are using the same object of B class
ThreadEx ob1 = new ThreadEx("My Thread1", ob);
ThreadEx ob2 = new ThreadEx("My Thread2", ob);
ThreadEx ob3 = new ThreadEx("My Thread3", ob);
}
}
My Thread1 has started its execution
My Thread1 is printing 0
My Thread1 is printing 1
My Thread1 is printing 2
My Thread1 has finished its execution
My Thread2 has started its execution
My Thread2 is printing 0
My Thread2 is printing 1
My Thread2 is printing 2
My Thread2 has finished its execution
My Thread3 has started its execution
My Thread3 is printing 0
My Thread3 is printing 1
My Thread3 is printing 2
My Thread3 has finished its execution
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement