Answer the question
In order to leave comments, you need to log in
Profit from multithreading on a simple example?
I don’t understand multithreading very well, I sketched the code on threads ( kotlin )
val start = System.currentTimeMillis()
var arrayStart = mutableListOf<String>()
var arrayResult = mutableListOf<String>()
var arrayHelper1 = mutableListOf<String>()
var arrayHelper2 = mutableListOf<String>()
repeat(3_000_000) {
arrayStart.add((it * 2).toString())
}
val thread1 = Thread(
Runnable {
for (i in 0 until arrayStart.size/2-3 step 3) {
val t = arrayStart.subList(i, i+3)
arrayHelper1.addAll(0, t.reversed())
}
}
)
val thread2 = Thread(
Runnable {
for (i in arrayStart.size/2..arrayStart.size-3 step 3) {
val t = arrayStart.subList(i, i+3)
arrayHelper2.addAll(0, t.reversed())
}
}
)
thread1.start()
thread2.start()
thread1.join()
thread2.join()
arrayResult.addAll(arrayHelper1)
arrayResult.addAll(arrayHelper2)
val time = (System.currentTimeMillis() - start) / 1000
println("time: $time")
val start = System.currentTimeMillis()
var arrayStart = mutableListOf<String>()
var arrayResult = mutableListOf<String>()
var arrayHelper1 = mutableListOf<String>()
var arrayHelper2 = mutableListOf<String>()
repeat(3_000_000) {
arrayStart.add((it * 2).toString())
}
for (i in 0 until arrayStart.size/2-3 step 3) {
val t = arrayStart.subList(i, i+3)
arrayHelper1.addAll(0, t.reversed())
}
for (i in arrayStart.size/2..arrayStart .size-3 step 3) {
val t = arrayStart.subList(i, i+3)
arrayHelper2.addAll(0, t.reversed())
}
arrayResult.addAll(arrayHelper1)
arrayResult.addAll(arrayHelper2)
val time = (System.currentTimeMillis() - start) / 1000
println("time: $time")
Answer the question
In order to leave comments, you need to log in
This is a very dangerous example. It is tied to the garbage collection phase. Warming up the JIT. And the cache size. Depending on the versions of java and OS. And no signor-tomato here will be able to say exactly what transients flow in the 2nd version of the code.
How to rewrite - I do not know. But multithreading is always useful when parsing many sites at the same time. Let the author make a web crawler.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question