This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[LOG]: "Total tasks: 10" | |
[LOG]: "worker 1 is started" | |
[LOG]: "worker 1 handles task" | |
[LOG]: " --> Task 1 started" | |
[LOG]: "worker 2 is started" | |
[LOG]: "worker 2 handles task" | |
[LOG]: " --> Task 2 started" | |
[LOG]: "worker 3 is started" | |
[LOG]: "worker 3 handles task" | |
[LOG]: " --> Task 3 started" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
await Promise.all([worker(1), worker(2), worker(3), worker(4)]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let worker = async (index: number) => { | |
console.log(`worker ${index} is started`); | |
while(tasksQueue.length > 0) { | |
console.log(`worker ${index} handles task`); | |
let task = tasksQueue[0]; // should be atomic | |
tasksQueue = tasksQueue.slice(1); | |
await task(); | |
console.log(`worker ${index} finished handling task`); | |
} | |
console.log(`worker ${index} is retired`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let idx =1, tasksQueue = Array(numberOfTasks).fill(1) | |
.map(v => Math.round(Math.random() * 30) + v) | |
.map(v => (() => taskExecutor({delay: v, index: `${idx++}` }))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[LOG]: "* start running batch 0" | |
[LOG]: " --> Task 0 started" | |
[LOG]: " --> Task 1 started" | |
[LOG]: " --> Task 2 started" | |
[LOG]: " <-- Task 0 finished" | |
[LOG]: " <-- Task 1 finished" | |
[LOG]: " <-- Task 2 finished" | |
[LOG]: "* finished running batch 0" | |
[LOG]: "* start running batch 3" | |
[LOG]: " --> Task 3 started" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let taskExecutor = async (task: {delay: number, index: string}) => { | |
console.log(` --> Task ${task.index} started`); | |
await sleep(task.delay); | |
console.log(` <-- Task ${task.index} finished`); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function solution1() { | |
for(let i = 0; i < tasksSleepInput.length; i+= chunkSize) { | |
let batch = tasksSleepInput.slice(i, i + chunkSize); | |
console.log(`* start running batch ${i}`); | |
await Promise.all(batch.map(taskExecutor)); | |
console.log(`* finished running batch ${i}`); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const chunkSize = 3; | |
const numberOfTasks = 10; | |
let tasksSleepInput = Array(numberOfTasks).fill(30).map(v => ({delay: v, index: '' })); | |
// add indices | |
Object.keys(tasksSleepInput).forEach(k => tasksSleepInput[Number.parseInt(k)].index = k); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const chunkSize = 3; | |
const numberOfTasks = 10; | |
let tasksSleepInput = Array(numberOfTasks).fill(30).map(v => ({delay: v, index: '' })); | |
// add indices | |
Object.keys(tasksSleepInput).forEach(k => tasksSleepInput[Number.parseInt(k)].index = k); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<application> | |
.... | |
<meta-data | |
android:name="com.google.android.geo.API_KEY" | |
android:value="@string/google_maps_key" /> | |
</application> | |
<uses-permission android:name="android.permission.INTERNET" /> |
NewerOlder