This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 2. Pass a function as a parameter | |
function sayHello() { | |
return "Hello, "; | |
} | |
function greeting(helloMessage, name) { | |
console.log(helloMessage() + name); | |
} | |
greeting(sayHello, "JavaScript!"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. Assign a function to a variable | |
const sayHello = function() { | |
console.log("Hello"); | |
return "Hello, "; | |
} | |
// Invoke it using the variable | |
sayHello(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ExecutorService executorService = Executors.newSingleThreadExecutor(); | |
Callable<String> hello = () -> { | |
Thread.sleep(2000L); | |
return "Hello"; | |
}; | |
Callable<String> java = () -> { | |
Thread.sleep(3000L); | |
return "Java"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Thread newThread = new Thread(() -> { | |
System.out.println("Thread : "+ Thread.currentThread().getName()); | |
try { | |
Thread.sleep(2000L); | |
} catch (InterruptedException e) { | |
System.out.println("Exit!"); | |
return; | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Thread thread = new Thread(() -> { | |
try { | |
Thread.sleep(1000L); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
System.out.println("Thread : "+ Thread.currentThread().getName()); | |
}); |