forked from Java2ArkTS/Java2ArkTS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndex.ets
More file actions
136 lines (115 loc) · 3.06 KB
/
Copy pathIndex.ets
File metadata and controls
136 lines (115 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
class CompletableFutureExample {
private task1(): string {
console.log("T1: 1");
for (let i = 0; i < 1000; i++);
console.log("T1: 2");
for (let i = 0; i < 1000; i++);
return null;
}
private task2(): string {
console.log("T2: 1");
for (let i = 0; i < 1000; i++);
console.log("T2: 2");
for (let i = 0; i < 1000; i++);
console.log("T2: 3");
for (let i = 0; i < 1000; i++);
return " ???? ";
}
private task3(tea: string): string {
console.log("T1: 1" + tea);
console.log("T1: 2");
return " ???:" + tea;
}
public static main(): void {
let example = new CompletableFutureExample();
let t1 = new Thread(() => {
example.task1();
});
let tea = [null];
let t2 = new Thread(() => {
tea[0] = example.task2();
});
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (e) {
console.error(e);
}
let result = example.task3(tea[0]);
}
}
class SerialRelation {
private static task1(): string {
return "Hello World";
}
private static task2(s: string): string {
return s + " QQ";
}
private static task3(s: string): string {
return s.toUpperCase();
}
public static main(): void {
let result = this.task1();
result = this.task2(result);
result = this.task3(result);
}
}
class ConvergeRelation {
private static task1(): string {
let t = this.getRandom(5, 10);
for (let i = 0; i < 1000; i++);
return String(t);
}
private static task2(): string {
let t = this.getRandom(5, 10);
for (let i = 0; i < 1000; i++);
return String(t);
}
private static getRandom(i: number, j: number): number {
return Math.floor(Math.random() * (j - i)) + i;
}
public static main(): void {
let result1 = this.task1();
let result2 = this.task2();
let result = result1 != null ? result1 : result2;
}
}
class ExceptionHandler {
private static task(): number {
try {
return 7 / 0;
} catch (e) {
if (e instanceof ArithmeticException) {
return 0;
}
throw e;
}
}
private static task2(r: number): number {
return r * 10;
}
public static main(): void {
let result = this.task();
result = this.task2(result);
}
}
class Thread {
private runnable: () => void;
constructor(runnable: () => void) {
this.runnable = runnable;
}
start(): void {
setTimeout(this.runnable, 0);
}
join(): void {
// TypeScript/JavaScript does not provide a direct join() equivalent
// Use promise or other synchronizing techniques in real implementation
}
}
// Simulate static main method calls
CompletableFutureExample.main();
SerialRelation.main();
ConvergeRelation.main();
ExceptionHandler.main();