-2

I just started learning a functional language (Scala) and one of the claims/recommendations made is, "you should try to use react instead of recieve" method while doing multithreading. To make it clear, react doesn't return any value but recieve does. They have their own reasons to support this recommendation. As Scala works on the JVM. It is making me curious to think if using Callable is a more costly affair than using Runnable in Java?

Does anyone has any experience with the same or comments on this?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Prateek
  • 523
  • 2
  • 13

2 Answers2

3

Runnable and Callback have the same "performance" as they are just Interfaces.

The two interfaces have slight API differences - a type compatible with the consuming API must be used; that is all.

This has nothing to do with Scala or react vs. recieve in Actors; the question boxes itself into the wrong corner.

Community
  • 1
  • 1
Paul
  • 247
  • 1
  • 4
  • Not sure what is the intent of this answer. The question is all about if Callable has some performance difference as compared to Runnable in java. I am not comparing Scala and Java or their api. – Prateek Jun 25 '13 at 07:21
  • 1
    @Prateek The intent was to answer the question, which it did. ***Runnable and Callback have the same "performance" as they are just Interfaces.*** - the use of quotes is to signify that interfaces *don't have* performance metrics as they are just signature contracts; code that *uses* or *consumes* the interfaces may have different performance characteristics. The relevant links explain 1) the different interface contracts (there is no mention of "performance") and; 2) react/receive (which is a different and unrelated topic). Rex took the time to spell it out for you. – Paul Jun 25 '13 at 16:45
  • No one expects that in the conference of writers someone will be arguing C comes after B. – Prateek Jun 26 '13 at 03:15
2

Wellll, you're really mixing different concepts here.

The reason to use react instead of receive is that each actor with a receive requires its own thread. So you've got one thread per actor. react on the other hand is handled by a pool of threads that will run that message on that actor and then go on to the next actor and message. (This really only permits you to be reactive--you can't wait for a certain amount of time.)

On the other hand, the Runnable and Callable interfaces are just ways to package up code in Java depending on whether you just want it to do stuff (Runnable) or return a value (Callable). The interfaces themselves don't have any difference in performance, but in order to get a Callable return value back to you there is additional stuff that needs to happen, so if you could write it either way you'd possibly be better off using something that only requires a Runnable. (In practice, this means starting a thread instead of a future, probably.) But the implementation details matter so much that you can't really make any general recommendations on the basis of the interface alone. You need to know how the interface is actually being used in the actual class you're calling.

Rex Kerr
  • 166,841
  • 26
  • 322
  • 407