HTTPCLIENT / NOTES</>↗HttpClient16 Apr 2025
To set a timeout on HTTP requests in Java 11, you can use the HttpClient provided by the java.net.http module. The HttpClient API allows you to configure timeouts for requests in a convenient and standardized way. Here’s how you can do it: Set a Connection Timeout: This controls the timeout when establishing a connection to […]
HTTPCLIENT / NOTES</>↗HttpClient16 Apr 2025
In Java 11, the java.net.http package introduced the new HttpClient API, which simplifies working with HTTP requests and responses. To set custom headers for an HttpRequest, you can use the headers method or the setHeader method while building your request using the HttpRequest.Builder. Here’s a step-by-step guide for setting custom headers: Example Code package org.kodejava.net.http; […]
HTTPCLIENT / NOTES</>↗HttpClient15 Apr 2025
Handling HTTP response status codes with Java 11’s HttpClient API involves making a request, receiving a response, and then checking the status code returned in the response. Here’s how you can do this: Steps to Handle HTTP Response Status Codes Create the HttpClient: Build an instance of HttpClient. Build the Request: Define the HTTP request […]
CONCURRENCY / NOTES</>↗Concurrency15 Apr 2025
In Java, the Callable interface and Future interface are used in conjunction to run tasks asynchronously in a separate thread and fetch the result of the computation once it is complete. This is particularly useful when you need the task to return a result or throw a checked exception. Here’s a step-by-step guide to how […]
CONCURRENCY / NOTES</>↗Concurrency14 Apr 2025
When working with a multithreaded application, ConcurrentHashMap is a great choice for safely sharing data between threads. It is a thread-safe version of a HashMap that provides high concurrency for both retrieval and updates. Here are some guidelines to safely use a ConcurrentHashMap in a multithreaded environment: 1. Use Thread-Safe Access Operations ConcurrentHashMap ensures that […]
CONCURRENCY / NOTES</>↗Concurrency14 Apr 2025
The ScheduledExecutorService is a Java concurrency utility used for scheduling tasks to run after a delay or to execute periodically. Introduced in Java 5 as part of the java.util.concurrent package, it provides flexible scheduling functionality. Here’s how you can use it: 1. Getting an Instance of ScheduledExecutorService You can obtain an instance using the Executors […]