27
loading...
This website collects cookies to deliver better user experience
package com.bazlur;
import java.util.concurrent.TimeUnit;
public class Day024_Playground {
public static void main(String[] args) {
var thread = new Thread(() -> {
while (true) {
//this thread is supposed to be running forever
sleep(1);
System.out.println(Thread.currentThread().getName()
+ " is daemon? "
+ Thread.currentThread().isDaemon());
}
});
thread.setDaemon(true);
thread.start();
System.out.println("Running from main method");
sleep(5);
System.out.println("Done! will die now");
}
private static void sleep(int second) {
try {
TimeUnit.SECONDS.sleep(second);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
main method is running from Thread [main,5,main]
Running from main method
Thread-0 is daemon? true
Thread-0 is daemon? true
Thread-0 is daemon? true
Thread-0 is daemon? true
Done! will die now
CompletableFuture
. Using it, we can execute a piece of code asynchronously.runAsync()
, supplyAsync()
. Example-CompletableFuture<?> future = CompletableFuture.runAsync(() -> {
System.out.println("Running asynchronously!");
});
CompletableFuture<Integer> meaningOfLife = CompletableFuture.supplyAsync(() -> {
return 42;
});
package com.bazlur;
import java.util.concurrent.CompletableFuture;
public class Day024_Playground2 {
public static void main(String[] args) {
CompletableFuture.runAsync(() -> {
System.out.println("Running asynchronously!");
System.out.println("from " + Thread.currentThread());
});
}
}
Running asynchronously!
from Thread[ForkJoinPool.commonPool-worker-19,5,main]
package com.bazlur;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class Day024_Playground2 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture.runAsync(() -> {
System.out.println("Running asynchronously!");
System.out.println("from " + Thread.currentThread());
}).get();
}
}
package com.bazlur;
import java.util.concurrent.CompletableFuture;
public class Day024_Playground2 {
public static void main(String[] args) throws InterruptedException {
CompletableFuture.runAsync(() -> {
System.out.println("Running asynchronously!");
System.out.println("from " + Thread.currentThread());
});
System.out.println("Doing some important work!");
Thread.sleep(1000);
}
}
CompletableFuture
. Assuming the CompletableFuture
won’t take longer than 1000 milliseconds.package com.bazlur;
import java.util.concurrent.CompletableFuture;
public class Day024_Playground2 {
public static void main(String[] args) throws InterruptedException {
CompletableFuture.runAsync(() -> {
System.out.println("Running asynchronously!");
System.out.println("from " + Thread.currentThread());
System.out.println("Is it a daemon thread? "+ Thread.currentThread().isDaemon());
});
System.out.println("Doing some important work!");
Thread.sleep(1000);
}
}
CompletableFuture
. This is because they will not live to finish their work.CompletableFuture
do some asynchronous jobs, but the user thread from which they are created dies early?package com.bazlur;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
public class Day024_Playground2 {
public static void main(String[] args) throws InterruptedException {
var threadPool = Executors.newCachedThreadPool();
CompletableFuture.runAsync(() -> {
System.out.println("Running asynchronously!");
sleep();
System.out.println("from " + Thread.currentThread());
}, threadPool);
System.out.println("I'm Done!! going to die.");
threadPool.shutdown();
}
private static void sleep() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
CompletableFuture
; it will finish its job and then it would die.