Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java vs Scala Threads - under the covers in the JVM

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.

like image 551
Rawle Ramkeesoon Avatar asked Oct 17 '13 09:10

Rawle Ramkeesoon


1 Answers

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.

like image 156
Grzegorz Żur Avatar answered Sep 25 '22 01:09

Grzegorz Żur