Java Magazine, Sept/Oct 2016
ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2016 55 jvm languages new make ActorPool pool super pool override Obj receive Obj msg i msg as Int throw ArgErr Expecting Int msg return i i There are several things to note in the preceding code Actors are required to be const classes immutable The constructor takes an ActorPool which manages the threading for this actor The receive method is overridden to process incoming messages Notice that receive takes and returns an Obj type This syntax illustrates a nullable type which means the type system allows null to be used for these signatures Conversely the type Obj without a question mark is nonnullable and enforced by the type system to never be null The implementation of receive casts the object to an integer and returns the square Note the use of the as keyword which works as it does in C and the elvis operator which is syntax sugar for this code Int i msg instanceof Int Int msg null if i null throw new ArgErr Expecting Int msg Now lets look at how we can use this actor pool ActorPool actor SquareActor pool future actor send 3 val future get Lets digest the code above First I create an ActorPool instance which manages the thread pool Next I create an instance of SquareActor which I bind to the pool The third line sends a message to the actor What happens under the covers is that the actor queues the messages allocates a thread from the pool and processes the message on a background thread using the receive method Meanwhile the client code has blocked on the Future get method When the result becomes available then the val variable will be assigned the result of 9 The Fantom actor framework enforces that all messages are immutable using the const class type system This approach ensures that data mutations are restricted to a single actor thread and avoids the need for synchronization Actors provide an elegant robust alternative to the mainstream concurrency techniques used in languages such as Java domkit One of Fantoms most recent developments is a new HTML5 widget toolkit named domkit It provides a rich library to build highly polished HTML5 UIs with a design familiar to anyone who has experience with a traditional toolkit such as Swing Fantom provides two levels of abstraction for working in HTML5 The dom module provides a statically typed low level API to access and manipulate the DOM user input events CSS and XHR XML HTTP Request Under the covers it provides the JavaScript glue to the HTML5 platform The domkit module provides a much higher level of abstraction It is built entirely in Fantom on top of the dom module It provides DOM backed widgets using a familiar widget design including Menus Tables Trees Buttons and Dialogs domkit leverages CSS for styling and layout but allows you to work at a higher level of abstraction Lets look at an example for a table widget In Swing if you wanted to show information as a table you would create an instance of TableModel and render it using a JTable instance Browsers dont come with anything like JTable but domkit comes with a Table and TableModel API which work
You must have JavaScript enabled to view digital editions.