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
129 lines (111 loc) · 3.96 KB
/
Copy pathIndex.ets
File metadata and controls
129 lines (111 loc) · 3.96 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
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 CustomReadWriteLock {
private readers: any = getClass('number', 0);
private writing: any = getClass('boolean', false);
public synArray: any = getSyc();
public sharedType: string = "object";
public lockRead() {
SynStart(this['synArray']);
while (getValues(this.writing)) {
}
setValues(this.readers, getValues(this.readers) + 1);
SynEnd(this['synArray']);
}
public unlockRead() {
SynStart(this['synArray']);
setValues(this.readers, getValues(this.readers) - 1);
if (getValues(this.readers) === 0) {
}
SynEnd(this['synArray']);
}
public lockWrite() {
SynStart(this['synArray']);
while (getValues(this.readers) > 0 || getValues(this.writing)) {
}
setValues(this.writing, true);
SynEnd(this['synArray']);
}
public unlockWrite() {
SynStart(this['synArray']);
setValues(this.writing, false);
SynEnd(this['synArray']);
}
}
class SharedData {
private lock: any = getClass('CustomReadWriteLock', new CustomReadWriteLock());
private data: any = getClass('number', 0);
public synArray: any = getSyc();
public sharedType: string = "object";
public readData(readerName: string): void {
getValues(this.lock.lockRead());
console.log(readerName + " is reading data: " + getValues(this.data));
for (let j = 0; j < 1000; j++) ;
getValues(this.lock.unlockRead());
}
public writeData(writerName: string, newData: number): void {
getValues(this.lock.lockWrite());
console.log(writerName + " is writing data: " + getValues(newData));
setValues(this.data, newData);
for (let j = 0; j < 1000; j++) ;
getValues(this.lock.unlockWrite());
}
}
class ReaderWorker implements Runnable {
private readonly name: any = getClass('string', '');
private readonly sharedData: any = getClass('SharedData', new SharedData());
public synArray: any = getSyc();
public sharedType: string = "object";
constructor(name: string, sharedData: SharedData) {
setValues(this.name, name);
setValues(this.sharedData, sharedData);
}
run(): void {
for (let i = 0; i < 5; i++) {
getValues(this.sharedData.readData(getValues(this.name)));
try {
getValues(Thread.sleep(200));
} catch (e) {
console.error(e);
}
}
}
}
class WriterWorker implements Runnable {
public synArray: any = getSyc();
public sharedType: string = "object";
private readonly name: any = getClass('string', '');
private readonly sharedData: any = getClass('SharedData', new SharedData());
constructor(name: string, sharedData: SharedData) {
setValues(this.name, name);
setValues(this.sharedData, sharedData);
}
run(): void {
for (let i = 0; i < 5; i++) {
getValues(this.sharedData.writeData(getValues(this.name), i));
try {
Thread.sleep(200);
} catch (e) {
console.error(e);
}
}
}
}
class ReadWriteSimulation {
public synArray: any = getSyc();
public sharedType: string = "object";
static main(args: string[]) {
const sharedData = new SharedData();
const reader1 = new Thread(new ReaderWorker('Reader 1', sharedData));
const reader2 = new Thread(new ReaderWorker('Reader 2', sharedData));
const writer1 = new Thread(new WriterWorker('Writer 1', sharedData));
const writer2 = new Thread(new WriterWorker('Writer 2', sharedData));
reader1.start();
reader2.start();
writer1.start();
writer2.start();
}
}