![]() |
JavaPartyA distributed companion to JavaCurrent release 1.9.5 Bernhard Haumacher, Thomas Moschny and Michael Philippsen |
|
Contents See also
Powered by
|
Transparent Replicated ObjectsSince JavaParty 1.9.4 Local and remote objectsBefore introducing transparent replicated
objects, let’s first have a look at local and remote objects. Replicated objects
public replicated class P {
public P() {...} // constructor
int[] x; // instance variable
String l; // reference to a local object
R r; // reference to a remote object
P p; // another reference to a remote object
static R sr; // static variable
static P sp; // static variable
void foo() {...} // instance method
static void sfoo() { ... } // class method
}
Replicated objects are simply created by
using the constructor Constructing a replicated object creates exactly one replicate per virtual machine and returns a local reference to it. The behavior of replicated objects is semi local and semi remote: They are only locally accessible like local objects but passed as reference in a remote call like remote objects. That is, the reference to the replicate is replaced by the corresponding local reference during the remote call. Replicates on other nodes are only accessible through a remote object. SynchronizationRecall that access to a replicate happens in a direct and local manner: Both read and write operations access and modify respectively the state of the local replicate. Changes to the local replicate temporary destroy consistency, so the consistency of the replicates must be restored by synchronization. In pure Java, synchronization is always exclusive - an approach that’s not feasible for a remote environment: If only one access was allowed simultaneously, no parallelism would be possible and if all other replicates had to be locked before accessing the local replicate, the access would no longer be local. Existing synchronization primitives don’t support a protocol that allows for multiple readers in the presence of a single writer. JavaParty introduces replicated classes that allow for collective replication of local objects. Parallel, distributed read access may be used to synchronize multiple readers.
A shared synchronized(p) { ... }
replicated class P {
shared synchronized int getX() { ... }
}
Exclusive, local write access with a following state update of all replicates may be
used to synchronize a single writer. An exclusive synchronized(p) { ... }
exclusive synchronized void setX(int x) { ... }
Unlike Java synchronization, the synchronization of replicated objects must always happen at the replicated object that‘s currently read or modified. State updates take place before releasing an exclusive lock for the object that was used for synchronization. Please note that exclusive synchronization is quite expensive compared to shared synchronization, because exclusive synchronization has to use communication. Collective modificationReplicated objects also support parallel operations like multiple collective writers. In order to automatically merge the changes, parallel modifications of the state of a replicated object must not overlap. That is, each activity may only change a disjoint part of the overall state. After the modification, the concurrent modifications are merged to a new consistent state. Collective modifications are marked as synchronized blocks: collective synchronized(p) {
// your updates here
}
Collective synchronization is achieved by synchronizing all replicates in parallel. The collective synchronized block will not be entered until a thread has been synchronized with each local replicate. Remember that this also requires to assign a thread to each node, otherwise the system will end up in a deadlock. Before the collective synchronized block is left, all concurrent changes are merged. Everything that has been discussed so far is summarized in the following example: P p = new P(); // object creation
r.foo(p); // argument passing
p.foo(); // method call
shared synchronized(p) { // shared read access
System.out.println(p.x[0]);
}
exclusive synchronized(p) { // exclusive write access
p.l = "Hello World!";
}
// forall n {0..N}
collective synchronized(p) { // collective modification
p.x[n] = computeX(n);
}
At a particular time, a replicated object may be synchronized either shared / exclusive or collectively at all replicates. Be warned that mixing collective and shared / exclusive synchronization is likely to lead to a deadlock. SignalizingIn shared or exclusive synchronized blocks,
the |
|||||||
| For comments and bug reports please use the JavaParty users mailing list. Page design & maintenance: Bernhard Haumacher. Last update: Fri Mar 30 18:46:00 GMT+01:00 2007 Java is a trademark of Sun Microsystems. |