Synchronized question


J

Joona I Palaste

Is there any difference at run-time, no matter how small, between this:

public synchronized void foo() {
/* ... */
}

and this:

public void foo() {
synchronized (this) {
/* ... */
}
}

?

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Bad things only happen to scoundrels."
- Moominmamma
 
Ad

Advertisements

J

Jordan Zimmerman

Joona I Palaste said:
Is there any difference at run-time, no matter how small, between this:

public synchronized void foo() {
/* ... */
}

and this:

public void foo() {
synchronized (this) {
/* ... */
}
}

I don't know for certain if the generated code is different, but there is no
functional difference between the two.
 
T

Timour

Hello Joona,

First sample synchronized a method, while rest of the methods ain't
But in the second whole instance is synchronized.

Cheers,
Timour
 
J

Joona I Palaste

Timour said:
Hello Joona,
First sample synchronized a method, while rest of the methods ain't
But in the second whole instance is synchronized.

I don't think so. Both samples make only the method foo() synchronized.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Stronger, no. More seductive, cunning, crunchier the Dark Side is."
- Mika P. Nieminen
 
J

Jon Skeet

Timour said:
First sample synchronized a method, while rest of the methods ain't
But in the second whole instance is synchronized.

No, that's not true in the slightest.

The two code snippets are effectively equivlent.
 
N

Neal Gafter

Joona said:
Is there any difference at run-time, no matter how small, between this:

public synchronized void foo() {
/* ... */
}

and this:

public void foo() {
synchronized (this) {
/* ... */
}
}

?

Behaviorally, they are the same. However, some VMs have an easier time
generating (and running) good code for the former.

-Neal
 
Ad

Advertisements

D

Dario

Joona said:
Is there any difference at run-time, no matter how small, between this:

public synchronized void foo() {
/* ... */
}

and this:

public void foo() {
synchronized (this) {
/* ... */
}
}

The difference is "where" a synchronization is made:
in the caller in the first case,
in the called (i.e. foo) in the second case.

So a possible difference is the exception thrown
in the case this is equal to null.

Please consider the following statement:

X.foo();

If X is null then the thrown NullPointerException:
in the first case has a StackTrace
(see <http://makeashorterlink.com/?T215216C5>)
ending with the caller, but in the second case
it has a StackTrace ending with the called (i.e. foo).

So if your exception handling code uses
StackTraces there are differents codes to handle it
in the two different cases you made.

- Dario
 

Top