Java.Multithreading.What happens if the thread pool queue is already full but a new task is submitted?

When the thread pool queue is full and a new task is submitted, what happens depends on the thread pool’s rejection policy.

Let’s break it down clearly 👇

Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.What happens if the thread pool queue is already full but a new task is submitted?

Java.Multithreading.How big should a thread pool be?

The optimal thread pool size depends on several factors, mainly the type of tasks your application runs and the hardware it runs on.

Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.How big should a thread pool be?

Java.Multithreading.What is a thread pool?

A thread pool is a programming construct used to manage a group of worker threads for executing tasks concurrently. Instead of creating and destroying a thread for each task (which is expensive), a thread pool reuses a fixed number of threads to execute many tasks efficiently

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

Java.Multithreding.What is the difference between interrupted() and isInterrupted()?

🧠 interrupted() vs isInterrupted()

MethodStatic?Clears Interrupt Flag?Checks Which Thread?
Thread.interrupted()✅ Yes✅ Yes (resets to false)Current thread only
isInterrupted()❌ No❌ NoAny specific thread object

Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreding.What is the difference between interrupted() and isInterrupted()?

Java.Multithreding.What happens when an exception is thrown in a thread?

✅ 1. If it’s a checked or unchecked exception thrown from run():

  • The thread terminates immediately.
  • The exception is not rethrown to the main thread.
  • If uncaught, it’s passed to the thread’s UncaughtExceptionHandler (if any).
Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreding.What happens when an exception is thrown in a thread?

Java.Multithreading.Why is it not recommended to use Thread.stop()?

Thread.stop() is deprecated and strongly discouraged because it’s unsafe, unpredictable, and can corrupt program state.

Let me explain why in a clear, structured way.

Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.Why is it not recommended to use Thread.stop()?

Java.Multithreading.How to stop a thread?

❌ Deprecated (DO NOT USE)

thread.stop();    // 🚫 Dangerous and deprecated

This kills the thread immediately — which can leave shared data in an inconsistent state.

It doesn’t give the thread a chance to clean up resources or release locks.

Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.How to stop a thread?

Java.Multithreading.In Counter class we dont use compareAndSet method, so how it works ?

we use smth like this in Counter class

AtomicInteger counter = new AtomicInteger(0);
counter.incrementAndGet();

👉 We don’t directly call compareAndSet() — but under the hood, incrementAndGet() uses compareAndSet() (or CAS instructions) internally!

Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.In Counter class we dont use compareAndSet method, so how it works ?

Java.Multithreading.How CAS works under the hood and how it helps in sychronization ?

🔁 What is CAS (Compare-And-Swap)?

CAS is a hardware-level atomic operation used to implement lock-free synchronization.

It works like this:

  1. Read the current value of a variable (expected).
  2. Compare it to an expected value.
  3. If it matches, swap it with a new value.
  4. If it doesn’t, do nothing — try again (typically in a loop).
Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.How CAS works under the hood and how it helps in sychronization ?

Java.Multithreading.Is there a way to solve a race condition?

Yes, there are multiple ways to solve a race condition depending on what you’re doing and how critical performance is.

Let’s go over the most common and effective ways 🔧:

Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.Is there a way to solve a race condition?