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
58 lines (50 loc) · 1.82 KB
/
Copy pathIndex.ets
File metadata and controls
58 lines (50 loc) · 1.82 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
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 CoffeeShop {
private availableSeats: any = getClass('number', 5);
private customersServed: any = getClass('number', 0);
public synArray: any = getSyc();
public sharedType: string = "object";
public serveCustomer(): void {
{
SynStart(this['synArray']);
if (getValues(this.availableSeats) > 0) {
setValues(this.availableSeats, getValues(this.availableSeats) - 1);
setValues(this.customersServed, getValues(this.customersServed) + 1);
console.log(`Customer served. Seats available: ${getValues(this.availableSeats)}. Total customers served: ${getValues(this.customersServed)}`);
for (let i = 0; i < 100000; i++);
setValues(this.availableSeats, getValues(this.availableSeats) + 1);
} else {
console.log("No seats available. Customer has to wait.");
}
SynEnd(this['synArray']);
}
}
}
class Waiter implements Runnable {
public synArray: any = getSyc();
public sharedType: string = "object";
private coffeeShop: any = getClass('CoffeeShop', new CoffeeShop());
constructor(coffeeShop: CoffeeShop) {
setValues(this.coffeeShop, coffeeShop);
}
run(): void {
for (let i = 0; i < 10; i++) {
getValues(this.coffeeShop.serveCustomer());
}
}
}
class CoffeeShopExample {
public synArray: any = getSyc();
public sharedType: string = "object";
public static main(args: string[]): void {
const coffeeShop = new CoffeeShop();
const waiter1 = new Thread(new Waiter(coffeeShop));
const waiter2 = new Thread(new Waiter(coffeeShop));
waiter1.start();
waiter2.start();
}
}