Synchronizing access to resources in Java

D

dlugi

Hi!!

I would like to synchronize access to one resource with two different
applications working in two virtual machines. Is there some technology
which enable to block access to resources (e.g. file, network etc) -
something like global semaphore.

I consider to use JavaSpaces but this mechanism is very slow.
So, has anybody similar problem??

Thanks in advance.
 
D

dlugi

(e-mail address removed) opalinski from opalpaweb napisa³(a):
How about putting the resource behind RMI?

Perform synchronize in RMI call.

Opalinski
(e-mail address removed)
http://www.geocities.com/opalpaweb/

it's not good solution because RMI transfer byte code and methods of the
object are invoked locally so two clients can call the same method and
get access to resources in the same time
 
O

opalpa

it's not good solution because RMI transfer byte code and methods of the
object are invoked locally so two clients can call the same method and
get access to resources in the same time


The methods run in remote virtual machine. You can lock there. Where
the invocation happens and that parameters get serialized does not
prevent that.

Cheers,
Opalinski
(e-mail address removed)
http://www.geocities.com/opalpaweb/
 
D

dlugi

I found it on http://java.sun.com/docs/books/tutorial/rmi/overview.html

"Advantages of Dynamic Code Loading
One of the central and unique features of RMI is its ability to download
the bytecodes (or simply code) of an object's class if the class is not
defined in the receiver's virtual machine. The types and the behavior of
an object, previously available only in a single virtual machine, can be
transmitted to another, possibly remote, virtual machine. RMI passes
objects by their true type, so the behavior of those objects is not
changed when they are sent to another virtual machine. This allows new
types to be introduced into a remote virtual machine, thus extending the
behavior of an application dynamically. The compute engine example in
this chapter uses RMI's capability to introduce new behavior to a
distributed program."
 
O

opalpa

"Advantages of Dynamic Code Loading
One of the central and unique features of RMI is its ability to download
the bytecodes (or simply code) of an object's class if the class is not
defined in the receiver's virtual machine. The types and the behavior of
an object, previously available only in a single virtual machine, can be
transmitted to another, possibly remote, virtual machine. RMI passes
objects by their true type, so the behavior of those objects is not
changed when they are sent to another virtual machine. This allows new
types to be introduced into a remote virtual machine, thus extending the
behavior of an application dynamically. The compute engine example in
this chapter uses RMI's capability to introduce new behavior to a
distributed program."


These aspects do not prevent using RMI to lock a resource. The
paragraph you quote, in short, says: "If you have two virtual machines
and one has class file A and the other one doesn't the one that does
can share the class file with the one that doesn't". The paragraph
does not talk about instances of those classes. The paragraph is
orthogonal to topic.

Good luck,
Opalinski
(e-mail address removed)
http://www.geocities.com/opalpaweb/
 
D

dlugi

(e-mail address removed) opalinski from opalpaweb napisa³(a):
These aspects do not prevent using RMI to lock a resource. The
paragraph you quote, in short, says: "If you have two virtual machines
and one has class file A and the other one doesn't the one that does
can share the class file with the one that doesn't". The paragraph
does not talk about instances of those classes. The paragraph is
orthogonal to topic.

Good luck,
Opalinski
(e-mail address removed)
http://www.geocities.com/opalpaweb/

Ok, you are right, I read this paragraph through. Thank you for your
advices.
RMI involves a massive overhead, but I need this mechanism to lock
records in my implementation of database management system, so I search
a little bit faster solution than RMI. I look for eguivalent of system
semaphore in operating systems .
 
D

dlugi

Another doubt is does RMI guarantee that the method of remote object can
be invoked only by one client??

Thnak you in advance
 
M

Matt Humphrey

dlugi said:
Hi!!

I would like to synchronize access to one resource with two different
applications working in two virtual machines. Is there some technology
which enable to block access to resources (e.g. file, network etc) -
something like global semaphore.

I consider to use JavaSpaces but this mechanism is very slow.
So, has anybody similar problem??

Before you begin I think you first have to say what the resource is and how
you plan to access it from two different JVM. If the resource is a file
(something outside the JVM) and you just need a global lock you can use a
TCP port. The code for accessing the file first tries to allocate a server
port for a high number. If the access succeeds, you get to use the file.
When you're done, release the port. If the port is already allocated, wait
a little while and try again.

If the resource is some Java thing, you're better off with RMI, which does
not have to have a big overhead. There are techniques for exporting objects
with just a few lines of code (I've posted some here before.)

Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
N

Nigel Wade

dlugi said:
Another doubt is does RMI guarantee that the method of remote object can
be invoked only by one client??

RMI doesn't, no (the opposite, in fact).

But your implementation of the remote interface can impose whatever restrictions
and limitations on the clients it wishes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top