Created
October 4, 2025 03:07
-
-
Save anandwana001/ef18747d7614fa21b63c8e6ee368e7da to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import kotlinx.coroutines.* | |
| import kotlin.system.measureTimeMillis | |
| data class ScrapedData(val url: String, val title: String, val contentLength: Int) | |
| class WebScraper { | |
| suspend fun scrapeWebsite(url: String): ScrapedData = withContext(Dispatchers.IO) { | |
| println("Scraping $url on ${Thread.currentThread().name}") | |
| delay(500) // Simulate network request | |
| ScrapedData( | |
| url = url, | |
| title = "Title from $url", | |
| contentLength = (1000..5000).random() | |
| ) | |
| } | |
| suspend fun scrapeMultipleSites(urls: List<String>): List<ScrapedData> = coroutineScope { | |
| urls.map { url -> | |
| async(Dispatchers.IO) { | |
| scrapeWebsite(url) | |
| } | |
| }.awaitAll() | |
| } | |
| } | |
| fun main() = runBlocking { | |
| val scraper = WebScraper() | |
| val urls = List(100) { "https://example.com/page$it" } | |
| val time = measureTimeMillis { | |
| val results = scraper.scrapeMultipleSites(urls) | |
| println("Scraped ${results.size} websites") | |
| println("Total content: ${results.sumOf { it.contentLength }} characters") | |
| } | |
| println("Time taken: $time ms") | |
| println("With 64 parallel IO operations, much faster than sequential!") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment