-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chap02 ex1 better implement #31
base: master
Are you sure you want to change the base?
Conversation
Hi @amoszhou,
|
@ryblovAV yes you are right. |
@amoszhou |
t1.join() | ||
t2.join() | ||
|
||
(aVal, bVal) | ||
(a, b) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, thank you
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are welcome, just don't forget to add lazy val
s and push again to sync the pull request if you made any changes.
I think this is more better