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
 
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
 
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
 

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,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top