• Java concurrency and parallelism: barrier synchronizers (CountDownLatch, CyclicBarrier, Phaser)

    Introduction

    Barrier synchronizers (barriers) are a kind of synchronizer that ensures that any threads must stop at a certain point and cannot proceed further until all other threads reach this point.

    By purpose, barriers can be grouped into the following categories:

    • entry barriers, that prevents threads from starting processing
    • exit barriers, that waiting for all threads to finish processing

    Barriers also can be grouped by the number of iterations (one-time or cyclic) and by the number of parties/threads (fixed or variable).

    In Java 7+ there are 3 predefined barrier classes: CountDownLatch, CyclicBarrier, Phaser.

    Read on →

  • Java concurrency and parallelism: executors and thread pools

    Introduction

    In small applications to execute each task (Runnable object) is created a new thread (Thread object). When the task is completed, the thread is terminated as well. But in large applications overhead of threads creation and termination can be significant. Such overhead can be reduced by reusing the same threads for the execution of many tasks.

    For that purpose are used executors and thread pools. An executor is a design pattern that provides API for task executions and hides its implementation. A thread pool is one of the executor implementations that uses a pool of threads for task execution.

    Read on →

  • Java Stream collectors

    Introduction

    There are several ways to reduce Stream as a sequence of input elements into a single summary result. One of them is to use implementations of Collector interface with Stream.collect(collector) method. It’s possible to implement this interface explicitly, but it should start with studying its predefined implementations from Collectors class.

    Read on →

  • Server-Sent Events (SSE) in Spring 5 with Web MVC and Web Flux

    Introduction

    There are no simple, general-purpose methods to implement asynchronous server-to-client communication in web applications with acceptable performance.

    HTTP is a request-response protocol in the client-server computing model. To start an exchange, a client submits a request to a server. To finish the exchange, the server returns a response to the client. The server can send a response to only one client - the one that made the request. In the HTTP protocol, a client is the initiator of messages exchange.

    There are cases when a server should be the initiator of exchange. One of the methods to implement this is to allow the server to push messages to clients in the publish/subscribe computing model. To start an exchange, a client subscribes to messages from the server. During the exchange, the server sends messages (as soon as they become available) to many subscribed clients. To finish the exchange, the client cancels the subscription.

    Server-Sent Events (SSE) is a simple technology to implement asynchronous server-to-client communication for specific web applications.

    Read on →