Sometimes when we are using threading in a project and there are simultaneous threads running a piece of code but you want the threads to run the code sequentially,then we can make use of lock and create an object and place the critical section code in this lock.
private static readonly Object _lock = new Object();
lock (_lock)
{
// critical section
}
Code language: JavaScript (javascript)