Skip to content

Instantly share code, notes, and snippets.

View Yatharth0045's full-sized avatar
🎯
Focusing

Yatharth Sharma Yatharth0045

🎯
Focusing
View GitHub Profile
@Yatharth0045
Yatharth0045 / docker-quickstart.sh
Last active August 3, 2021 17:06
This gist gives a quickstart over docker and helps in understanding the whole container lifecycle.
## Check docker server and client version
docker version
## Check for running containers | Currently, it should be empty
docker ps
## Check for images | Currently, it should be empty
docker images
## Now Pull an Image (Nginx) from DockerHub
@Yatharth0045
Yatharth0045 / playbook.yml
Created April 21, 2019 03:29
Playbook to show installation of java on different distributions.
---
# installing openjdk on diff OS.
# on debian
- apt:
name: openjdk-8-jdk
state: present
when: ansible_os_family == 'Debian'
@Yatharth0045
Yatharth0045 / FutureChaining.java
Created April 7, 2019 16:20
This program is to show the chaining of multiple completablefutures.
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
class Test {
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<Void> completableFuture = CompletableFuture.supplyAsync(() -> (int) (Math.random() * 10))
.thenApply(value -> {
System.out.println("Generated random number is " + value);
return value % 2 == 0;
})
@Yatharth0045
Yatharth0045 / TestThenRun.java
Last active April 7, 2019 16:04
This program is to show the use of theRun() method of completablefuture
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
class Test {
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> (int) (Math.random() * 10));
CompletableFuture<Void> voidCompletableFuture = completableFuture.thenRun(() -> {
System.out.println("Random number is generated.");
});
voidCompletableFuture.get();
@Yatharth0045
Yatharth0045 / TestThenAccept.java
Last active April 7, 2019 16:11
This program is to show the use of thenAccept() method of completablefuture
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
class Test {
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> (int) (Math.random() * 10));
CompletableFuture<Void> voidCompletableFuture = completableFuture.thenAccept(System.out::println);
voidCompletableFuture.get();
}
}
@Yatharth0045
Yatharth0045 / TestThenApply.java
Last active April 7, 2019 16:12
This program is to show the use of thenApply() method of completablefuture
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
class Test {
public static void main(String[] args) throws InterruptedException, ExecutionException {
CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> (int) (Math.random() * 10));
CompletableFuture<Boolean> completableFuture2 = completableFuture.thenApply(number -> number % 2 == 0);
System.out.println(completableFuture2.get() ? "Generated number is Even" : "Generated number is Odd");
}
}
@Yatharth0045
Yatharth0045 / ManualCompletionExample.java
Last active April 7, 2019 16:12
This program is to show the manual completion of completablefuture.
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
class Test {
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
try {
TimeUnit.SECONDS.sleep(1000);
} catch (InterruptedException ex) {
@Yatharth0045
Yatharth0045 / SupplyAsyncExample.java
Created April 3, 2019 01:29
An example to show runAsync method of completablefuture.
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
class Test {
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture completableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + " Runnable Call to be executed by another thread");
return "Result of the asynchronous computation";
});
System.out.println(Thread.currentThread().getName() + " Main Thread");
@Yatharth0045
Yatharth0045 / RunAsyncExample.java
Created April 3, 2019 01:22
An example to show runAsync method of completablefuture.
import java.util.concurrent.CompletableFuture;
class Test {
public static void main(String[] args) {
CompletableFuture completableFuture = CompletableFuture.runAsync(() -> {
System.out.println(Thread.currentThread().getName() + " Runnable Call to be executed by another thread");
});
System.out.println(Thread.currentThread().getName() + " Main Thread");
}
}
@Yatharth0045
Yatharth0045 / Executor-Thread-Pool.java
Last active March 26, 2019 13:13
This program shows the execution of ExecutorService class.
class Threads {
public static void main(String[] args) {
ExecutorService service = Executors.newFixedThreadPool(2);
for(int count = 0; count < 10; count++){
service.execute(()->{
System.out.println("Child Thread" + Thread.currentThread().getName());
});
}
service.shutdown();
System.out.println("|Main Thread|");