Scala vs java5/24/2023 If a thread holds on to the resource indefinitely (for example accidentally running an infinite loop) other threads waiting on the resource can not progress. A good example is a resource which can be used exclusively by one thread using mutual exclusion. We talk about blocking if the delay of one thread can indefinitely delay some of the other threads. Actors are asynchronous by nature: an actor can progress after a message send without waiting for the actual delivery to happen. In general, it is preferred to use asynchronous APIs, as they guarantee that the system is able to progress. A very CPU intensive task might give a similar behavior as blocking. On the other hand, an asynchronous call allows the caller to progress after a finite number of steps, and the completion of the method may be signalled via some additional mechanism (it might be a registered callback, a Future, or a message).Ī synchronous API may use blocking to implement synchrony, but this is not a necessity. ![]() SynchronousĪ method call is considered synchronous if the caller cannot make progress until the method returns a value or throws an exception. Parallelism on the other hand arise when the execution can be truly simultaneous. This can for example be realized with time slicing where parts of tasks are executed sequentially and mixed with parts of other tasks. Concurrency means that two or more tasks are making progress even though they might not be executing simultaneously. ParallelismĬoncurrency and parallelism are related concepts, but there are small differences. We seek to give working definitions that will be used in the scope of the Pekko documentation. Please note that, for many of these terms, there is no single agreed definition. If we were to call the object’s name, followed by parenthesis with any number of arguments, the corresponding apply(…) method in the object would be invoked.In this chapter we attempt to establish a common terminology to define a solid ground for communicating about concurrent, distributed systems which Pekko targets. While we can define any functions in a class’ companion object, the apply() and unapply() methods have special meaning. And their bodies typically come after an equals sign: def getAnimal(): Animal = Their return types are declared after the function name and a colon. Methods and functions (a function is like a method, but doesn’t belong to any class) are declared with the def keyword. We can, and almost always do, omit semicolons at the end of lines: var a = getAnimal() Methods and functions are declared with the ‘def’ keyword Semicolons are really only needed when multiple statements occur in the same line: var a = getAnimal() println(a) That’s one reason why the class comes last when declaring a variable it’s easy to simply omit it that way: var a = getAnimal() // `a` is still an instance of Animal Semicolons are not required to denote the end of a line So we rarely need to explicitly declare the type. Scala favors immutability, so most often, you’ll see val used. Var (short for variable) indicates that the variable is mutable: var a: Animal = getAnimal() // `a` can be subsequently reassigned Val (short for value) indicates that the variable is immutable (similar to final in Java): val a: Animal = getAnimal() // `a` cannot be subsequently reassigned Rather than beginning a variable declaration with the class name, as we do in Java, we begin them with either val or var in Scala. In Scala, the type comes afterwards: val a: Animal = getAnimal() Variables are declared as either immutable (val) or mutable (var) ![]() In Java, we declare types, then variable names, like so: Animal a = getAnimal() ![]() Type declarations come after variable names Instead, this article will help any Java developers who are interested in exploring Scala, but are simply having a hard time understanding the code examples they’re seeing. For-comprehensions, implicits, pattern-matching, and other features are better discussed in dedicated articles. Note that we won’t explore the advanced, powerful features of Scala here. At the end, we’ll walk through a comprehensive example of Scala code. So below is a guide to some of the common Scala syntax rules that differ from Java’s. Java developers who are curious about Scala might find the differences in syntax to be daunting.
0 Comments
Leave a Reply. |