Advertisement
Methods | Description |
---|---|
public static bool Wait(Object obj) | This static method releases the lock on an object and blocks the current thread until it reacquires the lock. In other words, this method makes the thread that has called the Wait() method to wait for the other thread to complete its work on the same object. |
public static void Pulse(Object obj) | This static method notifies the first thread in the waiting queue of threads about a change in the locked object's state. The thread was waiting(after calling Wait() method) on the same object. |
public static void PulseAll(Object obj) | This static method notifies all waiting threads of a change in the object's state i.e. about the release of lock over the object. These threads were waiting(after calling the Wait() method) on the same object. |
using System;
using System.Threading;
class ThreadEx
{
Thread thread;
//Constructor of our class
public ThreadEx(String name)
{
//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
thread.Start();
}
//The entry-point method of the thread
public void Run()
{
//Synchronizing/locking the object of ThreadEx by using this reference
//Doing so, restricts any other thread to access a block of code using this object at the same time.
lock(this)
{
Thread th = Thread.CurrentThread;
Console.WriteLine(th.Name + " running and printing the table of 5 ");
for(int i=1; i<=10; i++)
{
Console.WriteLine("5 x "+ i + " = " + (5*i));
}
} //synchronized block ends
}
public static void Main(String[] ar)
{
//Creating an object of our ThreadEx class
//where each object is used to construct a separate thread
ThreadEx ob1 = new ThreadEx("My Thread1");
//Synchronizing/locking the access to the object of ThreadEx, referenced by ob1
//Doing so, restricts any other thread to access a block of code using this object at the same time.
lock(ob1)
{
Thread th = Thread.CurrentThread;
th.Name = "Main Thread";
Console.WriteLine(th.Name + " running and printing the table of 6");
for(int i=1; i<=10; i++)
{
Console.WriteLine("6 x "+ i + " = " + (6*i));
}
} //synchronized block ends
}
}
Main Thread running and printing the table of 6
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60
My Thread1 running and printing the table of 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Advertisement
using System;
using System.Threading;
class ThreadEx
{
Thread thread;
//Constructor of our class
public ThreadEx(String name)
{
//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
thread.Start();
}
//The entry-point method of the thread
public void Run()
{
//Synchronizing/locking the object of ThreadEx by using this reference
//Doing so, restricts any other thread to access a block of code using this object at the same time.
lock(this)
{
Thread th = Thread.CurrentThread;
Console.WriteLine(th.Name + " running and printing the table of 5 ");
for(int i=1; i<=10; i++)
{
Console.WriteLine("5 x "+ i + " = " + (5*i));
}
//The manually created thread is calling the Pulse() method
//To notifying the Main thread that it is releasing the lock over the object of ThreadEx class
//And Main Thread could lock the object to continue its work
Monitor.Pulse(this);
} //synchronized block ends
}
public static void Main(String[] ar)
{
//Creating an object of our ThreadEx class
//where each object is used to construct a separate thread
ThreadEx ob1 = new ThreadEx("My Thread1");
//Synchronizing/locking the access to the object of ThreadEx, referenced by ob1
//Doing so, restricts any other thread to access a block of code using this object at the same time.
lock(ob1)
{
//Calling the Wait() method in a synchronized context
//Doing so, makes the Main Thread stops its execution and wait
//until it is notified by the Pulse() method
//on the same object of ThreadEx class
Monitor.Wait(ob1);
Thread th = Thread.CurrentThread;
th.Name = "Main Thread";
Console.WriteLine(th.Name + " running and printing the table of 6");
for(int i=1; i<=10; i++)
{
Console.WriteLine("6 x "+ i + " = " + (6*i));
}
} //synchronized block ends
}
}
My Thread1 running and printing the table of 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Main Thread running and printing the table of 6
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement