chap02 ex1 better implement#31
chap02 ex1 better implement#31amoszhou wants to merge 1 commit intoconcurrent-programming-in-scala:masterfrom
Conversation
|
Hi @amoszhou,
|
|
@ryblovAV yes you are right. |
|
@amoszhou |
| t2.join() | ||
|
|
||
| (aVal, bVal) | ||
| (a, b) |
There was a problem hiding this comment.
@amoszhou Commeny from @ryblovAV is correct - the expression a is by name (not lazy), and it will be executed twice now. The first time in parallel in the thread statement, and the second time when returning from the method. This effectively removes parallelism, and if the a or b are side-effecting, will execute the side-effect twice.
If you want nicer code, you could do this:
lazy evaluatedA = a
lazy evaluatedB = b
val t1 = thread {
evaluatedA
}
...
(evaluatedA, evaluatedB)
There was a problem hiding this comment.
You are welcome, just don't forget to add lazy vals and push again to sync the pull request if you made any changes.
I think this is more better