Java.Multithreading.Can new instances of a class be created while a static synchronized method is executing?

Yes, new instances of a class can be created while a static synchronized method is executing.

Let’s break it down:


🔒 static synchronized method

  • It locks on the Class object, not on an instance.
  • So, if thread A is executing a static synchronized method, it holds the class-level lock.

💡 Creating new instances

  • Creating an object via new MyClass() does not require any lock.
  • It’s simply calling a constructor, which is not synchronized by default.
  • Therefore, other threads can create new instances of the class, even while the static synchronized method is running.
Posted in Без рубрики | Comments Off on Java.Multithreading.Can new instances of a class be created while a static synchronized method is executing?

Java.Multithreding.What states can a thread be in?

Let’s walk through the 6 thread states in Java, according to the java.lang.Thread.State enum.


🧵 Java Thread States

StateWhen It Happens
NEWThread object is created, but start() has not been called
RUNNABLEThread is ready to run or currently running
BLOCKEDWaiting to acquire a monitor lock (e.g. synchronized)
WAITINGWaiting indefinitely for another thread to signal
TIMED_WAITINGWaiting for a specified time (e.g. sleep(), join(timeout))
TERMINATEDThread has completed execution or thrown an exception
Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreding.What states can a thread be in?

Java.Multithreading.Example with consumer and producer

Let’s compare wait()/notify() (used with synchronized) and Condition (used with ReentrantLock) — both are used for thread coordination, but Condition gives you more flexibility.

We’ll walk through:

  1. A wait()/notify() example
  2. A Condition example doing the same thing

Scenario: We have a producer and consumer. The producer sets a flag when data is ready; the consumer waits until that happens.

Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.Example with consumer and producer

Java.Multithreading.When to use what ?

🔎 Key Criteria to Decide:

  1. Do you need mutual exclusion?
  2. Do you need only visibility or atomicity?
  3. Do you need fine-grained locking or advanced features like try-lock, fairness, etc.?
  4. Are threads coordinating around a condition (like a signal or phase)?

Let’s look at each scenario 👇

Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.When to use what ?

Java.Multithreading.What are the different types of synchronization in Java?

🧠 1. Intrinsic (Built-in) Synchronization

This is the most commonly used type of synchronization in Java, based on monitors.

A. Synchronized Method

public synchronized void increment() {
    // Only one thread at a time can execute this method
}

Locks on the current object (this)

For static methods, it locks on the class object (ClassName.class)

Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.What are the different types of synchronization in Java?

Java.Multithreading.Define the concept of “synchronization”.

🧠 What is Synchronization?

Synchronization is the process of controlling access to shared resources by multiple threads to prevent race conditions, data inconsistency, and memory visibility issues.

Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.Define the concept of “synchronization”.

Java.Multithreading.if you say monitor is the mutex, tell me in terms of mutex how sync happens ?

💡 First: A Monitor in Java = A Mutex + Condition Variable

Java’s monitor is not just a lock — it’s a synchronization mechanism that includes:

  • a mutex (for mutual exclusion)
  • zero or more condition queues (for waiting/notification: wait(), notify())
Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.if you say monitor is the mutex, tell me in terms of mutex how sync happens ?

Java.Multithreading.Give me details about monitor, i cannot understand how it can synchronize only part of the method if we locking whole the object ?

🧠 First, What Is a Monitor?

A monitor is a mutex (mutual exclusion lock) + condition variable attached to every object in Java.

  • Every Java object has one monitor.
  • A monitor ensures that only one thread at a time can hold the monitor lock on the object.
  • When a thread holds the monitor, other threads trying to lock it are blocked until it’s released.
Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.Give me details about monitor, i cannot understand how it can synchronize only part of the method if we locking whole the object ?

Java.Multithreading.Why do we need Synchronized (this) and Synchronized method if they give us the same result ?

Let’s unpack it carefully — the short answer is: ✅ they behave the same, but 🚀 give you flexibility in different contexts.

Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.Why do we need Synchronized (this) and Synchronized method if they give us the same result ?

Java.Multithreading.What is a “monitor” in Java?

🧠 What Is a Monitor in Java?

A monitor is a synchronization construct that allows threads to have mutually exclusive access to critical sections of code or data.

In Java, every object has an implicit monitor — yes, even a new Object() has one!

Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.What is a “monitor” in Java?