I'm learning the scala. I am very confused about the difference between actor and goroutine.
As known, the concurrency unit of golang is goroutine, and the concurrency model is very special and elegant in my opinion:
- CPU bound task
The runtime scheduler would preempt long-running coroutine at best effort, e.g. check preemption at function call, so that the long-running coroutine would not occupy the system thread too long
- event driven call
For example, on Linux, all types of events which could be managed by epoll, e.g. socket connect, would yield the goroutine and resume it when call finished later. When the goroutine yields, the holding thread would be released to handle next ready goroutine
- cgo calls
Such calls would exclusively occupy one system thread until the call returns.
How to handle all these types of calls in scala actor?
The scala reuses and depends on the JDK to access low-level syscalls, right? e.g. network socket. And some high-level libraries are also reused in scala, e.g. JDBC. The Java's concurrency model is threading (system threads), and all those calls in Java would block the thread until call returns. So how to encapsulate them in actor? Does actor perform different from goroutine?
Looking forward to any professional answers! Thanks!
Edit:
I search some documents, and I guess the answer maybe:
- CPU bound task
no preemption provided in scala
- event driven call
As Akka is integrated into scala, so either use Akka async wrapped libraries (e.g. Akka TCP) or use future?
- cgo calls
???
I change the title of this post. I already understand the style difference between actor and goroutine, and I just care about how to handle different types of calls in actor, just like gorotuine.
Edit:
Sorry, finally I find out what's exactly my question:
could we yield/resume actor just like goroutine?
Or actor just a scheduled computation piece which should run from start to end? i.e. the whole message processing could not be interrupted.
And because of the threading model in Java, I think it lacks of low-level support of blocking calls based on coroutine, right? e.g. socket read, if blocks, yield the coroutine, when new data read to read, resume the coroutine; while it's transparent to the programmer, all you need to write is socket.read(). As known, in Java, this whole procedure is based on system thread, the traditional way just like C.