The Artima Developer Community
Sponsored Link

Make Room for JavaSpaces, Part VI
Build and Use Distributed Data Structures in Your JavaSpaces Programs
by Susan Hupfer
First Published in JavaWorld, October 2000

<<  Page 2 of 15  >>


Build Distributed Data Structures with Entries

Let's say you want to store an array of values in a space, and that you'd like the array to be accessible and modifiable by multiple processes simultaneously. For example, suppose your array holds today's highest temperatures for all 50 US states, and that weather stations in the various states will need to read and alter the values throughout the day.

The first approach you might think up is to store the array in an entry, like this:

public class HighTemps implements Entry {
    public Integer[] values;

    public HighTemps() {

If a weather station wants to read or alter the high temperature for its state, it must read or remove the entire array. With this scheme, you haven't yet created a truly distributed array, since multiple processes cannot access or modify the array values simultaneously. The entry itself can become a bottleneck if many processes are trying to access the array concurrently.

You can try a different approach and decompose the temperature array into a collection of entries, each of which represents a single array element. Here's how you would represent an array element:

public class HighTemp implements Entry {
    public Integer index;
    public Integer value;
    public HighTemp () {

    public HighTemp(int index, int value) {
        this.index = new Integer(index);
        this.value = new Integer(value);

Each HighTemp entry has an index (the element's position in the array) and a value (the element's temperature value). Here's how to initialize a 10-element array (leaving out the try/catch clause for simplicity):

for (int i = 1; i <= 10 ; i++) {
    HighTemp temp = new HighTemp(i, -99);
    space.write(temp, null, Lease.FOREVER);

This loop deposits 10 HighTemp entries into the space, initializing each with an index and a value of -99. Here's how you would read one of the temperature entries -- the fifth one, in this example:

// create a template to retrieve the element
HighTemp template = new HighTemp();
template.index = new Integer(5);

// read it from the space
HighTemp temp = (HighTemp), null, Long.MAX_VALUE);

To modify that temperature, you take the temperature entry from the space (using the same template), update its value, and then write the entry back to the space as follows:

// remove the element
HighTemp temp = (HighTemp)space.take(template, null, Long.MAX_VALUE);

// alter the value
temp.value = new Integer(105);
// return the element to the space
space.write(temp, null, Lease.FOREVER);

This example demonstrates the basics of building and using a very simple distributed data structure. Since this distributed array is represented by multiple entries in the space, multiple processes can access and modify it concurrently -- for example, one process can modify element 0 without interfering with another process modifying element 48.

Along with the distributed data structure's representation, you've seen the protocol that processes follow to deal with the array. To read an array value, a process reads that value's entry in the space. To modify an array value, a process first takes the entry from the space, updates its value field, and then writes the entry back to the space. You could augment this ultra-simple protocol in various ways: for example, you could also store the size of the array, copy a value from one element to another, or delete an element.

While the distributed array data structure has a counterpart in the sequential programming world, you'll find that some distributed data structures are completely new. In the compute server example discussed in Part 2 and Part 4 of this series, you already encountered a distributed data structure that has no sequential analog: bags (which were used for holding task and result entries). Let's take a closer look.

<<  Page 2 of 15  >>

Sponsored Links

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