Skip to content

Commit 6f5c885

Browse files
committed
feat: add scheduler in js
1 parent 297ae18 commit 6f5c885

File tree

3 files changed

+98
-3
lines changed

3 files changed

+98
-3
lines changed

js/scheduler.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* @Author: Chacha
3+
* @Date: 2022-03-26 16:18:26
4+
* @Last Modified by: Chacha
5+
* @Last Modified time: 2022-03-26 16:25:18
6+
*/
7+
8+
/**
9+
* JS实现一个带并发限制的异步调度器Scheduler,保证同时运行的任务最多有两个。完善下面代码的Scheduler类,使以下程序能够正常输出:
10+
* class Scheduler {
11+
* add(promiseCreator) { ... }
12+
* // ...
13+
* }
14+
*
15+
* const timeout = time => {
16+
* return new Promise(resolve => {
17+
* setTimeout(resolve, time)
18+
* }
19+
* })
20+
*
21+
* const scheduler = new Scheduler()
22+
*
23+
* const addTask = (time,order) => {
24+
* scheduler.add(() => timeout(time).then(()=>console.log(order)))
25+
* }
26+
*
27+
* addTask(1000, '1')
28+
* addTask(500, '2')
29+
* addTask(300, '3')
30+
* addTask(400, '4')
31+
* // output: 2 3 1 4
32+
*
33+
* 整个的完整执行流程:
34+
* 起始1、2两个任务开始执行
35+
* 500ms时,2任务执行完毕,输出2,任务3开始执行
36+
* 800ms时,3任务执行完毕,输出3,任务4开始执行
37+
* 1000ms时,1任务执行完毕,输出1,此时只剩下4任务在执行
38+
* 1200ms时,4任务执行完毕,输出4
39+
*
40+
*/
41+
42+
class Scheduler {
43+
constructor() {
44+
this.queue = []; // 任务列表
45+
this.capacity = 2; // 任务队列容量
46+
this.runCount = 0; // 运行任务计数器
47+
}
48+
49+
add(promiseCreator) {
50+
// 小于等于2,直接执行
51+
this.queue.push(promiseCreator);
52+
this.runQueue();
53+
}
54+
55+
runQueue() {
56+
// 队列中还有任务才会被执行
57+
if (this.queue.length && this.runCount < this.capacity) {
58+
// 执行先加入队列的函数
59+
const promiseCreator = this.queue.shift();
60+
// 开始执行任务 计数+1
61+
this.runCount += 1;
62+
63+
promiseCreator().then(() => {
64+
// 任务执行完毕,计数-1
65+
this.runCount -= 1;
66+
this.runQueue();
67+
});
68+
}
69+
}
70+
}
71+
72+
const timeout = (time) => {
73+
return new Promise((resolve) => {
74+
setTimeout(resolve, time);
75+
});
76+
};
77+
78+
const scheduler = new Scheduler();
79+
80+
const addTask = (time, order) => {
81+
scheduler.add(() => timeout(time).then(() => console.log(order)));
82+
};
83+
84+
addTask(1000, "1");
85+
addTask(500, "2");
86+
addTask(300, "3");
87+
addTask(400, "4");
88+
89+
// 控制台输出 2 3 1 4

leetcode/string/implement_strstr

0 Bytes
Binary file not shown.

leetcode/string/implement_strstr.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: Chacha
33
* @Date: 2022-03-20 14:32:32
44
* @Last Modified by: Chacha
5-
* @Last Modified time: 2022-03-26 01:16:22
5+
* @Last Modified time: 2022-03-26 15:51:40
66
*/
77

88
/**
@@ -239,7 +239,7 @@ int Solution::strStr(string haystack, string needle)
239239
}
240240
}
241241

242-
return 0;
242+
return -1;
243243
}
244244

245245
void Solution::getNext(int *next, const string &s)
@@ -268,9 +268,15 @@ int main(int argc, char const *argv[])
268268
{
269269
Solution s;
270270
string haystack = "aabaabaafa";
271+
string haystack1 = "hello";
272+
string haystack2 = "aaaaa";
271273
string needle = "aabaaf";
274+
string needle1 = "ll";
275+
string needle2 = "bba";
272276

273-
cout << s.strStr(haystack, needle) << endl; // 输出 3
277+
cout << s.strStr(haystack, needle) << endl; // 输出 3
278+
cout << s.strStr(haystack1, needle1) << endl; // 输出 2
279+
cout << s.strStr(haystack2, needle2) << endl; // 输出 -1
274280

275281
return 0;
276282
}

0 commit comments

Comments
 (0)