Found 2616 Articles for Java

When to use the delayedExecutor() method of CompletableFuture in Java 9?

raja
Updated on 09-Mar-2020 10:59:05

2K+ Views

The delayedExecutor() method has been added to the CompletableFuture class in Java 9. CompletableFuture defines two overloaded methods of delayedExecutor(): first method returns an Executor object from the default Executor object that CompletableFuture object uses to execute the task after the delay and new Executor object can do task execution whereas the second method also returns an Executor object but is an Executor object that we pass into this method after the delay and new Executor object can also do task execution.Syntaxpublic static Executor delayedExecutor(long delay, TimeUnit unit, Executor executor) public static Executor delayedExecutor(long delay, TimeUnit unit)Exampleimport java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; ... Read More

How can we implement the SubmissionPublisher class in Java 9?

raja
Updated on 09-Mar-2020 09:45:11

587 Views

Since Java 9, we can create Reactive Streams by introducing four core interfaces: Publisher, Subscriber, Subscription, Processor, and one concrete class: SubmissionPublisher that implements the Publisher interface. Each interface plays a different role, corresponding to the principles of Reactive Streams. We can use the submit() method of SubmissionPublisher class to publish the provided item to each subscriber.Syntaxpublic class SubmissionPublisher extends Object implements Flow.Publisher, AutoCloseableIn the below example, we can implement the SubmissionPublisher classExampleimport java.util.concurrent.Flow.Subscriber; import java.util.concurrent.Flow.Subscription; import java.util.concurrent.SubmissionPublisher; class MySubscriber implements Subscriber {    private Subscription subscription;    private String name;    public MySubscriber(String name) {       this.name = name;   ... Read More

What are the core interfaces of Reactive Streams in Java 9?

raja
Updated on 09-Mar-2020 06:36:32

501 Views

Java 9 has introduced Reactive Streams under java.util.concurrent.Flow package that supports an interoperable publish-subscribe framework. It processes an asynchronous stream of data across the asynchronous boundary (passing elements into another thread or thread-pool), and the receiving side is not forced to buffer arbitrary amounts of data, then buffer overflow can't occur.Flow API contains four interrelated core interfaces: Publisher, Subscriber, Subscription, and Processor.Syntax@FunctionalInterface public static interface Publisher {    public void subscribe(Subscriber

How to retrieve all processes data of Process API in Java 9?

raja
Updated on 09-Mar-2020 05:36:55

459 Views

In Java 9, Process API has been used to control and manage operating system processes. ProcessHandle class provides the process’s native process ID, start time, accumulated CPU time, arguments, command, user, parent process, and descendants. It also provides a method to check processes liveness and to destroy processes. We retrieve all ProcessHandle data as a stream by using the allProcesses() method.In the below example, we retrieve all the processes information.Exampleimport java.util.stream.Stream; import java.util.Optional; import java.util.stream.Stream; public class AllProcessesTest {    public static void main(String args[]) throws InterruptedException {       System.out.println("---------------------------");       System.out.println("All Processes:");       Stream processStream = ProcessHandle.allProcesses();       processStream.forEach(process -> ... Read More

How to get all children of a process using Process API in Java 9?

raja
Updated on 06-Mar-2020 18:23:41

1K+ Views

Process handling classes and related API have been introduced in Java 9. We can use the ProcessHandle interface and related methods to get pid and information about other related processes. We need to get all children of a process, then use java.lang.ProcessHandle.children() method. This method returns a stream, typically a process that has no children.In the below example, we can get the first process and retrieve its children's process information.Exampleimport java.util.stream.Stream; import java.util.Optional; public class ChilderenProcessTest {    public static void main(String args[]) throws InterruptedException {       System.out.println("---------------------------");       System.out.println("Children Processes:");       Optional processHandle = ... Read More

How to terminate/destroy a process using Process API in Java 9?

raja
Updated on 06-Mar-2020 12:28:44

1K+ Views

In Java 9, Process API supports an easy way to get much information about a process. ProcessHandle interface can identify and provide the control of native processes and method to check processes liveness and destroy the processes whereas ProcessHandle.Info interface can give an Information snapshot of a process. We need to destroy a process by using the destroy() method of the ProcessHandle interface.In the below example, we need to terminate a process by using the ProcessHandle interface.Exampleimport java.io.File; import java.io.IOException; import java.util.Objects; public class DestroyProcessTest {    public static void main(String[] args) throws InterruptedException {       System.out.println("---------------------------");       ... Read More

Importance of ofInstant() method in Java 9?

raja
Updated on 06-Mar-2020 10:35:54

140 Views

In Java 9, the ofInstant() method has introduced for conversion. It is a static method of LocalDate, LocalTime, and LocalDateTime classes. This method converts java.time.Instant object to LocalDate that requires a time zone in the form of java.time.ZoneId.Syntaxpublic static LocalTime ofInstant(Instant instant, ZoneId zone) public static LocalDate ofInstant(Instant instant, ZoneId zone) public static LocalDateTime ofInstant(Instant instant, ZoneId zone)Exampleimport java.time.LocalDate; import java.time.LocalTime; import java.time.LocalDateTime; import java.time.Instant; import java.time.ZoneId; public class OfInstantMethodTest {    public static void main(String args[]) { Instant instant = Instant.parse("2020-02-05T02:35:15.245Z"); System.out.println("Instant: " + instant); ... Read More

What is the use of the toEpochSecond() method in Java 9?

raja
Updated on 06-Mar-2020 08:23:43

599 Views

In Java 9, the LocalDate class provides the toEpochSecond() method to convert local date into epoch seconds. The toEpochSecond() method converts the LocalDate to a number of seconds since the epoch 1970-01-01T00:00:00Z. The LocalDate can be combined with a given time and zone offset to calculate seconds starting from 1970-01-01T00:00:00Z.Syntaxpublic long toEpochSecond(LocalTime time, ZoneOffset offset)Exampleimport java.time.LocalDate; import java.time.LocalTime; import java.time.ZoneOffset; public class ToEpochSecondMethodTest {    public static void main(String args[]) {       LocalDate date = LocalDate.now();       LocalTime time = LocalTime.now();       System.out.println("LocalDate toEpochSecond : " + date.toEpochSecond(time, ZoneOffset.of("Z")));       ... Read More

What are the enhancements in Internationalization in Java 9?

raja
Updated on 06-Mar-2020 07:32:25

79 Views

Internationalization enhancements in Java 9 include Unicode 8.0, UTF-8 properties files and enabling CLDR locale data by default. Java 9 supports up to Unicode 8.0 standards with 10, 555 characters, 29 scripts, and 42 blocks.In Java 9, the properties files are loaded in UTF-8 encoding. By default, reading an input stream throws MalformedInputException or UnmappableCharacterException. In this case, PropertyResourceBundle instance reset to a state before the exception, re-reads the input stream in ISO-8859-1, and continues reading.If PropertyResourceBundle.encoding has set to either ISO-8859-1 or UTF-8, then PropertyResourceBundle instance read an input stream in that encoding, and throw an exception if encountering an invalid sequence. The system property read ... Read More

How to access each stack element of StackWalker in Java 9?

raja
Updated on 05-Mar-2020 13:37:55

170 Views

Java 9 introduced StackWalker API as an alternative to Thread.getStackTrace() or Throwable.getStackTrace() and SecurityManager.getClassContext(). This API targets a mechanism to traverse and materialize required stack frames allowing efficient lazy access to additional stack frames when required.If we need to access each stack element of an exception stack trace, then we can use the getStackTrace() method of Throwable class. It returns an array of StackTraceElement.Exampleimport java.util.*; // Test1 class class Test1 {    public void test() throws Exception {       Test2 test2 = new Test2();       test2.test();    } } // Test2 class class Test2 {    public void test() ... Read More

Advertisements