M
martijn
Hi,
I programmed my own custom gui system as an excercise, but I have some
troubles with threads accessing it simultaneous. I've posted this in
the
java.programmer group because its not AWT or Swing specific issue.
Before going into the actual problem I'll quickly tell how my gui
system is
setup:
-GuiComponent which handles all component specific methods, like
processing
input events, location, appearance etc.
-GuiContainer, a container holds components, the container also extends
component (so a container can contain other containers).
In my main app I add one "parent" container. This container is given
the AWT
peer component as a reference so it knows what size it has etc. I Also
dispatch all mouse and key events to this parent container, the
container
delivers these to its components. Each container holds a list with
pointers
to active components (which it should draw and deliver events to) which
I'll
call activeList.
Now the problem I have occurs while painting. In the paint method of my
app
I call the parent container's render(Graphics g), which will trigger
the
parent container to iterate through all its components and draw them to
that
graphics context. This itself works ok.
But in some rare scenario's it throws a nullpointer exception, this is
caused by the AWT dispatch threads:
In my main app I call the parent container to draw its components, but
when
a mouse event occurs during this painting it's dispatched to the
components
in a seperate thread (because the events are triggered by the AWT
processMouseEvent, processKeyEvent on the AWT peer component). When a
component receives an event which triggers it to register itself
inactive,
it updates the container's activeList to remove itself. The paint
however is
also still working in the other thread, it loops through the
activeList,
which was updated while rendering which may cause a nullpointer
exception.
What would be a elegant solution to this problem?
I thought of creating a copy of the activeList in the render method,
but
this would be costy (for each frame a copy of the entire list) and
would
still result in nullpointer exceptions if the actual component is
removed.
The synchronized attribute would be no solution ofcourse since it only
protects that method from multiple threads accessing it. I would need
some lock on the activeList attribute...
Any ideas?
I programmed my own custom gui system as an excercise, but I have some
troubles with threads accessing it simultaneous. I've posted this in
the
java.programmer group because its not AWT or Swing specific issue.
Before going into the actual problem I'll quickly tell how my gui
system is
setup:
-GuiComponent which handles all component specific methods, like
processing
input events, location, appearance etc.
-GuiContainer, a container holds components, the container also extends
component (so a container can contain other containers).
In my main app I add one "parent" container. This container is given
the AWT
peer component as a reference so it knows what size it has etc. I Also
dispatch all mouse and key events to this parent container, the
container
delivers these to its components. Each container holds a list with
pointers
to active components (which it should draw and deliver events to) which
I'll
call activeList.
Now the problem I have occurs while painting. In the paint method of my
app
I call the parent container's render(Graphics g), which will trigger
the
parent container to iterate through all its components and draw them to
that
graphics context. This itself works ok.
But in some rare scenario's it throws a nullpointer exception, this is
caused by the AWT dispatch threads:
In my main app I call the parent container to draw its components, but
when
a mouse event occurs during this painting it's dispatched to the
components
in a seperate thread (because the events are triggered by the AWT
processMouseEvent, processKeyEvent on the AWT peer component). When a
component receives an event which triggers it to register itself
inactive,
it updates the container's activeList to remove itself. The paint
however is
also still working in the other thread, it loops through the
activeList,
which was updated while rendering which may cause a nullpointer
exception.
What would be a elegant solution to this problem?
I thought of creating a copy of the activeList in the render method,
but
this would be costy (for each frame a copy of the entire list) and
would
still result in nullpointer exceptions if the actual component is
removed.
The synchronized attribute would be no solution ofcourse since it only
protects that method from multiple threads accessing it. I would need
some lock on the activeList attribute...
Any ideas?