Skip to content

Commit 3567565

Browse files
committed
Fix infinite loop bug
Closes: #2
1 parent 6110358 commit 3567565

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

JavaScript/9-exclusive.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ class Pool {
1717
item = this.items[this.current];
1818
free = this.free[this.current];
1919
this.current++;
20+
if (this.current === this.size) this.current = 0;
2021
} while (!item || !free);
21-
if (this.current === this.size) this.current = 0;
2222
return item;
2323
}
2424

JavaScript/a-await.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class Pool {
2323
item = this.items[this.current];
2424
free = this.free[this.current];
2525
this.current++;
26+
if (this.current === this.size) this.current = 0;
2627
} while (!item || !free);
27-
if (this.current === this.size) this.current = 0;
2828
return item;
2929
}
3030

@@ -62,10 +62,12 @@ class Pool {
6262

6363
(async () => {
6464
const pool = new Pool();
65-
pool.add({ item: 1 });
66-
pool.add({ item: 2 });
67-
const last = { item: 3 };
68-
pool.add(last);
65+
const item1 = { item: 1 }
66+
pool.add(item1);
67+
const item2 = { item: 2 }
68+
pool.add(item2);
69+
const item3 = { item: 3 };
70+
pool.add(item3);
6971

7072
const x1 = await pool.capture();
7173
console.log({ x1 });
@@ -84,7 +86,7 @@ class Pool {
8486
console.log({ x6 });
8587
});
8688

87-
pool.release(x2);
88-
pool.release(x1);
89-
pool.release(x3);
89+
pool.release(item2);
90+
pool.release(item1);
91+
pool.release(item3);
9092
})();

JavaScript/b-timeout.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class Pool {
3030
item = this.items[this.current];
3131
free = this.free[this.current];
3232
this.current++;
33+
if (this.current === this.size) this.current = 0;
3334
} while (!item || !free);
34-
if (this.current === this.size) this.current = 0;
3535
return item;
3636
}
3737

@@ -98,5 +98,5 @@ class Pool {
9898
pool.release(x1);
9999
setTimeout(() => {
100100
pool.release(x3);
101-
}, 3000);
101+
}, 6000);
102102
})();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy