Skip to content

Commit 12018a3

Browse files
committed
#15 - Java
1 parent ac0d918 commit 12018a3

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
public class h4ckxel{
2+
3+
public static void main(String[] args) {
4+
try {
5+
exercise();
6+
extra();
7+
8+
} catch (Exception e) {
9+
System.out.println(e.getMessage());
10+
}
11+
}
12+
13+
private static Thread asynFunction(String funcitonName, int duration) {
14+
return new Thread(() -> {
15+
16+
long startTime = System.currentTimeMillis();
17+
System.out.println(
18+
funcitonName + " started. Time: " + startTime +
19+
" milliseconds. Should take " + duration * 1000 + " milliseconds.");
20+
try {
21+
Thread.sleep((long) duration * 1000);
22+
} catch (InterruptedException e) {
23+
System.out.println(e.getMessage());
24+
}
25+
26+
long endTime = System.currentTimeMillis();
27+
System.out.println(funcitonName + " has done. Time: " + (endTime - startTime) + " milliseconds");
28+
29+
});
30+
}
31+
32+
private static void exercise() {
33+
System.out.println("EXERCISE:");
34+
Thread thread = asynFunction("my function1", 20);
35+
thread.start();
36+
try {
37+
38+
thread.join();
39+
} catch (InterruptedException e) {
40+
e.printStackTrace();
41+
}
42+
43+
}
44+
45+
private static void extra() {
46+
47+
System.out.println();
48+
System.out.println("EXTRA:");
49+
50+
Thread threadA = asynFunction("Function A", 1);
51+
Thread threadB = asynFunction("Function B", 2);
52+
Thread threadC = asynFunction("Function C", 3);
53+
Thread threadD = asynFunction("Function D", 1);
54+
55+
try {
56+
threadA.start();
57+
threadB.start();
58+
threadC.start();
59+
60+
threadA.join(5000);
61+
threadB.join(5000);
62+
threadC.join(5000);
63+
64+
System.out.println("Starting Function D...");
65+
threadD.start();
66+
threadD.join();
67+
68+
} catch (InterruptedException e) {
69+
System.out.println(e.getMessage());
70+
}
71+
72+
}
73+
}

0 commit comments

Comments
 (0)