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
66 lines (58 loc) · 2.07 KB
/
Copy pathIndex.ets
File metadata and controls
66 lines (58 loc) · 2.07 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
import { setValues, getSyc, getClass, getValues, SynStart, SynEnd, addFunc, Runnable, Thread, wait, notify } from './ThreadBridge';
export function sharedWash(threadId: number){
let archetype = Thread.runnableList[threadId];
archetype.run();
}
class ParallelComputationExample {
public synArray: any = getSyc();
public sharedType: string = "object";
static main(args: string[]): void {
let array: number[] = new Array(100);
for (let i = 0; i < array.length; i++) {
setValues(array[i], i + 1);
}
const numThreads = 4;
const chunkSize = Math.floor(array.length / numThreads);
let threads: Thread[] = new Array(numThreads);
let tasks: ST[] = new Array(numThreads);
for (let i = 0; i < numThreads; i++) {
const start = i * chunkSize;
const end = (i === numThreads - 1) ? array.length : start + chunkSize;
setValues(tasks[i], new ST(array, start, end));
setValues(threads[i], new Thread(getValues(tasks[i])));
getValues(threads[i].start());
}
let totalSum = 0;
for (let i = 0; i < numThreads; i++) {
try {
getValues(threads[i].join());
setValues(totalSum, getValues(totalSum) + getValues(tasks[i].getSum()));
} catch (e) {
console.error(e);
}
}
console.log("Total sum of squares: " + getValues(totalSum));
}
}
class ST implements Runnable {
private readonly array: any = getClass('number[]', []);
private readonly start: any = getClass('number', 0);
private readonly end: any = getClass('number', 0);
private sum: any = getClass('number', 0);
public synArray: any = getSyc();
public sharedType: string = "object";
constructor(array: number[], start: number, end: number) {
setValues(this.array, array);
setValues(this.start, start);
setValues(this.end, end);
setValues(this.sum, 0);
}
run(): void {
for (let i = getValues(this.start); i < getValues(this.end); i++) {
setValues(this.sum, getValues(this.sum) + getValues(this.array[i]) * getValues(this.array[i]));
}
}
getSum(): number {
return getValues(this.sum);
}
}