dathoang.se
03/15/2017, 1:31 PMdathoang.se
03/15/2017, 1:33 PMdathoang.se
03/15/2017, 1:34 PMmiha-x64
03/15/2017, 2:41 PMObjectRef
(IntRef
, ByteRef
, etc) to hold changing value. In IDE, when your mouse pointer is over such variable, you can see a tooltip explaining this.gbm
03/15/2017, 3:16 PMgbm
03/15/2017, 3:16 PMfun main(args: Array<String>) {
val pool = Executors.newFixedThreadPool(4)
val length: Int = 10000
var startIndex: Int = 0
var endIndex: Int = length
for (i in 1..4) {
pool.execute { task(startIndex, endIndex) }
startIndex = endIndex
endIndex = if (i < 4 - 1) endIndex + length else 40000
}
pool.shutdown()
}
inline fun task(start: Int, end: Int) {
println("Start: $start; End: $end")
}
and it almost works fine but it is possible to get this output:
Start: 10000; End: 20000
Start: 10000; End: 20000
Start: 20000; End: 30000
Start: 30000; End: 40000
gbm
03/15/2017, 3:18 PMuli
03/15/2017, 3:23 PMgbm
03/15/2017, 3:25 PMuli
03/15/2017, 3:28 PMgbm
03/15/2017, 3:28 PMgbm
03/15/2017, 3:29 PMuli
03/15/2017, 3:29 PMuli
03/15/2017, 3:30 PMgbm
03/15/2017, 3:32 PMgbm
03/15/2017, 3:33 PMObjectRef
(IntRef
, ByteRef
, etc) to hold changing value. In IDE, when your mouse pointer is over such variable, you can see a tooltip explaining this."robin
03/15/2017, 3:33 PMgbm
03/15/2017, 3:33 PMrobin
03/15/2017, 3:34 PMgbm
03/15/2017, 3:34 PMrobin
03/15/2017, 3:37 PMuli
03/15/2017, 3:39 PMuli
03/15/2017, 3:39 PMrobin
03/15/2017, 3:40 PMvar x = 0
repeat(10) { x += 1 }
After this code block, x
will be 10
.
If the code behaved like you seem to be expecting, x
would still be 0
after the block, which negates all the benefits you get by having real closures.gbm
03/15/2017, 3:41 PMusernamewastaken
03/15/2017, 8:12 PMvoddan
03/15/2017, 8:16 PMvoddan
03/15/2017, 8:17 PMvoddan
03/15/2017, 8:18 PMclass is in Kotlin and has now "hidden" those fieldsWhat fields did you mean?
reinhard_codes
03/15/2017, 8:21 PM