Repository navigation
Expand file tree
/
Copy pathIndex.ets
More file actions
95 lines (85 loc) · 2.84 KB
/
Copy pathIndex.ets
File metadata and controls
95 lines (85 loc) · 2.84 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
import {
SynStart,
SynEnd,
wait,
notify,
SharedBoolean,
SharedString,
SharedNumber,
Syc,
isMainThread,
addFunc,
Runnable,
Thread,
} from './ThreadBridge';
export function sharedWash(runnable: Runnable) {
let archetype: Runnable;
if (runnable.className === 'TicketBookingTask') {
archetype = new TicketBookingTask(new TicketCounter(), 'DefaultPassenger', 1);
} else {
archetype = new Thread();
}
addFunc(runnable, archetype);
runnable.run();
}
class TicketCounter {
private ticketsAvailable = new SharedNumber(20);
public syn: Syc = new Syc();
public static staticSyn: Syc = new Syc();
public className: string = 'TicketCounter';
public bookTicket(passengerName: string, numberOfTickets: number): void {
SynStart(this.syn);
if (numberOfTickets <= this.ticketsAvailable.getValue()) {
this.ticketsAvailable.setValue(this.ticketsAvailable.getValue() - numberOfTickets);
console.log(
`${passengerName} booked ${numberOfTickets} ticket(s). Tickets left: ${this.ticketsAvailable.getValue()}`
);
} else {
console.log(
`Not enough tickets for ${passengerName} to book ${numberOfTickets} ticket(s). Tickets left: ${this.ticketsAvailable.getValue()}`
);
}
SynEnd(this.syn);
}
public getTicketsAvailable(): number {
SynStart(this.syn);
const availableTickets = this.ticketsAvailable.getValue();
SynEnd(this.syn);
return availableTickets;
}
}
class TicketBookingTask implements Runnable {
private ticketCounter: TicketCounter;
private passengerName = new SharedString();
private numberOfTickets = new SharedNumber();
public syn: Syc = new Syc();
public static staticSyn: Syc = new Syc();
public className: string = 'TicketBookingTask';
constructor(ticketCounter: TicketCounter, passengerName: string, numberOfTickets: number) {
this.ticketCounter = ticketCounter;
this.passengerName.setValue(passengerName);
this.numberOfTickets.setValue(numberOfTickets);
}
run(): void {
this.ticketCounter.bookTicket(this.passengerName.getValue(), this.numberOfTickets.getValue());
}
}
class TicketBookingTest2 {
public syn: Syc = new Syc();
public static staticSyn: Syc = new Syc();
public className: string = 'TicketBookingTest2';
public static main(args: string[]): void {
const ticketCounter = new TicketCounter();
const thread1 = new Thread(new TicketBookingTask(ticketCounter, 'Passenger1', 5));
const thread2 = new Thread(new TicketBookingTask(ticketCounter, 'Passenger2', 7));
const thread3 = new Thread(new TicketBookingTask(ticketCounter, 'Passenger3', 4));
const thread4 = new Thread(new TicketBookingTask(ticketCounter, 'Passenger4', 6));
thread1.start();
thread2.start();
thread3.start();
thread4.start();
}
}
if (isMainThread()) {
// You can put the entry of your code here to test.
}