Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- runBlocking {
- val firstRefs = (1..threadsCount).mapNotNull { articles.poll() }
- for (element in firstRefs) {
- if (element.first == HITLER) {
- return@runBlocking element.second
- }
- }
- val flags = mutableListOf<Boolean>()
- val coroutines = Array<Job?>(firstRefs.size) { null }
- for (i in firstRefs.indices) {
- coroutines[i] = async { processRefs(articles, firstRefs[i].first, firstRefs[i].second, flags) }
- }
- for (i in firstRefs.indices) {
- coroutines[i]?.join()
- }
- if (true in flags) {
- val index = flags.indexOfFirst { true }
- return@runBlocking firstRefs[index].second + 1
- }
- else {
- }
- }
- ___________________________________________________________________________________________________________________
- fun processRefs(articles: Queue<Pair<String, Int>>, ref: String, priority: Int, flags: MutableList<Boolean>) {
- val wikipediaPages = searchRefs(getHtmlDocument(ref))
- for (page in wikipediaPages) {
- if (page == HITLER) {
- flags.add(true)
- return
- }
- articles.add(Pair(page, priority + 1))
- }
- flags.add(false)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement