Repository navigation
Expand file tree
/
Copy pathIndex.ets
More file actions
76 lines (64 loc) · 2.24 KB
/
Copy pathIndex.ets
File metadata and controls
76 lines (64 loc) · 2.24 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
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 AlternatePrintingExample {
public synArray: any = getSyc();
public sharedType: string = "object";
public static main(args: string[]): void {
const printer: NumberPrinter = new NumberPrinter();
const oddThread: Thread = new Thread(new OddNumberTask(printer));
getValues(oddThread.start());
const evenThread: Thread = new Thread(new EvenNumberTask(printer));
getValues(evenThread.start());
}
}
class NumberPrinter {
private number: any = getClass('number', 1);
private readonly MAX: any = getClass('number', 10);
public synArray: any = getSyc();
public sharedType: string = "object";
public printOdd(): void {
while (getValues(this.number) <= getValues(this.MAX)) {
SynStart(getValues(this['synArray']));
while (getValues(this.number) % 2 === 0) {
getValues(wait(getValues(this['synArray'])));
}
console.log(Thread.currentThread().getName() + " - " + getValues(this.number));
setValues(this.number, getValues(this.number) + 1);
getValues(notify(getValues(this['synArray'])));
SynEnd(getValues(this['synArray']));
}
}
public printEven(): void {
while (getValues(this.number) <= getValues(this.MAX)) {
SynStart(getValues(this['synArray']));
while (getValues(this.number) % 2 !== 0) {
getValues(wait(getValues(this['synArray'])));
}
console.log(Thread.currentThread().getName() + " - " + getValues(this.number));
setValues(this.number, getValues(this.number) + 1);
getValues(notify(getValues(this['synArray'])));
SynEnd(getValues(this['synArray']));
}
}
}
class OddNumberTask implements Runnable {
private printer: any;
constructor(printer: NumberPrinter) {
setValues(this.printer, printer);
}
run(): void {
getValues(this.printer.printOdd());
}
}
class EvenNumberTask implements Runnable {
private printer: any;
constructor(printer: NumberPrinter) {
setValues(this.printer, printer);
}
run(): void {
getValues(this.printer.printEven());
}
}