PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

Java · Theory

Static Synchronization in Java

← All stacks

Theory

126/270

Static Synchronization in Java

Static Synchronization is part of Java concurrency. A thread is a path of work inside a process. Create with Thread or Runnable, then call start() — not run(). run() only runs on the same thread.

For Static Synchronization: if two threads share data, synchronize or the result can be wrong. sleep pauses. join waits for another thread to finish. daemon threads die when all user threads finish.

Static Synchronization deadlock: two threads wait for each other’s lock forever. Avoid by locking in the same order. Don’t guess thread order — it can change every run.

Let's take this on the board with one tiny Main class — no extra files. Static Synchronization in Java — output: 2000. Two threads each inc 1000 times. synchronized keeps the count exact — without it, you can get less than 2000. Start from main, go line by line, and stop at each print. That output is the proof for Static Synchronization.

Exam tip

Define deadlock with a 2-lock A/B story.

Example

class Counter {
  private int n;
  synchronized void inc() { n++; }
  synchronized int get() { return n; }
}
public class Main {
  public static void main(String[] args) throws Exception {
    Counter c = new Counter();
    Thread t1 = new Thread(() -> { for (int i = 0; i < 1000; i++) c.inc(); });
    Thread t2 = new Thread(() -> { for (int i = 0; i < 1000; i++) c.inc(); });
    t1.start(); t2.start();
    t1.join(); t2.join();
    System.out.println(c.get()); // 2000
  }
}

Static Synchronization in Java — output: 2000. Two threads each inc 1000 times. synchronized keeps the count exact — without it, you can get less than 2000.

Short notes

  • DefStatic Synchronization — thread = one path of work. start() schedules it. run() does not.
  • RuleStatic Synchronization — shared data needs synchronization.
  • RememberStatic Synchronization — sleep = pause. join = wait for other thread.
  • TrapStatic Synchronization — calling run() instead of start(). Deadlock = locks in wrong order.

Questions

1

What is Static Synchronization?

2

Why do we use Static Synchronization in Java?

3

Write one small example of Static Synchronization.

4

What mistake do beginners make with Static Synchronization?

5

Where is Static Synchronization used in a real program?

Previous← Java Synchronized BlockNextDeadlock in Java →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.