开发者

Waiting for a thread to finish from a 3rd party library (OpenMap/Java)?

开发者 https://www.devze.com 2022-12-16 20:37 出处:网络
My application currently has an interesting problem. This problem is with OpenMap but could probably be applied to any 3rd party library.

My application currently has an interesting problem. This problem is with OpenMap but could probably be applied to any 3rd party library.

In this particular example, our code needs to create our OpenMap tool and load its layers (in the background) and take a screenshot of a particular point of interest.

However, the problem is that the OpenMap librar开发者_运维百科y creates its own thread(s) to load these layers, thus returning to our code to take a screenshot immediately and most times the screenshot is empty or incomplete.

The pseudo-code of our application is like this:

check database for layers
load layers using OpenMap
take screenshot of map at point of interest

I've assumed that some sort of thread management was in order, but how can this be done when the library is using its own thread(s) that we have no access to? Also, OpenMap has no returns or flags to indicate that these thread are finished (that I've seen).

Any suggestions?

Thank you


I don't know OpenMap, but the normal way is to register some callback with the 3rd party library. In Java that means implementing an Interface and registering as a listener at the object on which you call the API method. You (almost?) never have to (or should) know something about the internal thread handling of a library!


Layers can be written for OpenMap so they can respond to projection queries and render into a java.awt.Graphics object. Extend the OMGraphicHandler layer, override the prepare() method so it returns an OMGraphicList for the projection set on the Layer. If you call Layer.renderDataForProjection(), the OMGraphicList returned in prepare will be rendered.

The com.bbn.openmap.image.ImageServer is a good object to use for managing the creation of images for your Layers.


Had the same problem with the 3rd party jpeg extractor pulling the image before the MapBean was finished producing the image. Followed the hint about the invoke later and added the following code between my layer handling code and my export to graphic code:


//...OpenMap editing code stops here ...

final Runnable doWaitUntilTheMapBeanIsFinishedRendering = new Runnable() {
   public void run()
   {
       System.out.println("Ding, fries are done!");
   } 
};

try
{
    SwingUtilities.invokeAndWait( doWaitUntilTheMapBeanIsFinishedRendering );
}
catch( Exception e )
{
    e.printStackTrace();
}

byte[] image = JPEGImageFormatter.getImageFromMapBean( mapbean );
0

精彩评论

暂无评论...
验证码 换一张
取 消