The Artima Developer Community
Sponsored Link

Make Room for JavaSpaces, Part III
Coordinate your Jini Apps with JavaSpaces
by Susan Hupfer
First Published in JavaWorld, March 2000

<<  Page 2 of 10  >>


Space Operations and Synchronization

Coordinating distributed processes can be hard work. In an application that runs on a single machine, the operating system, acting as a centralized manager, can synchronize multiple threads. But in a networked environment, a single point of control doesn't necessarily exist. Processes run on different machines and at their own pace. Unless you want to build a centralized controller to manage those processes, which would introduce an unwelcome bottleneck to the environment, you must build a distributed means of managing their interactions, which can be significantly trickier.

JavaSpaces can ease the synchronization of distributed processes because synchronization is built into the space operations themselves. Recall the basics of the JavaSpaces API: write deposits an object, called an entry, into a space; read makes a copy of an entry in a space (but leaves the object there); and take removes an entry from a space. Let's see how this simple API incorporates the basics of space-based synchronization. Suppose you define a simple Message entry like this:

public class Message implements Entry {
    public String content;

    public Message() {

Then you instantiate a Message entry and set its content:

Message msg = new Message();
msg.content = "Hello!";

Assuming you have access to a space object, you call its write method to place a copy of the entry into the space:

space.write(msg, null, Lease.FOREVER);

Now any process with access to the space can create a template and read the entry:

Message template = new Message();
Message result = (Message), null, Long.MAX_VALUE);

Any number of processes can read the entry in the space at a given time. But suppose a process wants to update the entry: the process must first remove the entry from the space, make the changes, and then write the modified entry back to the space:

Message result = (Message)space.take(template, null, Long.MAX_VALUE);
result.content = "Goodbye!";
space.write(result, null, Lease.FOREVER);

It's important to note that a process needs to obtain exclusive access to an entry that resides in the space before modifying it. If multiple processes are trying to update the same entry using this code, only one obtains the entry at a time. The others wait during the take operation until the entry has been written back to the space, and then one of the waiting processes takes the entry from the space while the others continue to wait.

The basics of space-based programming synchronization can be quickly summarized: Multiple processes can read an entry in a space at any time. But when a process wants to update an entry, it first has to remove it from the space and thereby gain sole access to it. In other words, the read, take, and write operations enforce coordinated access to entries. Entries and their operations give you everything you need to build more complex coordination schemes. With the basics under your belt, let's start building the game service.

<<  Page 2 of 10  >>

Sponsored Links

Copyright © 1996-2018 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use