Differences Between CompletableFuture and Future in Java 9

raja
Updated on 01-May-2020 11:45:14

6K+ Views

CompletableFuture class implements Future interface in Java. CompletableFuture can be used as a Future that has explicitly completed. The Future interface doesn’t provide a lot of features, we need to get the result of asynchronous computation using the get() method, which is blocked, so there is no scope to run multiple dependent tasks in a non-blocking fashion whereas CompleteFuture class can provide the functionality to chain multiple dependent tasks that run asynchronously, so we can create a chain of tasks where the next task is triggered when the result of the current task is available.Syntaxpublic class CompletableFuture extends Object implements Future, CompletionStageExampleimport java.util.function.Supplier; import java.util.concurrent.CompletableFuture; ... Read More

Get Snapshot of Process API in Java 9

raja
Updated on 01-May-2020 08:33:51

246 Views

Java 9 has improved Process API by including new methods and introduced new interfaces ProcessHandle and ProcessHandle.Info to get all the details regarding the process and its information.ProcessHandle interface can identify and provide control of native processes. Each individual process can be monitored for liveness, listed its children, get information about the process, or destroys it. ProcessHandle.Info interface gives information snapshots about a process.SyntaxProcessHandle.Info info()Examplepublic class ProcessSnapShotTest {    public static void main(String[] args) {       ProcessHandle currentProcessHandleImpl = ProcessHandle.current();             // Process snapshot of the current running process with ProcessHandle.Info:       ProcessHandle.Info processInfo = ... Read More

Importance of destroyForcibly Method in Java 9

raja
Updated on 30-Apr-2020 17:21:25

453 Views

The destroyForcibly() method can be used to kill a process. It will be needed if the process has finished or has frozen. For instance, the isAlive() method returns true after destroyForcibly() is called. The destroyForcibly() method returns true if the termination successfully requested, otherwise returns false.Syntaxboolean destroyForcibly()In the below example, we will able to launch a notepad application, and it will be terminated after the destroyForcibly() method called.Exampleimport java.io.IOException; import java.lang.ProcessBuilder; public class DestroyForciblyTest {    public static void main(String args[]) throws IOException, InterruptedException {       ProcessBuilder pBuilder = new ProcessBuilder();       pBuilder.command("notepad.exe");       ... Read More

Remove Zero Sum Consecutive Nodes from Linked List in C++

Arnab Chakraborty
Updated on 30-Apr-2020 15:51:36

2K+ Views

Suppose we have given the head of a linked list; we have to repeatedly delete consecutive sequences of nodes that sum to 0 until there are no such sequences. So after doing so, we have to return the head of the final linked list. So if the list is like [1, 2, -3, 3, 1], then the result will be [3, 1].To solve this, we will follow these steps −Create a node called dummy, and store 0 into it, set next of dummy := headcreate one map m, store dummy for the key 0 into m, set sum = 0while ... Read More

Swap for Longest Repeated Character Substring in C++

Arnab Chakraborty
Updated on 30-Apr-2020 15:46:05

585 Views

Suppose we have a string text, so we are allowed to swap two of the characters in the string. We have to find the length of the longest substring with repeated characters. So if the input is like “ababa”, then the result will be 3, as if we swap first b with last a, or the last b with first a, then the longest repeated character will be “aaa”, so the length is 3.To solve this, we will follow these steps −Define a map cnt, set ret := 1, j := 0, n := size of text, v := 0, ... Read More

Longest Common Subsequence in C++

Arnab Chakraborty
Updated on 30-Apr-2020 15:41:22

494 Views

Suppose we have two strings text1 and text2, we have to return the length of their longest common subsequence. The ubsequence of a string is a new string generated from the original string with some characters deleted without changing the relative order of the remaining characters. (So for example "abe" is a subsequence of "abcde" but "adc" is not). A common subsequence of two strings is a subsequence that is common to both strings. So If there is no common subsequence, return 0. If the input is like “abcde”, and “ace”, then the result will be 3.To solve this, we ... Read More

Maximum of Absolute Value Expression in C++

Arnab Chakraborty
Updated on 30-Apr-2020 15:36:17

807 Views

Suppose we have two arrays of integers with equal lengths, we have to find the maximum value of: |arr1[i] - arr1[j]| + |arr2[i] - arr2[j]| + |i - j|. Where the maximum value is taken over all 0

Brace Expansion in C++

Arnab Chakraborty
Updated on 30-Apr-2020 15:32:58

392 Views

Suppose we have a string S that represents a list of words. Here each letter in the word has 1 or more options. If there is only one option, the letter is represented as is. If there is more than one option, then curly braces delimit the options. So for example, "{a, b, c}" will represent the options ["a", "b", "c"]. Now for example, if the input is like "{a, b, c}d{e, f}" this will represent the list ["ade", "adf", "bde", "bdf", "cde", "cdf"].Return all words that can be formed in this manner, in lexicographical order.To solve this, we will ... Read More

Lexicographically Smallest Equivalent String in C++

Arnab Chakraborty
Updated on 30-Apr-2020 15:29:20

798 Views

Suppose we have strings A and B of the same length, now we can say A[i] and B[i] are equivalent characters. So for example, if A = "abc" and B = "cde", then we have 'a' = 'c', 'b' = 'd' and 'c' = 'e'. The equivalent characters follow the usual rules of any equivalence relation:Reflexivity: 'a' = 'a'Symmetry: 'a' = 'b' implies 'b' = 'a'Transitivity: 'a' = 'b' and 'b' = 'c' implies 'a' = 'c'Now for example, given the equivalency information from A and B above, S = "eed", "acd", and "aab" are equivalent strings, and "aab" is ... Read More

Longest Repeating Substring in C++

Arnab Chakraborty
Updated on 30-Apr-2020 15:28:46

2K+ Views

Suppose we have a string S, we have to find the length of the longest repeating substring(s). We will return 0 if no repeating substring is present. So if the string is like “abbaba”, then the output will be 2. As the longest repeating substring is “ab” or “ba”.Return all words that can be formed in this manner, in lexicographical order.To solve this, we will follow these steps −n := size of Sset S := one blank space concatenated with Sset ret := 0create one matrix dp of size (n + 1) x (n + 1)for i in range 1 ... Read More

Advertisements