Creating a mutex

Q

Qaurk Noble

Hello all,

Does anyone know how I can create a mutex to control access to a shared
resource on one computer?

Regards,
Q. Noble
 
C

Chris Smith

Qaurk said:
Does anyone know how I can create a mutex to control access to a shared
resource on one computer?

In the Java VM? Each object has a monitor associated with it, which can
be used as a recursive mutex.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
S

Shripathi Kamath

Qaurk Noble said:
Hello all,

Does anyone know how I can create a mutex to control access to a shared
resource on one computer?

Regards,
Q. Noble


It depends on where you want to control access from:

1. All clients in a single JVM -- that is all clients of that shared
resource have their lifecycles within a single JVM

or

2. The clients are in different JVMs.
(or)
2b. The clients are different entities, at least one of which is not even a
JVM.


1. is the easiest because any object can be used as a mutex. If the shared
resource is represented by an Object, simply synchronizing around it will do
the trick.

2 and 2b. require that you use some OS specific synchronization resource.
For example, you can use a UDP or a TCP socket which is bound to a
particular port as a mutex. To gain control of the shared resource, a
client must gain control of the mutex first. In this case, this can be done
by opening a socket at a chosen port number. If the client suceeds in doing
so, it can assume access to the shared resource. When done, it closes the
socket. Other clients can then make use of the shared resource.

Of course, the above are simplistically stated, but I hope they give you
some ideas.

HTH,
 
J

Jim Hansson

Shripathi said:
It depends on where you want to control access from:

1. All clients in a single JVM -- that is all clients of that shared
resource have their lifecycles within a single JVM

or

2. The clients are in different JVMs.
(or)
2b. The clients are different entities, at least one of which is not even
a JVM.


1. is the easiest because any object can be used as a mutex. If the
shared resource is represented by an Object, simply synchronizing around
it will do the trick.

2 and 2b. require that you use some OS specific synchronization resource.
For example, you can use a UDP or a TCP socket which is bound to a
particular port as a mutex. To gain control of the shared resource, a
client must gain control of the mutex first. In this case, this can be
done by opening a socket at a chosen port number. If the client suceeds in
doing
so, it can assume access to the shared resource. When done, it closes the
socket. Other clients can then make use of the shared resource.

Of course, the above are simplistically stated, but I hope they give you
some ideas.

HTH,

for 2 and 2b you could setup the shared resource to be a remote object with
RMI, should that not solv it?

Jim
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top