Advertisement
Artyom_Kopan

Корутины №1

Jun 2nd, 2022
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.34 KB | None | 0 0
  1. runBlocking {
  2.             val firstRefs = (1..threadsCount).mapNotNull { articles.poll() }
  3.             for (element in firstRefs) {
  4.                 if (element.first == HITLER) {
  5.                     return@runBlocking element.second
  6.                 }
  7.             }
  8.             val flags = mutableListOf<Boolean>()
  9.             val coroutines = Array<Job?>(firstRefs.size) { null }
  10.             for (i in firstRefs.indices) {
  11.                 coroutines[i] = async { processRefs(articles, firstRefs[i].first, firstRefs[i].second, flags) }
  12.             }
  13.             for (i in firstRefs.indices) {
  14.                 coroutines[i]?.join()
  15.             }
  16.  
  17.             if (true in flags) {
  18.                 val index = flags.indexOfFirst { true }
  19.                 return@runBlocking firstRefs[index].second + 1
  20.             }
  21.             else {
  22.  
  23.             }
  24.         }
  25.  
  26. ___________________________________________________________________________________________________________________
  27.  
  28. fun processRefs(articles: Queue<Pair<String, Int>>, ref: String, priority: Int, flags: MutableList<Boolean>) {
  29.     val wikipediaPages = searchRefs(getHtmlDocument(ref))
  30.     for (page in wikipediaPages) {
  31.         if (page == HITLER) {
  32.             flags.add(true)
  33.             return
  34.         }
  35.         articles.add(Pair(page, priority + 1))
  36.     }
  37.     flags.add(false)
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement