Re: threads and Window.dispose
From: John Harrison (john_andronicus_at_hotmail.com)
Date: 03/29/04
- Next message: John Harrison: "Re: threads and Window.dispose"
- Previous message: Babu Kalakrishnan: "Re: How do I limit input in a JTable cell to integers only?"
- In reply to: Babu Kalakrishnan: "Re: threads and Window.dispose"
- Next in thread: Babu Kalakrishnan: "Re: threads and Window.dispose"
- Reply: Babu Kalakrishnan: "Re: threads and Window.dispose"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 29 Mar 2004 13:50:10 +0100
"Babu Kalakrishnan" <k.a.l.a@sankya.com> wrote in message
news:c49582$2gb929$1@ID-76750.news.uni-berlin.de...
> John Harrison wrote:
> > I'm working on a large java app which was written by someone else. I'm
also
> > a java newbie.
> >
> > My problem is that the application hangs on Mac OS X with the 1.4.2
> > platform, it did not hang on 1.3.1 and it does not hang on Windows.
> >
> > It seems to be a thread deadlock situation and the cause is code like
the
> > following
> >
> > class SomeWindow extends Window
> > {
> > ...
> > public void dispose()
> > {
> > synchronized (this)
> > {
> > // some app specific code
> > ...
> > super.dispose();
> > }
> > }
> > ...
> > }
> >
> > The problem goes away if I remove the synchronized keyword, it also goes
> > away if I remove the call to super.dispose() from inside the
synchronized
> > clause.
> >
> > So my question is whether code like the above is legitimate, or is there
> > something wrong with disposing of a Window while synchronizing on it.
> >
>
> The general rule of thumb to be followed while synchronizing to any object
is
> that you should have an idea as to which other code is synchronized to the
> same object. This is essential for building a deadlock free system.
>
> In my experience, it is inherently unsafe to use any object in the
java.awt or
> javax.swing hierarchy for synchronization.This is basically because the
awt
> subsystem is not threadsafe, and there is absolutely no documentation on
which
> parts of Sun's code is synchronized to which object.
>
> Therefore for achieving your synchronization requirements, it is always
best to
> create a separate object to lock on (instead of synchronizing on "this").
>
> Of course there are some cases where you *need* to synchronize with the
"this"
> object (or on an object of a class that belongs to the java library),
because
> you want to depend on the thread safety of inherited methods in that
class.
> In such a case you should ensure that you know exactly how the documented
> synchronization behaviour of this class is. There are several parts of the
> java library with reasonably well defined synchronization behaviour (such
as
> the Collections classes), on which you can depend on - but the awt / swing
> classes certainly don't fall into that category.
>
> BK
>
Thanks for the info. Your argument that you need to know what other code is
synchronized on the same object and that this is not documented for
Swing/AWT classes certainly makes sense to me.
The other thing I've noticed is that this deadlock only occurs when there
are no other windows on the screen. This occurs in two situations, removal
of the splash screen and removal of the final window on program exit. As you
say its probably the internal details of the implementation that are
relevant here.
As I said I inherited this code and I'm currently trying to find out why
synchronize(this) appears in the dispose method (it does so consistently
throughout the code). It entirely possible that there is no good reason for
this synchronization at all, certainly none leaps out when looking at the
code.
In my (possibly naive) view this would seem much more reasonable.
class SomeWindow extends Window
{
public void dispose()
{
synchronized (this)
{
// app specific code
...
}
super.dispose();
}
in other words if the super class needs to synchronize anything, it can do
it itself. There's no threading or eventing within the app specific code,
just some simple cleanup. Do that seem more reasonable to you?
john
- Next message: John Harrison: "Re: threads and Window.dispose"
- Previous message: Babu Kalakrishnan: "Re: How do I limit input in a JTable cell to integers only?"
- In reply to: Babu Kalakrishnan: "Re: threads and Window.dispose"
- Next in thread: Babu Kalakrishnan: "Re: threads and Window.dispose"
- Reply: Babu Kalakrishnan: "Re: threads and Window.dispose"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|