← Back to team overview

mugle-dev team mailing list archive

Re: Static methods with 'synchronized'

 

> May be you are correct. I've used synchronized when working with threads
> and i *thought* synchronized also makes the statements wait for a call to
> return within that method.
>
> For example, if a query was made, I'd want it to wait till there's a
> feedback from the query without using a callback.
>

Ah, no, that can't possibly be. In Java (and all other languages), calls to
a method *always* wait for a return statement before continuing (this is
called "synchronous calling"), so normally this is already the behaviour you
would expect.

The reason in GWT we are getting calls to methods which *don't* wait for a
return (this is called "asynchronous calling") is that we are calling across
the client/server boundary, and the JavaScript which GWT is generating has
no idea *how* to wait for a return (this is sort of a failing of
JavaScript). This is why we are forced to use the Async classes with
callbacks. It's the only way to have code run when you finally receive a
response from the method (which is on the server side), and there is no way
to abstract over it.

Therefore, if I am right about what "synchronized" actually does, I would
avoid using it for static methods unless you need thread-safe access to
static variables -- otherwise it will just slow down threaded code, and
possibly deadlock.

Follow ups

References