I have an application that loads a series of large images on startup. This is a Processing application, so most things run on the single main animation thread, and it's using Processing's OpenGL renderer.
EDIT: the RuntimeException is thrown when some process within JOGL takes > 5 seconds. Making it back up the stack to subsequent loadImage() calls means the RuntimeException is avoided. I'm not clear yet how to repro this and so haven't found a way around it save the ugly try-catch + reflection workaround here.
The image loads sometimes take longer than 5 seconds, and when they do, JOGL throws a RuntimeException from within RecursiveLockImpl01Unfairish.lock(). My understanding is that RecursiveLockImpl01Unfairish.lock() complains if the main GL animation thread halts execution for > 5 seconds.
Is there a simple fix for this? I could shift the image loads to a different thread and reshuffle my init sequence to be more asynchronous, but that's a lot of work for something that happens only once, on application init, at a time when the application has plenty of time to start up regardless.
(Note: this is for an installation, no one will be present or trying to use the application when it first launches in the morning, so a delay of many seconds on init is not a problem.)
Related
I am making a javafx application that simulates a robot vacuum.
I want it to be automated so it would vacuum the environment by itself.
I need to insert a delay so a human can see the steps the vacuum is taking as it traverses the environment.
So far all the delay methods I have tested crash my program if they are inside a while loop.
If I put it outside the while and just click a button for the next step, everything works fine.
It also works fine if I set the delay to really short time, like 1 ms.
Any ideas of why this is happening?
Any application that executes a set of instructions for a while (is busy) and cannot respond to user input or system events is "seen" by Windows as "not responding" and when you try to interact with a "not responding" program, Windows will tell you it crashed.
The problem, you see, is that you try to delay interface updates with a while loop, and that makes your program execute something for a while and while is busy executing your loop it cannot respond to system or user events.
If you want to make delayed updates, use multithreading. Your while loop is blocking the main thread which is also responsible for rendering and taking any input, so you cannot block this thread. Create another thread and share state (eg. use observer pattern). And then you can execute TimeUnit's sleep() in this helper thread and it won't make your app "crash".
I'm developing a Processing view (with v2.2.1 that still extends Applet).
I use some pushMatrix()/popMatrix() to do transformations and to represent composite objects (I'm aware of the 32 depth matrix stack limit and relatively sure I don't reach that depth in composition and/or successive transformations or dont pair push and pops properly).
So far I didn't have any unexpected issue, but after introducing yet another component(not the first text enabled component at all), I'm starting to occasionally get errors like these:
The font size is too large to be properly displayed with OpenGL
Exception in thread "Animation Thread" java.lang.RuntimeException:
Image width and height cannot be larger than 0 with this graphics
card. at processing.opengl.Texture.setSize(Texture.java:1148) at
processing.opengl.Texture.init(Texture.java:213) at
processing.opengl.Texture.(Texture.java:160) at
processing.opengl.FontTexture.addTexture(FontTexture.java:134) at
processing.opengl.FontTexture.initTexture(FontTexture.java:103) at
processing.opengl.FontTexture.(FontTexture.java:71) at
processing.opengl.PGraphicsOpenGL.textLineImpl(PGraphicsOpenGL.java:3602)
at processing.core.PGraphics.textLineAlignImpl(PGraphics.java:4659)
at processing.core.PGraphics.text(PGraphics.java:4356) at
processing.core.PGraphics.text(PGraphics.java:4307) at
processing.core.PApplet.text(PApplet.java:13183) at
ygg.desktop.vm.extVM.MetadataProcessingVM.render(MetadataProcessingVM.java:81)
at
ygg.desktop.vm.extVM.MetadataProcessingVM.render(MetadataProcessingVM.java:88)
at ygg.desktop.vm.groups.TreeLayout.render(TreeLayout.java:43) at
ygg.desktop.vm.groups.RenderArea.render(RenderArea.java:167) at
ygg.desktop.view.MainView.draw(MainView.java:179) at
processing.core.PApplet.handleDraw(PApplet.java:2386) at
processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:240)
at processing.core.PApplet.run(PApplet.java:2256) at
java.lang.Thread.run(Thread.java:745)
which refers to (note that I get it both on first and second text on different runs) *father is the PApplet instance
father.pushMatrix();
father.translate(posX, posY+8);
father.rotate(-father.HALF_PI);
father.fill(father.color(30,30,30));
father.textAlign(father.CENTER);
father.textSize(16);
**father.text(md.getId()!=null?md.getId():"NONE",-(finalY-posY)/2,width/2);**
father.fill(father.color(220,220,50));
father.textSize(12);
**father.text(md.getId()!=null?md.getId():"NONE",-(finalY-posY)/2,width/2);**
father.popMatrix();
or
Exception in thread "Animation Thread" java.lang.RuntimeException: Too
many calls to popMatrix(), and not enough to pushMatrix(). at
processing.opengl.PGraphicsOpenGL.popMatrix(PGraphicsOpenGL.java:3811)
at processing.core.PApplet.popMatrix(PApplet.java:13322) at
ygg.desktop.vm.extVM.MetadataProcessingVM.render(MetadataProcessingVM.java:72)
at ygg.desktop.vm.groups.TreeLayout.render(TreeLayout.java:43) at
ygg.desktop.vm.groups.TreeLayout.render(TreeLayout.java:46) at
ygg.desktop.vm.groups.TreeLayout.render(TreeLayout.java:46) at
ygg.desktop.vm.groups.RenderArea.render(RenderArea.java:167) at
ygg.desktop.view.MainView.draw(MainView.java:180) at
processing.core.PApplet.handleDraw(PApplet.java:2386) at
processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:240)
at processing.core.PApplet.run(PApplet.java:2256) at
java.lang.Thread.run(Thread.java:745)
on
father.pushMatrix();
father.translate(posX, posY+8);
father.rotate(-father.HALF_PI);
father.fill(father.color(30,30,30));
father.textAlign(father.CENTER);
father.textSize(16);
father.text(md.getId()!=null?md.getId():"NONE",-(finalY-posY)/2,width/2);
father.fill(father.color(220,220,50));
father.textSize(12);
father.text(md.getId()!=null?md.getId():"NONE",-(finalY-posY)/2,width/2);
**father.popMatrix();**
}
I am no expert in OpenGl and/or Processing at all, so I was wondering how can successive executions (with no previous jvm left open) lead to different outcomes without any random element in my code (apparently if the application doesnt crash immediately it keeps working no matter how many objects I create), also I don't understand how can I get that exception on popMatrix right after a push given that all the calls are in the draw cycle and of course no thread (that I'm aware) there.
Before proceeding I would like to know what am I getting so wrong (if it as already transpired) and what could I do to achieve stability at each run, also I would like to know if multiple instances of PApplet clients will necessarly confict with each others.
It's hard to pin point all the errors without the full code listing.
From what you posted it looks like you have unpaired pushMatrix()/popMatrix() calls. For each pushMatrix() operation you will need a popMatrix() when you're done doing local coordinate system transformations.
Be sure to read the 2D Transformations Processing tutorial for more details.
I have recently learnt that jMonkey operates on only a single thread which is its openGL render thread. However I'm not able to understand it completely. I can understand that it executes all update and initialize() calls on a single update loop but input should be independent of this update loop, otherwise it will become a polling mechanism.
What exactly happens when the jMonkey application starts. Three tasks that it needs to do is run a update loop, run initialize methods of app states once, do rendering and constantly cater to events? How does it manage all of this via a single thread?
What happens when I add a new app state in the initialize method of another app state?
In input handling input manager notifies the various listeners about events. How are nifty callback events say onClick() are handled on the same render loop?
Lastly in which order this listen- update-render loop runs and where we can find code related to it?
jME uses an approach that's very common in a lot of game engines (and also used in some other user interface libraries such as Swing).
Everything that the game engine does is done in one thread. This can be called the LWJGL thread but as jME can work with alternatives to LWJGL it's more generic to call it the Render Thread or just the jME Thread.
Everything is indeed done on the Render thread, and it does indeed use a polling mechanism. For example if you are holding down the "left" key then each frame the relevant control or app state will be called on the render thread and will move you left by an amount modified by tpf. Tpf is time-per-frame and is very important for keeping smooth movement and letting game systems run at the same speed independently of frame rate.
The only thing within jME3 that commonly uses a separate thread is the Physics Engine. That has a thread that it uses for doing the physics updates and then changes are pushed through to the Render thread using appropriate mechanisms. The system handles this for you though so it is not something you need to worry about.
The thread runs around a game loop. Each time around the loop it checks for things it needs to do (like enqueued tasks, initialize app states, render, etc. It calls update in each active Controller and control, etc). Once it has finished updating everything it then goes on to perform the render. All of this happens every single frame, but computers are so fast that it can still handle all of that and render the game at good frame rates.
That's an implementation detail that you shouldn't need to worry about except to know that it will definitely work. The adding actually gets queued up and handled in the next frame I think.
Everything is handled from the same loop. jME calls through to Nifty to allow Nifty to do its processing. As part of that processing Nifty detects the events and fires off the callback. This means that the callbacks are already coming in on the render thread so you can safely modify the scene graph. jME uses some specially written collections such as SafeArrayList to allow you to modify the scene graph at the same time as iterating over it.
Update, Render, Update, Render, etc. Events firing generally happens as part of the update process when an update is detected. To find the code start by looking in the Application class, you should be able to find the main game loop in there by tracing through from the start() call.
The jME3 Threading Tutorial covers a fair amount of this:
http://hub.jmonkeyengine.org/wiki/doku.php/jme3:advanced:multithreading
I'd like to ask you about the best solution/idea how to solve a following situation.
I'm developing an Android app which on one of screens has a set of buttons. After clicking on any of them a kind of config is posted to the server over http.
To prevent multiple clicks one by one which could result in concurrency problems I decided that after each click on a particular button there'll be a waiting interval of 30 seconds before a config is sent to the server. If another click on the same button happens before this 30 seconds are exceeded, then the execution of method is delayed for another 30 seconds - as long as no new click is performed, then the config will be sent.
I need an idea of an algorithm which would implement the mechanism above. What I know is that I don't want to start a separate thread for each click (too heavy for my app). A potential idea is to make a queue of events and send them in a loop but idea of a running endless loop in a thread (or Handler) also isn't my favourite.
Maybe there's a kind of mechanism in Android or J2SE in general, that allows to schedule an execution of method to a given time in the future but still be able to postopone execution for some additional time before 30sec rolled out.
thanks in advance!
The javadoc and tutorial have information about the four applet lifecycle methods (init() -> start() -> stop() -> destroy()). But they talk mostly in abstract language.
What I'm looking for are concrete examples of when it makes a difference if I put my code in init vs start, and similarly for destroy vs stop. The only thing I've found so far is in the tutorial's description of the destroy method. It says:
Note: Keep implementations of the
destroy method as short as possible,
because there is no guarantee that
this method will be completely
executed. The Java Virtual Machine
might exit before a long destroy
method has completed.
(I'm a bit shocked that the above isn't in the javadoc.)
Edit: to be more specific: Can anyone provide a browser + JVM combo that, upon some specific action (switching tabs, hitting the 'back' button, etc.), invokes stop but not destroy (or start but not init)?
init and destroy are called when the applet is loaded or unloaded, respectively. It's possible for a browser to load an applet and stop it, but not destroy it, when navigating around, switching tabs, etc.
start and stop are for pausing and resuming the applet, in the case above (when the applet becomes, or ceases to be, shown on a page).
I don't know if any browser actually does keep an applet loaded, so it may not matter much. But as far as i learned it, the general rule is:
init should get the applet ready to run, but not actually set it in motion. The applet should be in a "stopped" state upon return from init. (A stopped applet should be using as few resources as practically possible, and no CPU.)
start should start the applet running (starting threads, etc). It generally won't read params and reload images and all that, as that should be done in init.
stop should undo what start does...returning the applet to the "stopped" state, but leaving it able to start again. It should not undo any of init's work, as that would leave the applet unstartable if the functionality is properly separated.
destroy should do any final cleanup before the applet is unloaded. It basically undoes init. It should not stop the applet; that's stop's job, and should already be done before destroy is called.
In practical terms, I think that start() and stop() were intended to be called each time an applet-bearing page was viewed (for example, using the browser's "back" and "forward" buttons), while init() and destroy() were only called once.
It's been about 15 years since I wrote an applet though, so I might be mis-remembering.