Skip to content

Instantly share code, notes, and snippets.

View wkdalsgh192's full-sized avatar
🏠
Working from home

Blue_Avocado wkdalsgh192

🏠
Working from home
View GitHub Profile
// Creata a hash map class and put values into the map
Map<String, String> map = new HashMap<>();
map.put("1", "A");
map.put("2", "B");
map.put("3", "C");
// Create a class that only contains a collection.
public class GameRanking {
// 2. Pass a function as a parameter
function sayHello() {
return "Hello, ";
}
function greeting(helloMessage, name) {
console.log(helloMessage() + name);
}
greeting(sayHello, "JavaScript!");
// 1. Assign a function to a variable
const sayHello = function() {
console.log("Hello");
return "Hello, ";
}
// Invoke it using the variable
sayHello();
// Using a callback method
CompletableFuture<String> future4 = CompletableFuture.supplyAsync(() -> {
System.out.println("Hello "+ Thread.currentThread().getName());
return "Hello";
}).thenApply((s) -> { // thenApply, thenAccept or thenRun can also be used here
System.out.println(Thread.currentThread().getName());
return s.toUpperCase(Locale.ROOT);
});
System.out.println(future4.get());
// when there is no return value
CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> {
System.out.println("Hello "+ Thread.currentThread().getName());
});
future2.get(); // get() should be called to execute the thread
// when there is a return value
CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> {
System.out.println("HEllo " + Thread.currentThread().getName());
ExecutorService executorService = Executors.newSingleThreadExecutor();
Callable<String> hello = () -> {
Thread.sleep(2000L);
return "Hello";
};
Callable<String> java = () -> {
Thread.sleep(3000L);
return "Java";
// new single thread created and managed by executor service
ExecutorService executorService = Executors.newSingleThreadExecutor();
Callable<String> hello = () -> {
Thread.sleep(2000L);
return "Hello";
};
Future<String> submit = executorService.submit(hello); // callable object executed and falls asleep for 2 sec
System.out.println(submit.isDone()); // main thread keep running - 1
Thread newThread = new Thread(() -> {
System.out.println("Thread : "+ Thread.currentThread().getName());
try {
Thread.sleep(2000L);
} catch (InterruptedException e) {
System.out.println("Exit!");
return;
}
});
Thread newThread = new Thread(() -> {
while (true) {
System.out.println("Thread : "+ Thread.currentThread().getName());
try {
Thread.sleep(2000L);
} catch (InterruptedException e) {
System.out.println("Exit!");
return;
}
}
Thread thread = new Thread(() -> {
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread : "+ Thread.currentThread().getName());
});