Synchronized keyword ignored??


J

Josh G

Is java ignoring my synchronized keyword or do I have a wrong idea as to
what it should do? I have a method looking somewhat like this:

synchronized void dostuff() {

System.out.println("");
System.out.println("********************************* Starting dostuff
for " + myname);


//Does stuff in here...


System.out.println("********************************* ending dostuff
for " + myname);
System.out.println("");

}

Anyway the method is synchronized, but it's being entered multiple times
by the various threads I create. What am I doing wrong?

Cheers,
-Josh G
 
Ad

Advertisements

G

Gordon Beaton

Is java ignoring my synchronized keyword or do I have a wrong idea
as to what it should do? I have a method looking somewhat like this:

synchronized void dostuff() { [...]
}

Anyway the method is synchronized, but it's being entered multiple
times by the various threads I create. What am I doing wrong?

The synchronized keyword says that the monitor for the specific
monitor object must be held before the code block can be entered.
Every object has its own distinct monitor.

When you declare a method synchronized, "this" is the implied object.
So if your threads are holding different objects, there are separate
monitors involved that do not mutually synchronize.

If you want them to, you need to use a single monitor and specify it
explicitly someplace in the code, e.g.:


dostuff() {
synchronized (foo) {
}
}

....making sure that the same "foo" is used by each thread.

Depending on the synchronization scope you need, it could be as simple
as "this.class" or "Object foo = new Object();" in a suitable
location, depending on the synchronization scope you need.

/gordon
 
S

Stefan Poehn

Do you see output with two consecutive "Starting dostuff"
without and "ending doStuff" in between?

That should not happen and this is what synchronized garantuees
in this case.

Regards
Stefan
 
T

Tor Iver Wilhelmsen

Stefan Poehn said:
That should not happen and this is what synchronized garantuees
in this case.

Only if the threads use the same instance of the object.
 
S

Stefan Poehn

Tor Iver Wilhelmsen said:
Only if the threads use the same instance of the object.

Thank you for the addition. Synchronized works on the
object, not on the class.
 
Ad

Advertisements

T

Thomas G. Marshall

Gordon Beaton said:
Is java ignoring my synchronized keyword or do I have a wrong idea
as to what it should do? I have a method looking somewhat like this:

synchronized void dostuff() { [...]
}

Anyway the method is synchronized, but it's being entered multiple
times by the various threads I create. What am I doing wrong?

The synchronized keyword says that the monitor for the specific
monitor object must be held before the code block can be entered.
Every object has its own distinct monitor.

When you declare a method synchronized, "this" is the implied object.
So if your threads are holding different objects, there are separate
monitors involved that do not mutually synchronize.

If you want them to, you need to use a single monitor and specify it
explicitly someplace in the code, e.g.:


dostuff() {
synchronized (foo) {
}
}

...making sure that the same "foo" is used by each thread.

Depending on the synchronization scope you need, it could be as simple
as "this.class" or "Object foo = new Object();" in a suitable
location, depending on the synchronization scope you need.


I'd suggest /static Object foo/, in that case.

I find this a little neater than either of those:

public class
{
static Object mutexLock = new Object();

dostuff()
{
synchronized (mutexLock)
{
}
}
}
 
Ad

Advertisements


Top