Found 7442 Articles for Java

Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range

Rushi Javiya
Updated on 04-Jul-2023 15:51:49

270 Views

The naïve approach to finding the prime numbers in the given range is to check whether each number is prime. Also, we need to make iterations equal to the given number to check whether the number is prime. So, the naïve approach is very time-consuming, and we need to optimize it to make it time efficient. In this tutorial, we will learn wheel factorization and Sieve of Eratosthenes algorithms given by Sieve to find the prime numbers in the given range efficiently. Problem statement − We have given left and right integer values. We need to implement the Wheel factorization ... Read More

Java Program to Find All Palindromic Sub-Strings of a String

Shriansh Kumar
Updated on 16-Aug-2024 07:52:17

1K+ Views

In this problem, we are given a String and our task is to find all palindromic sub-strings of the specified length. There are two ways to solve the problem. The first way is to compare the characters of sub-string from start to last, and another way is to reverse the sub-string and compare it with the original sub-string to check whether it is palindrome. String in Java is a class which represents a sequence of characters. It is immutable which means once it is created a String object cannot be changed. And, sub-string is a small portion or part of ... Read More

Java program to find 2 elements in the array such that difference between them is largest

Rushi Javiya
Updated on 30-Aug-2024 11:41:20

563 Views

In this problem, we will find two array elements such that the difference between them is maximum using Java. We can make a pair of each element and find the difference between the elements of each pair. After that, we can take a pair whose element has a maximum difference. Another approach is to sort the array and take the largest and smallest elements from the array. Problem statement  We have given an array containing the integer values. We need to find two array elements to maximize the difference between them. Input 1 array[] = { 50, 10, 30, ... Read More

ToDoubleFunction Interface in Java with Examples

Sabid Ansari
Updated on 19-Jun-2023 12:41:47

437 Views

Understanding and effectively utilizing Java's functional interfaces is an essential skill for any modern Java developer. Among these interfaces, the ToDoubleFunction interface is a significant tool that offers tremendous utility. This article aims to provide a comprehensive exploration of the ToDoubleFunction interface in Java, enriched with practical examples to enhance your understanding. What is the ToDoubleFunction Interface? Introduced in Java 8, the ToDoubleFunction interface is a part of the java.util.function package. It represents a function that accepts an argument of one type and produces a double-valued result. Its primary use is in lambda expressions and method references where a function ... Read More

TimeUnit Class in Java with Examples

Sabid Ansari
Updated on 19-Jun-2023 12:05:04

1K+ Views

Introduction In Java, the manipulation and handling of time is a common requirement in programming tasks. The TimeUnit class, part of java.util.concurrent package, plays a crucial role in this aspect by providing a set of methods for converting time across different units. In this article, we delve into the TimeUnit class, its applications, and practical examples to illustrate its usefulness. Understanding TimeUnit in Java The TimeUnit class in Java provides methods for time conversions and thread-sleep operations with better readability and precision than standard approaches. TimeUnit defines the following time units: DAYS, HOURS, MICROSECONDS, MILLISECONDS, MINUTES, NANOSECONDS, and SECONDS, each ... Read More

Throttling Task Submission Rate with ThreadPoolExecutor and Semaphore in Java

Sabid Ansari
Updated on 19-Jun-2023 11:58:55

388 Views

Introduction Java concurrency provides several classes and tools that allow developers to create multi-threaded applications. Among these are the ThreadPoolExecutor and Semaphore classes. The former is used to manage a pool of worker threads, while the latter can limit the number of threads accessing a certain resource at a given time. This article delves into throttling the task submission rate using these two Java classes. By understanding how to effectively manage threads and control their execution, you can significantly optimize your Java applications. Understanding ThreadPoolExecutor and Semaphore Before we jump into how to throttle the task submission rate, it's essential ... Read More

ThreadLocalRandom vs SecureRandom Class in Java

Sabid Ansari
Updated on 19-Jun-2023 11:55:37

878 Views

Are you embarking on a journey into the realm of multi-threaded programming in Java? Are you finding yourself tangled in the web of classes Java offers for generating random numbers, such as ThreadLocalRandom and SecureRandom? Fear not! This article will break down the differences, similarities, and suitable use-cases for these two classes, ensuring you choose the right tool for your needs. Understanding ThreadLocalRandom in Java Java's ThreadLocalRandom class was introduced in Java 7 to handle the generation of random numbers in a multi-threaded environment more efficiently. The class is part of the java.util.concurrent package and is essentially a stripped-down version ... Read More

ThreadFactory Interface in Java with Examples

Sabid Ansari
Updated on 19-Jun-2023 11:51:57

1K+ Views

Introduction The ThreadFactory interface in Java is a vital component of multithreaded programming. This powerful interface creates a new thread on demand and can be customized to suit different requirements. This article aims to provide a comprehensive understanding of this interface, along with several practical examples. By the end of this piece, you'll have a firm grasp on the ThreadFactory interface and its usage in Java programming. Understanding Java ThreadFactory Interface Before delving into examples, let's start by understanding the basic concepts. What is the ThreadFactory Interface? The ThreadFactory interface is part of Java's java.util.concurrent package. It's designed to create ... Read More

Thread Safety and how to achieve it in Java

Sabid Ansari
Updated on 19-Jun-2023 11:38:05

2K+ Views

In the world of multithreaded programming, managing concurrent access to shared data is a considerable challenge. An essential aspect of this challenge is achieving thread safety. This article explores the concept of thread safety in Java and provides comprehensive guidance on how to ensure your Java code is thread-safe. Understanding Thread Safety Thread safety refers to the property of an object that guarantees safe execution by multiple threads concurrently, without causing any problems such as data inconsistencies or race conditions. When a piece of code is thread-safe, it functions correctly even when accessed by multiple threads simultaneously. A code segment ... Read More

Thread Interference and Memory Consistency Errors in Java

Sabid Ansari
Updated on 19-Jun-2023 11:35:31

533 Views

Java's multithreading capabilities can significantly enhance an application's performance and responsiveness. However, when multiple threads share and manipulate the same data, developers can face complex issues, notably thread interference and memory consistency errors. This article takes an in-depth look at these concepts and offers solutions to mitigate such challenges in your Java applications. Thread Interference in Java: The Race Condition Thread interference, also known as race condition, is a common issue in multithreaded environments. It occurs when two or more threads access shared data simultaneously, leading to unreliable and unexpected results. Suppose we have two threads that both increment the ... Read More

Advertisements