5

In trying to compare the "execution characteristics" of the same simple problem implemented in both Java and Scala, I found that the thread classes in the Java version run on threads with names like Thread-x while the Scala actors run on threads with names like ForkJoinPool-x-worker-y. Non-threaded Java classes and non-actor Scala classes consistently run on the main thread.

The log fragment below will illustrate:

Java version: Scoord thread class running in Thread[Thread-0,5,main]
              Semaphore class running in Thread[main,5,main]

Scala version: Scoord actor running in Thread[ForkJoinPool-1-worker-13,5,main]
               Semaphore class running in Thread[main,5,main]

What is the difference between these threads apart from being instances of different classes - the Thread class vs the ForkJoinWorkerThread class? How are the worker threads mapped to O/S kernel threads for execution?

Any explanation on the naming convention used for JVM threads would also be greatly appreciated.

Both the implementations were run on the same version of the JVM - HotSpot (TM) 64-bit Server VM (build 23.21-b01, mixed mode).The hardware was a 64-bit, 4-core Acer laptop with 8G of memory, running Windows 8.

user987339
  • 10,519
  • 8
  • 40
  • 45

2 Answers2

4

Scala actors are not threads! They are objects, that handles events asynchronously - they may run in different threads.

Scala actor is a concept similar to Communicating Sequential Processes which is different from threads. Nevertheless threads are usually used in CSP implementations, see Go language.

Grzegorz Żur
  • 47,257
  • 14
  • 109
  • 105
  • But doesn't it (a CSP implementation) ultimately get executed via a Thread ? Even "Goroutines" need threads ? – Rawle Ramkeesoon Oct 17 '13 at 10:16
  • @RawleRamkeesoon, Yes, I stated that in the answer. Threads can be seen as a low level mechanism for implementing CSP-like solutions. – Grzegorz Żur Oct 17 '13 at 10:19
  • 1
    Understood. Threads are the "low level mechanism" for implementing ANY solution. What I am after in this question is how the Scala actors (CSPs or whatever they may be) actually get mapped to O/S Threads that get executed. – Rawle Ramkeesoon Oct 17 '13 at 10:38
  • 1
    @RawleRamkeesoon, how actors are mapped to OS threads depends on [dispatcher](http://doc.akka.io/docs/akka/2.2.1/scala/dispatchers.html) you use for these specific actors. Default dispatcher maps all its actors to single thread pool, and default thread pool is special fork-join thread pool in Akka library. So you can look into implementation of this thread pool to find out exact strategy for mapping tasks to threads. IIUC, this is the link: https://github.com/akka/akka/blob/master/akka-actor/src/main/scala/akka/dispatch/AbstractDispatcher.scala#L364 – Vladimir Matveev Oct 17 '13 at 19:25
  • Is that Dispatcher part of the Scala Actor runtime system or the JVM ? Am only interested in Scala actors at present even though I know the Akka actors library may replace it in 2.x. – Rawle Ramkeesoon Oct 17 '13 at 20:15
3

There is no scala threads -- scala uses the very same java threads. So mapping to OS threads have the same rules. Actors are built on top of the same concurrency primitives, especially thread pools, so you're basically comparing thread pool versus bare threads. Default thread pool for actors is fork-join pool -- the main feature of it among other kinds of thread pools is work stealing.

enter image description here

Having said this, nothing stops you from using FJP from java (out-of-the-box on java 7, or as pluggable library in java 6) and observing the same results and naming (but obviously with dreaded low level api).

Community
  • 1
  • 1
om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
  • As can be seen from the partial log, Scala actors RUN on FJP threads while pure Java Thread classes RUN on JVM (?) threads. – Rawle Ramkeesoon Oct 17 '13 at 11:11
  • @RawleRamkeesoon in particular, actors run on FJP threads, FJP threads are java threads aka JVM threads which are backed by OS threads. If you use FJP framework from java you will see the same log statements. – om-nom-nom Oct 17 '13 at 11:13
  • Sorry pressed the return button too early. What I'm really after is the difference between the two threads - the FJP threads and the Thread-x type threads - and how they get scheduled for execution by O/S kernel threads. – Rawle Ramkeesoon Oct 17 '13 at 11:16
  • @RawleRamkeesoon no difference at all, FJP threads are just a bunch of ordinary java[/os] threads united under the same umbrella. On unix systems preemptive scheduling will be applied. [Java threads are OS threads](http://stackoverflow.com/a/9934747/298389). – om-nom-nom Oct 17 '13 at 11:27
  • These answers are very helpful in clarifying my understanding of the JVM-O/S environment – Rawle Ramkeesoon Oct 17 '13 at 11:36
  • @RawleRamkeesoon was that sarcasm? :-) By the way what **is not** mapped to OS is [some types of jvm locks](http://slava-technical.blogspot.ru/2011/04/how-does-jvm-handle-locks.html). – om-nom-nom Oct 17 '13 at 11:37
  • Absolutely no sarcasm intended. – Rawle Ramkeesoon Oct 17 '13 at 14:19