Re: [OpenMap Users] Location threading problem with DeclutterMatrix.getGraphics()?

From: Carsten Ø. Madsen <com_at_navicon.dk>
Date: Mon, 21 Jan 2008 13:30:25 +0100

This fixes the problem

...
    /**
     * Added this to work around problem with multi threaded calls to
     * DeclutterMatrix.getGraphics.
     *
     * Replaced all calls to DeclutterMatrix.getGraphics with
getGraphics. This
     * does have a performance impact!
     *
     * _at_see DeclutterMatrix
     *
     */
    private static class ThreadGraphicsDispenser extends ThreadLocal {
        _at_Override
        public Object initialValue() {
            BufferedImage bi = new BufferedImage(1, 1,
                    BufferedImage.TYPE_INT_RGB);
            GraphicsEnvironment ge = GraphicsEnvironment
                    .getLocalGraphicsEnvironment();
            Graphics2D workingGraphics = ge.createGraphics(bi);
            return workingGraphics;
        }
    }

    private static ThreadGraphicsDispenser graphicsDispencer = new
ThreadGraphicsDispenser();

    public static Graphics2D getGraphics() {
        return (Graphics2D) graphicsDispencer.get();
    }
...

BR
Carsten


Carsten Ø. Madsen wrote:
> Hello
>
> I'm running an OM app with multiple location layers on a quad core box
> and I get the following exception on a regular basis:
>
> Exception in thread "Thread-64" java.lang.NullPointerException
> at com.bbn.openmap.layer.location.Location.generate(Location.java:572)
> ...
> at
> com.bbn.openmap.layer.location.LocationLayer$LocationWorker.construct(LocationLayer.java:176)
>
> at com.bbn.openmap.util.SwingWorker$2.run(SwingWorker.java:116)
> at java.lang.Thread.run(Thread.java:619)
>
> The Location.generate() code goes like this:
>
> public boolean generate(Projection proj) {
> if (label != null) {
> label.setY(origYLabelOffset);
> label.setX(origXLabelOffset);
> }
>
> java.awt.Graphics g = DeclutterMatrix.getGraphics();
> if (g != null && label != null) {
> g.setFont(label.getFont());
> // Now set the vertical offset to the original place based
> // off the height of the label, so that the location place
> // is halfway up the text. That way, it looks like a
> // label.
> int height = g.getFontMetrics().getAscent(); // line 571
> currentFontDescent = g.getFontMetrics().getDescent();
> ....
>
>
> as LocationLayer uses a LocationWorker (thread) to run prepare and
> DeclutterMatrix.getGraphics() returns a static Graphics I assume the
> exception is caused by multiple threads simultaneously accessing the
> single graphics instance returned by DeclutterMatrix.getGraphics. As
> the location layers stop responding after the exception this rather
> problematic.
>
> As far as I can tell this is a general problem when having multiple
> LocationLayers running on a multi core box?
>
> BR
> Carsten
>
>
> --
> [To unsubscribe to this list send an email to "majdart_at_bbn.com"
> with the following text in the BODY of the message "unsubscribe
> openmap-users"]

--
[To unsubscribe to this list send an email to "majdart_at_bbn.com"
with the following text in the BODY of the message "unsubscribe openmap-users"]
Received on Mon Jan 21 2008 - 07:35:00 EST

This archive was generated by hypermail 2.3.0 : Tue Mar 28 2017 - 23:25:08 EDT