logoNEURAL COOK
SearchAll PostsTagsNotesAuthor
logoNEURAL COOK

A posts created with Next.js and Tailwind.css

Explore

  • All posts
  • Notes
  • Snippets
  • Tags
  • RSS feed

Connect

instagramlinkedinmailgithub

© 2026 VIKI. All rights reserved.

Built with
Contents
Global Lock OrderingRead Write SynchronizationReadWriteLock vs synchronized vs ReentrantLock60-Day LLD & Machine Coding Mastery Roadmap
The Four Pillars of OOPComposition, Asociation & AggregationDatabase locking / atomic updates
Don't Repeat Yourself (DRY) PrincipleKeep it Simple, Stupid (KISS) PrincipleYou Aren't Gonna Need It (YAGNI) Principle
Design a Library Management System (LMS)Design a Parking LotVending Machine
D : Dependency InversionI : The Interface Segregation PrincipleL : The Liskov Substitution PrincipleO : The Open/Closed PrincipleS : The Single Responsibility Principle
UML Activity Diagrams: Modeling Process and WorkflowUML Class Diagrams: Static Structure ModelingUML Sequence Diagrams: Modeling Dynamic InteractionsUse Case Diagrams: Functional Requirement Modeling
Low Level Design

Global Lock Ordering

2 min read

A lock is a mechanism that allows only one thread at a time to access a protected resource or critical section.

A deadlock occurs when two or more threads are permanently waiting for locks held by one another, so none of them can proceed.

Scenario, User A transfers 50toB.Attheexactsametime,Btransfers50 to B. At the exact same time, B transfers 50toB.Attheexactsametime,Btransfers20 back to A. For a transfer, we need to modify two accounts. We must lock both accounts to prevent another transaction from changing their balances midway through the transfer.

  • Thread 1 locks Account A → then waits for Account B.
  • Thread 2 locks Account B → then waits for Account A. Both threads are waiting. Neither can proceed.

The Coffman conditions, all four must hold simultaneously for a deadlock.

ConditionMeaningBank transfer example
1. Mutual ExclusionA resource can be held by only one thread at a time.Only one thread can hold Account A's lock.
2. Hold and WaitA thread holds one resource while waiting for another.Thread 1 holds A and waits for B.
3. No PreemptionA lock cannot simply be forcibly taken away from its owner.Thread 2 cannot forcibly take Account B's lock from Thread 1.
4. Circular WaitThreads form a circular chain of dependencies.Thread 1 waits for B, while Thread 2 waits for A.

Global Lock Ordering prevents deadlocks caused by circular wait by ensuring every thread acquires shared resources in the same predefined order, irrespective of the business operation it is performing. For example, always acquire locks in ascending order of their resource ID

Next→Read Write Synchronization