├── .gitignore
├── .idea
├── .gitignore
├── java-intensivo.iml
├── misc.xml
├── modules.xml
└── vcs.xml
├── CompletableFutureExample.java
├── HttpClient.java
├── Main.java
├── MultiThreadExample.java
└── PokemonAbilityFetch.java
/.gitignore:
--------------------------------------------------------------------------------
1 | *.class
2 | aws.xml
--------------------------------------------------------------------------------
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
--------------------------------------------------------------------------------
/.idea/java-intensivo.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/CompletableFutureExample.java:
--------------------------------------------------------------------------------
1 | import java.util.concurrent.CompletableFuture;
2 | import java.util.concurrent.ExecutionException;
3 |
4 | public class CompletableFutureExample {
5 | public void run() throws ExecutionException, InterruptedException {
6 | CompletableFuture pikachu1 = CompletableFuture.runAsync(() -> {
7 | PokemonAbilityFetch pokemonAbilityFetch = new PokemonAbilityFetch("pikachu");
8 | String response = pokemonAbilityFetch.fetch();
9 | });
10 |
11 | CompletableFuture pikachu = CompletableFuture.supplyAsync(() -> {
12 | PokemonAbilityFetch pokemonAbilityFetch = new PokemonAbilityFetch("pikachu");
13 | String response = pokemonAbilityFetch.fetch();
14 | return response;
15 | });
16 | CompletableFuture charmander = CompletableFuture.supplyAsync(() -> {
17 | PokemonAbilityFetch pokemonAbilityFetch = new PokemonAbilityFetch("charmander");
18 | String response = pokemonAbilityFetch.fetch();
19 | return response;
20 | });
21 |
22 | CompletableFuture bulbasaur = CompletableFuture.supplyAsync(() -> {
23 | PokemonAbilityFetch pokemonAbilityFetch = new PokemonAbilityFetch("bulbasaur");
24 | String response = pokemonAbilityFetch.fetch();
25 | return response;
26 | });
27 |
28 | CompletableFuture ditto = CompletableFuture.supplyAsync(() -> {
29 | PokemonAbilityFetch pokemonAbilityFetch = new PokemonAbilityFetch("ditto");
30 | String response = pokemonAbilityFetch.fetch();
31 | return response;
32 | });
33 |
34 | pikachu1.join();
35 |
36 | CompletableFuture allPokemons = CompletableFuture.allOf(pikachu, charmander, bulbasaur, ditto);
37 | allPokemons.thenRun(() -> {
38 | try {
39 | System.out.println("Pikachu: " + pikachu.get());
40 | System.out.println("Charmander: " + charmander.get());
41 | System.out.println("Bulbasaur: " + bulbasaur.get());
42 | System.out.println("Ditto: " + ditto.get());
43 | } catch (InterruptedException | ExecutionException e) {
44 | e.printStackTrace();
45 | }
46 | }).get();
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/HttpClient.java:
--------------------------------------------------------------------------------
1 | import java.io.IOException;
2 | import java.net.URI;
3 | import java.net.http.*;
4 | import java.time.Duration;
5 |
6 | public class HttpClient {
7 | private final java.net.http.HttpClient client = java.net.http.HttpClient.newHttpClient();
8 | private final HttpResponse.BodyHandler responseHandler;
9 |
10 | public HttpClient(HttpResponse.BodyHandler responseHandler) {
11 | this.responseHandler = responseHandler;
12 | }
13 |
14 | public T get(String url) throws IOException, InterruptedException {
15 | HttpRequest request = HttpRequest.newBuilder()
16 | .GET()
17 | .timeout(Duration.ofSeconds(10))
18 | .uri(URI.create(url))
19 | .build();
20 |
21 | HttpResponse response = client.send(request, responseHandler);
22 | return response.body();
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/Main.java:
--------------------------------------------------------------------------------
1 | import java.util.ArrayList;
2 | import java.util.List;
3 | import java.util.concurrent.CompletableFuture;
4 | import java.util.concurrent.ExecutionException;
5 |
6 | public class Main {
7 |
8 | public static void main(String[] args) throws InterruptedException, ExecutionException {
9 | MultiThreadExample multiThreadExample1 = new MultiThreadExample("Thread 1");
10 | MultiThreadExample multiThreadExample2 = new MultiThreadExample("Thread 2");
11 | MultiThreadExample multiThreadExample3 = new MultiThreadExample("Thread 3");
12 |
13 | Thread thread1 = new Thread(multiThreadExample1);
14 | Thread thread2 = new Thread(multiThreadExample2);
15 | Thread thread3 = new Thread(multiThreadExample3);
16 |
17 | thread1.start();
18 | thread2.start();
19 | thread3.start();
20 |
21 | for (int i = 0; i < 100; i++) {
22 | System.out.println("Main thread Valor atual do loop " + i);
23 | }
24 |
25 | thread1.join();
26 | thread2.join();
27 | thread3.join();
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/MultiThreadExample.java:
--------------------------------------------------------------------------------
1 | public class MultiThreadExample implements Runnable {
2 |
3 | String threadName;
4 |
5 | public MultiThreadExample(String threadName){
6 | this.threadName = threadName;
7 | }
8 |
9 | @Override
10 | public void run() {
11 | for (int i = 0; i < 10; i++) {
12 | System.out.println(this.threadName + " Valor atual do loop " + i);
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/PokemonAbilityFetch.java:
--------------------------------------------------------------------------------
1 | import java.io.IOException;
2 | import java.net.http.HttpResponse;
3 |
4 | public class PokemonAbilityFetch {
5 | private final String pokemonName;
6 | private final HttpClient httpClient = new HttpClient<>(HttpResponse.BodyHandlers.ofString());
7 |
8 | public PokemonAbilityFetch(String pokemonName){
9 | this.pokemonName = pokemonName;
10 | }
11 |
12 | public String fetch() {
13 | try {
14 | String apiUrl = "https://pokeapi.co/api/v2/pokemon/";
15 | String response = httpClient.get(apiUrl + pokemonName);
16 | printPokemonAbilities(response, pokemonName);
17 | return response.substring(0, 10);
18 | } catch (IOException e) {
19 | throw new RuntimeException(e);
20 | } catch (InterruptedException e) {
21 | throw new RuntimeException(e);
22 | }
23 | }
24 |
25 |
26 | private static void printPokemonAbilities(String jsonResponse, String pokemonName) {
27 | // Simplified string parsing to extract abilities
28 | String abilitiesSection = jsonResponse.split("\"abilities\":\\[")[1].split("],")[0];
29 | String[] abilities = abilitiesSection.split("\\},\\{");
30 |
31 | for (int i = 0; i < abilities.length; i++) {
32 | String abilityName = abilities[i].split("\"name\":\"")[1].split("\"")[0];
33 | System.out.println(pokemonName + " ability " + (i + 1) + ": " + abilityName);
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------