Skip to content

Commit 3c51713

Browse files
committed
#15 - Java
1 parent 9de89de commit 3c51713

File tree

2 files changed

+68
-5
lines changed

2 files changed

+68
-5
lines changed

Roadmap/15 - ASINCRONÍA/java/chartypes.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ private static Thread asynFunction(String funcitonName, int duration) {
1919
funcitonName + " started. Time: " + startTime +
2020
" milliseconds. Should take " + duration * 1000 + " milliseconds.");
2121
try {
22-
Thread.sleep((long) duration * 1000);
22+
Thread.sleep((long) duration * 10000);
2323
} catch (InterruptedException e) {
2424
System.out.println(e.getMessage());
2525
}
@@ -32,7 +32,7 @@ private static Thread asynFunction(String funcitonName, int duration) {
3232

3333
private static void exercise() {
3434
System.out.println("EXERCISE:");
35-
Thread thread = asynFunction("my function1", 20);
35+
Thread thread = asynFunction("my function1", 1);
3636
thread.start();
3737
try {
3838

@@ -58,9 +58,9 @@ private static void extra() {
5858
threadB.start();
5959
threadC.start();
6060

61-
threadA.join(5000);
62-
threadB.join(5000);
63-
threadC.join(5000);
61+
threadA.join();
62+
threadB.join();
63+
threadC.join();
6464

6565
System.out.println("Starting Function D...");
6666
threadD.start();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import java.time.LocalDateTime;
2+
import java.util.concurrent.ExecutorService;
3+
import java.util.concurrent.Executors;
4+
5+
public class danhingar {
6+
7+
public static void main(String[] args) throws InterruptedException {
8+
Thread thread = taskThread("1", 10);
9+
thread.start();
10+
taskExecutor("1", 10);
11+
12+
// Ejercicio extra
13+
extraThread();
14+
15+
}
16+
17+
private static Thread taskThread(String name, int seconds) {
18+
return new Thread(() -> {
19+
try {
20+
System.out.println("Tarea: " + name + ". Duración: " + seconds + "s. Inicio: " + LocalDateTime.now());
21+
Thread.sleep(seconds * 1000);
22+
System.out.println("Tarea: " + name + ". Fin: " + LocalDateTime.now());
23+
} catch (InterruptedException e) {
24+
e.printStackTrace();
25+
}
26+
});
27+
}
28+
29+
private static void taskExecutor(String name, int seconds) {
30+
ExecutorService threadpool = Executors.newCachedThreadPool();
31+
threadpool.submit(() -> {
32+
System.out.println("Tarea: " + name + ". Duración: " + seconds + "s. Inicio: " + LocalDateTime.now());
33+
try {
34+
Thread.sleep(seconds * 1000);
35+
} catch (InterruptedException e) {
36+
e.printStackTrace();
37+
}
38+
System.out.println("Tarea: " + name + ". Fin: " + LocalDateTime.now());
39+
});
40+
threadpool.shutdown();
41+
}
42+
43+
private static void extraThread() {
44+
Thread threadC = taskThread("C", 3);
45+
Thread threadB = taskThread("B", 2);
46+
Thread threadA = taskThread("A", 1);
47+
48+
threadC.start();
49+
threadB.start();
50+
threadA.start();
51+
52+
try {
53+
threadC.join();
54+
threadB.join();
55+
threadA.join();
56+
} catch (Exception e) {
57+
System.out.println("Error al terminar la ejecución");
58+
}
59+
60+
Thread threadD = taskThread("D", 1);
61+
threadD.start();
62+
}
63+
}

0 commit comments

Comments
 (0)