Repository navigation
Expand file tree
/
Copy pathIndex.ets
More file actions
62 lines (50 loc) · 1.65 KB
/
Copy pathIndex.ets
File metadata and controls
62 lines (50 loc) · 1.65 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
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 Accumulator {
private sum: any = getClass('number', 0);
public synArray: any = getSyc();
public sharedType: string = "object";
public addToSum(value: number): void {
{
SynStart(this['synArray']);
setValues(this.sum, getValues(this.sum) + value);
console.log(" added " + value + ". Current sum: " + getValues(this.sum));
SynEnd(this['synArray']);
}
}
public getSum(): number {
{
SynStart(this['synArray']);
const result = getValues(this.sum);
SynEnd(this['synArray']);
return result;
}
}
}
class AdderTask implements Runnable {
public synArray: any = getSyc();
public sharedType: string = "object";
private accumulator: any = getClass('Accumulator', new Accumulator());
private valueToAdd: any = getClass('number', 0);
constructor(accumulator: Accumulator, valueToAdd: number) {
setValues(this.accumulator, accumulator);
setValues(this.valueToAdd, valueToAdd);
}
run(): void {
getValues(this.accumulator.addToSum(getValues(this.valueToAdd)));
}
}
class MultiThreadAccumulator {
public synArray : any = getSyc();
public sharedType: string = "object";
public static main(args: string[]): void {
const accumulator = new Accumulator();
const thread1 = new Thread(new AdderTask(accumulator, 5));
const thread2 = new Thread(new AdderTask(accumulator, 10));
thread1.start();
thread2.start();
}
}