Java.Reactive.Explain the concept of data flows in reactive programming

In reactive programming, a data flow is like a pipeline where data travels through a series of transformations and reactions — all done in a declarative, non-blocking, and asynchronous way.

You don’t “call” data; it comes to you. You set up a pipeline and let data flow through it.

Continue reading →
Posted in Без рубрики | Comments Off on Java.Reactive.Explain the concept of data flows in reactive programming

Java.Reactive.What is reactive programming and how is it different from procedural programming?

Reactive Programming is a programming paradigm focused on asynchronous data streams and the propagation of change. It’s all about reacting to data as it comes in — like events, user interactions, or data updates — and building systems that respond to these changes automatically and efficiently.

Continue reading →
Posted in Без рубрики | Comments Off on Java.Reactive.What is reactive programming and how is it different from procedural programming?

Java.Multithreading.Write a simple multi-threaded bounded buffer using ReentrantLock.

Absolutely, Stanley! 💡 Let’s implement a simple, thread-safe, bounded buffer using:

  • ✅ ReentrantLock for thread safety
  • ✅ Condition variables for blocking and coordination
  • ✅ Classic producer-consumer pattern

🎯 Goal

  • Block put() when buffer is full
  • Block take() when buffer is empty
  • Avoid synchronized, use explicit locks and condition signaling
Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.Write a simple multi-threaded bounded buffer using ReentrantLock.

Java.Multithreading.Write a simple multi-threaded bounded buffer using synchronized.

📦 What Is a Bounded Buffer?

A bounded buffer is a fixed-size queue that:

  • Allows producers to put() items.
  • Allows consumers to take() items.
  • Blocks producers if the buffer is full.
  • Blocks consumers if the buffer is empty.

✅ Key Features:

  • Thread-safe with synchronized
  • Uses wait()/notifyAll() for coordination
  • Fixed-size circular buffer
Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.Write a simple multi-threaded bounded buffer using synchronized.

Java.Multithreading.Write a thread-safe implementation of a class with a non-blocking BigInteger next() method that returns the elements of the sequence: [1, 2, 4, 8, 16, …].

✅ Requirements

  • Thread-safe: Safe for concurrent calls from multiple threads.
  • Non-blocking: No synchronized, no locks.
  • Returns: BigInteger values doubling each time → 1, 2, 4, 8, 16, ...
Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.Write a thread-safe implementation of a class with a non-blocking BigInteger next() method that returns the elements of the sequence: [1, 2, 4, 8, 16, …].

Java.Multithreading.Write a minimal non-blocking ArrayList (only four methods – add(), get(), remove(), size()).

💪 Writing a non-blocking ArrayList is a tricky but fun task — because arrays require index-based coordination, which is much harder to do without locks than a linked structure like a stack.

But let’s take it step by step and aim for a minimal lock-free-ish dynamic array structure — using atomic operations only.

Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.Write a minimal non-blocking ArrayList (only four methods – add(), get(), remove(), size()).

Java.Multithreading.Write a minimal non-blocking stack (only two methods – push() and pop()) using Semaphore.

✅ Minimal Blocking Stack Using Semaphore

This stack:

  • Uses a Deque to store elements
  • Uses a Semaphore to track available elements
  • Is thread-safe, but not lock-free (uses internal synchronization)
import java.util.Deque;
import java.util.LinkedList;
import java.util.concurrent.Semaphore;

public class SemaphoreStack<T> {
    private final Deque<T> stack = new LinkedList<>();
    private final Semaphore items = new Semaphore(0); // Tracks how many items are available
    private final Object lock = new Object(); // Guards access to the stack

    public void push(T value) {
        synchronized (lock) {
            stack.push(value);
        }
        items.release(); // Signal: one more item available
    }

    public T pop() throws InterruptedException {
        items.acquire(); // Wait until item is available
        synchronized (lock) {
            return stack.pop();
        }
    }
}

🧪 Example Usage:

public class Main {
    public static void main(String[] args) throws InterruptedException {
        SemaphoreStack<Integer> stack = new SemaphoreStack<>();

        stack.push(1);
        stack.push(2);

        System.out.println(stack.pop()); // 2
        System.out.println(stack.pop()); // 1
    }
}

🔍 How It Works:

  • items.acquire() blocks if the stack is empty
  • items.release() increases the count (available items)
  • synchronized protects the internal stack from race conditions

🧠 Summary

FeatureSemaphore Stack
Thread-safe?✅ Yes
Lock-free?❌ No (uses synchronized)
Blocking behavior?✅ Yes (pop blocks if empty)
Use case?Thread coordination, bounded stacks, consumer-producer models
Posted in Без рубрики | Comments Off on Java.Multithreading.Write a minimal non-blocking stack (only two methods – push() and pop()) using Semaphore.

Java.Multithreading.Write a minimal non-blocking stack (only two methods – push() and pop()).

🔥 Let’s write a minimal non-blocking stack using lock-free programming with CAS (Compare-And-Swap) operations, leveraging AtomicReference.

Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.Write a minimal non-blocking stack (only two methods – push() and pop()).

Java.Multithreading.Given 3 threads T1, T2 and T3? How to implement execution in sequence T1, T2, T3?

🔄 Controlling the execution order of threads (T1 → T2 → T3) is a classic multithreading problem, and there are several ways to achieve it — depending on your style and needs.

✅ Goal: Run Threads in This Exact Order

T1 prints → T2 prints → T3 prints
Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.Given 3 threads T1, T2 and T3? How to implement execution in sequence T1, T2, T3?

Java.Multithreading.List the principles you follow in multithreaded programming?

✅ 1. Minimize Shared Mutable State

The best way to avoid concurrency issues is not to share data at all.

  • Prefer immutable objects
  • Use thread-local storage when needed
  • Copy-on-write or defensive copying strategies
Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.List the principles you follow in multithreaded programming?