Is There a Mutex in Java

Is there a Mutex in Java?

See this page: http://www.oracle.com/technetwork/articles/javase/index-140767.html

It has a slightly different pattern which is (I think) what you are looking for:

try {
mutex.acquire();
try {
// do something
} finally {
mutex.release();
}
} catch(InterruptedException ie) {
// ...
}

In this usage, you're only calling release() after a successful acquire()

What is mutex and semaphore in Java ? What is the main difference?

Semaphore can be counted, while mutex can only count to 1.

Suppose you have a thread running which accepts client connections. This thread can handle 10 clients simultaneously. Then each new client sets the semaphore until it reaches 10. When the Semaphore has 10 flags, then your thread won't accept new connections

Mutex are usually used for guarding stuff. Suppose your 10 clients can access multiple parts of the system. Then you can protect a part of the system with a mutex so when 1 client is connected to that sub-system, no one else should have access. You can use a Semaphore for this purpose too. A mutex is a "Mutual Exclusion Semaphore".

Programming a mutex in Java

You're creating a race condition and using a spinning lock. Spinning locks are not recommended in Java. Consider the following:

One thread begins executing while the other two wait. The other two threads wait. The other two threads now both set the mutexes at the same time and both begin executing, giving you an odd value of J because they're both altering it simultaneously.

To fix:

Implement the Java synchronized methodology. Synchronized is Java's internal method of handling thread-safety and control. No spinning locks!

Change your MyThread to the following:

public class MyThread extends Thread {
private static int j = 0;

public void run() {
synchronized(this) {
for (int i = 1; i <= 10; i++) {
j += i;
}
}
System.out.println(j);
}

Synchronized can surround any critical portions of code that could result in race conditions, simultaneous altering of data, etc. Note the lack of a specific lock. Note that synchronized takes its own object as a parameter, as you are synchronizing control over this object, but you can just as easily take other objects as a parameter, if desired, giving you more flexibility over when things lock down.

The rest of your code will run identically!

Hope this helps!

using Object as a mutex in java

According to the JavaDoc,

IllegalMonitorStateException is thrown
"to indicate that a thread has
attempted to wait on an object's
monitor or to notify other threads
waiting on an object's monitor without
owning the specified monitor."

In order to call mutex.wait() or mutex.notify(), the calling thread must own a lock on object mutex.

This exception is thrown if you call it without a preceding synchronized (mutex) { }

Check out the nice animation of wait and notify in this link : How do wait and notify really work?

Does the JVM create a mutex for every object in order to implement the 'synchronized' keyword? If not, how?

Speaking as someone who has looked at the way that some JVMs implement locks ...

The normal approach is to start out with a couple of reserved bits in the object's header word. If the object is never locked, or if it is locked but there is no contention it stays that way. If and when contention occurs on a locked object, the JVM inflates the lock into a full-blown mutex data structure, and it stays that way for the lifetime of the object.

EDIT - I just noticed that the OP was talking about OS-supported mutexes. In the examples that I've looked at, the uninflated mutexes were implemented directly using CAS instructions and the like, rather than using pthread library functions, etc.

Public final mutex for Java thread safety

If you want to synchronize between two methods of the same class, then mutex is something you should opt for.

exposing mutex as a public variable is against object oriented principles, and hence of course not recommended.

While working from outside the class, acquiring lock on the object you are working on is the best option, I would say.

X instance = new X();
synchronized(instance) {
instance.method1();
instance.method2();
... ... ...
}

Implement a mutex in Java using atomic variables

You want to build a, or use an existing, semaphore.

This has a better answer.



Related Topics



Leave a reply



Submit