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
53 lines (45 loc) · 1.65 KB
/
Copy pathIndex.ets
File metadata and controls
53 lines (45 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
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 CounterIncrementer implements Runnable {
public synArray: any = getSyc();
public sharedType: string = "object";
private synArray: any = {};
run(): void {
for (let i = 0; i < 1000; i++) {
getValues(this.incrementCounter());
}
}
private incrementCounter(): void {
SynStart(this.synArray);
setValues(counter, getValues(counter) + 1);
SynEnd(this.synArray);
}
}
class SimpleThreadExample {
private static counter: any = getClass('number', 0);
public synArray: any = getSyc();
public sharedType: string = "object";
public static main(args: string[]): void {
const thread1 = new Thread(new SimpleThreadExample.CounterIncrementer());
const thread2 = new Thread(new SimpleThreadExample.CounterIncrementer());
thread1.start();
thread2.start();
console.log("Final counter value: " + getValues(SimpleThreadExample.counter));
}
static CounterIncrementer = class implements Runnable {
run(): void {
for (let i = 0; i < 1000; i++) {
getValues(this.incrementCounter());
}
}
private incrementCounter(): void {
const obj = getClass(SimpleThreadExample);
synchronized(obj, () => {
setValues(SimpleThreadExample.counter, getValues(SimpleThreadExample.counter) + 1);
});
}
}
}