Skip to content

Instantly share code, notes, and snippets.

View viveknaskar's full-sized avatar
🎯
Focusing

Vivek Naskar viveknaskar

🎯
Focusing
View GitHub Profile
@viveknaskar
viveknaskar / ThenApplyChainingExample.java
Created September 20, 2022 21:48
Illustration of thenApply method chaining for CompletableFuture
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
public class ThenApplyChainingExample {
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
try { // delaying the thread by 2 seconds
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
@viveknaskar
viveknaskar / ThenApplyExample.java
Last active September 20, 2022 21:39
Simple Illustration of thenApply method for CompletableFuture
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
public class ThenApplyExample {
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
try {
// delaying the thread by 2 seconds
TimeUnit.SECONDS.sleep(2);
@viveknaskar
viveknaskar / SupplyAsyncExample.java
Last active September 20, 2022 21:26
Simple Illustration of supplyAsync method for CompletableFuture
import java.util.concurrent.*;
public class SupplyAsyncExample {
public static void main(String[] args) throws ExecutionException, InterruptedException {
// creating an executor to use thread pools to execute tasks
Executor executor = Executors.newFixedThreadPool(3);
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
try {
// fetching the name of the Thread
System.out.println("Executing from: " +
@viveknaskar
viveknaskar / RunAsyncExample.java
Last active September 20, 2022 04:51
Simple Illustration of runAsync method for CompletableFuture
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
public class RunAsyncExample {
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
try {
// delaying the thread by 2 seconds
TimeUnit.SECONDS.sleep(2);
@viveknaskar
viveknaskar / SimpleCompletableFutureExample.java
Created September 18, 2022 15:23
Simplest example of CompletableFuture
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class SimpleCompletableFutureExample {
public static void main(String[] args) {
CompletableFuture<String> completableFuture = new CompletableFuture<>();
try {
// we can get the result using CompletableFuture.get()
@viveknaskar
viveknaskar / vm.tf
Created March 22, 2022 12:30
A sample code snippet for provisioning compute instance in GCP using terraform
resource "google_compute_instance" "default" {
name = "compute_engine"
machine_type = "e2-medium"
zone = "us-central1-a"
boot-disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
@viveknaskar
viveknaskar / main.tf
Created March 22, 2022 12:24
Sample code snippet for terraform block for explain a tf file
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "4.14.0"
}
}
}
provider "google" {
@viveknaskar
viveknaskar / unsubscribe-youtube-channels.js
Created February 17, 2022 16:06
A script that can be executed to unsubscribe all the YouTube channels at once.
var i = 0;
var count = document.querySelectorAll(
"ytd-channel-renderer:not(.ytd-item-section-renderer)"
);
myTimer();
function myTimer() {
if (count == 0) return;
el = document.querySelector(".ytd-subscribe-button-renderer");
el.click();
setTimeout(function () {
@viveknaskar
viveknaskar / sample.yaml
Last active October 3, 2021 16:45
An illustration of a sample YAML file
---
# This is a comment for illustrating a YAML file
name: Vivek Naskar
designation: "Software Developer"
age: 29
company: Google
frameworks:
- Spring Boot
- Django
- nodeJs
@viveknaskar
viveknaskar / SingletonDeserializationPreventExample.java
Created May 17, 2021 18:03
Illustration of preventing the breaking of Singleton pattern using Serialization & Deserialization
import java.io.*;
/**
* Preventing the breaking of SingletonPattern due to Deserialization
* by implementing readResolve() to return the current instance
*/
public class SingletonDeserializationPreventExample implements Serializable {
private static SingletonDeserializationPreventExample single_instance = null;