Java.Multithreading.What is a race condition?

🔥 Understanding race conditions is essential for mastering concurrent programming — and avoiding some very sneaky bugs.

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

Java.Multithreading.What is the difference between CyclicBarrier and CountDownLatch?

Excellent question, Stanley! 🚧 Both CyclicBarrier and CountDownLatch are powerful synchronization tools in Java’s java.util.concurrent package — but they serve different purposes and behave differently in key ways.

Let’s break them down and compare side by side.

Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.What is the difference between CyclicBarrier and CountDownLatch?

Java.Multithreading.What is FutureTask?

📦 What is FutureTask?

✅ FutureTask<V> is a class that implements both Runnable and Future<V>, allowing you to:

  • Wrap a Callable or Runnable task,
  • Run it in a thread or executor,
  • And retrieve a result later using Future methods.

🔧 It’s in java.util.concurrent

public class FutureTask<V> implements RunnableFuture<V> { ... }

Where:

  • RunnableFuture<V> extends both Runnable and Future<V>

So a FutureTask is:

  • A task you can execute (because it’s Runnable)
  • A handle to get the result later (because it’s Future)

✅ Real-World Example

Callable<String> task = () -> {
    Thread.sleep(1000);
    return "Hello from Callable";
};

FutureTask<String> futureTask = new FutureTask<>(task);

// Run with Thread
new Thread(futureTask).start();

// Get the result
String result = futureTask.get(); // blocks until done
System.out.println(result);

🟢 Output after 1 second:

Hello from Callable

🧠 Why Use FutureTask?

FeatureDescription
✅ Combines Runnable + FutureCan run the task and get result
✅ Works with Executors or ThreadsFlexible execution
✅ Can be cancelledVia futureTask.cancel(true)
✅ Thread-safe result cachingOnly runs once, result is stored

🔁 Common Use Cases

  • Wrapping Callable for custom thread usage
  • Lazy initialization with result caching
  • Manually managing task lifecycle (run, cancel, get)
  • Submitting to thread pools and getting a result

📌 Bonus: Runnable Version

You can also wrap a Runnable with a predefined result:

Runnable runnable = () -> System.out.println("Runnable executed");
FutureTask<String> futureTask = new FutureTask<>(runnable, "Task Done");

new Thread(futureTask).start();
System.out.println(futureTask.get()); // prints: Task Done

⚠️ Notes

CaveatDetail
get() blocksUnless task is finished or timed out
Only executes onceEven if run() is called multiple times
Canceling is optionalYou can call cancel() before/during execution

🧠 Summary

TraitFutureTask
TypeClass implementing Runnable and Future
UseRun task + get result later
Works withThreads, Executors, manually
HandlesCallable, or Runnable + result
Result accessVia get(), isDone(), cancel()
Posted in Без рубрики | Comments Off on Java.Multithreading.What is FutureTask?

Java.Multithreading.What is the difference between the two interfaces Runnable and Callable?

🧵 1. Runnable — The Classic Thread Task

Runnable task = () -> {
    System.out.println("I'm running");
};
Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.What is the difference between the two interfaces Runnable and Callable?

Java.Multithreading.What does it mean to put a thread to sleep?

🧠 What Does “Putting a Thread to Sleep” Mean?

It means pausing the execution of the current thread for a specific period of time, without releasing any locks it may hold.

Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.What does it mean to put a thread to sleep?

Java.Multithreading.Can the main thread of a program be made a daemon?

🧩 Why?

Because:

  • The main thread is created by the JVM before your program code even runs.
  • You don’t get a chance to call setDaemon(true) on it.
  • Once a thread has started, you can no longer change its daemon status — it must be set before calling .start().
Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.Can the main thread of a program be made a daemon?

Java.Multithreading.What are “daemon threads”?

👻 What Is a Daemon Thread?

A daemon thread is a background thread that does not prevent the JVM from exiting.

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

Java.Multithreding.What does “thread priority” mean?

🧠 What is Thread Priority?

Thread priority is a number that tells the JVM and operating system how “important” a thread is relative to other threads.

In theory:
🔼 Higher-priority threads are given more CPU time than lower-priority ones.

Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreding.What does “thread priority” mean?

Java.Multithreading.What is the difference between java.util.concurrent.Atomic*.compareAndSwap() and java.util.concurrent.Atomic*.weakCompareAndSwap().

🔄 compareAndSet() vs weakCompareAndSet()

Both are methods in java.util.concurrent.atomic.* classes, like AtomicInteger, AtomicReference, etc.

They perform Compare-And-Swap (CAS) operations — which are the backbone of lock-free algorithms.

Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.What is the difference between java.util.concurrent.Atomic*.compareAndSwap() and java.util.concurrent.Atomic*.weakCompareAndSwap().

Java.Multithreading.What is the difference between volatile and Atomic variables?

volatile and atomic variables often seem similar at first glance, but they serve different purposes and offer different guarantees in Java concurrency.

Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.What is the difference between volatile and Atomic variables?