-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThreadJavaScript.js
More file actions
41 lines (33 loc) · 1.24 KB
/
ThreadJavaScript.js
File metadata and controls
41 lines (33 loc) · 1.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
const Thread = require("./Thread");
class ThreadJavaScript {
main(argv) {
try {
const runnable = {
num: parseInt(argv[2]),
startAt: new Date().getTime(),
run: () => {
const Prime = require("./Prime");
const prime = new Prime(this.num);
const count = prime.count();
const times = new Date().getTime() - this.startAt;
console.log(
`JavaScript thread id #${threadId}: executes ${count} prime numbers in ${times} ms`);
}
};
const t0 = new Thread(runnable);
const t1 = new Thread(runnable);
const t2 = new Thread(runnable);
const t3 = new Thread(runnable);
t0.start();
t1.start();
t2.start();
t3.start();
t0.run.then(console.log).catch(console.error);
t1.run.then(console.log).catch(console.error);
process.stdout.write("JavaScript Threads - initializing complete\n");
} catch (e) {
process.stdout.write("Invalid argument\n")
}
}
}
new ThreadJavaScript().main(process.argv);