We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Here is a code example from LearningProcessing book:
void draw() {
background(255);
// Update and display all balls
for (int i = 0; i < balls.length; i ++ ) { // Whatever the length of that array, update and display all of the objects.
balls[i].gravity();
balls[i].move();
balls[i].display();
}
}
void mousePressed() {
// A new ball object
Ball b = new Ball(mouseX,mouseY,24); // Make a new object at the mouse location.
balls = (Ball[]) append(balls,b);
What I am not clear about is: when the program is running, and then press mouse,
draw
function will halt immediately (even it is in the middle line of its code), mousePressed
function will run once, then draw
function continue to run until mousePressed
is triggered again or the program reload or stopped? draw
function will finish its latest run and then halts, then mousePressed
function will run once, then draw
function continue to run until mousePressed
is triggered again or the program reload or stopped? draw
and mousePressed
functions are running at the same time, but mousePressed
runs only once, draw
will continue until the program is stopped. which one is correct way of understanding their execution order behind the curtain? or how should I understand it?
Thank you!
Kenny
Answers
2nd option is closer to what really happens. :-j
Processing has a Thread called "Animation" which runs all its callbacks like setup(), draw(), mousePressed(), etc.
However, there's another "hidden" Thread called "Dispatch Event" which enqueues every input Event like keyboard & mouse while draw() is running.
Once draw() finishes, all accumulated Event are dequeued 1 by 1.
Then the corresponding Event callback is invoked.
But they're still run synchronously by the "Animation" Thread.
Unless noLoop() is active. Then the "Dispatch Event" itself is responsible for the dequeue executions.
The correct one is #3. The draw() function will just keep going over and over, no matter what. When the mouse is pressed, the code inside the mousePressed() function happens.
The explanation is that they're running in different threads.
Example sketch below proves that Event instances are enqueued asynchronously.
But their dequeue happens synchronously after draw() finishes.
Pay attention that the Event's println() statements only happen after each 5 seconds.
Since draw() callbacks are configured as
frameRate(1/5.0);
https://Processing.org/reference/frameRate_.html
Just hit some keys or click at the mouse during the 5 seconds between each draw():
@GoToLoop@TfGuy44 Thank you for the detailed answers and they are very helpful, @GoToLoop 's example in particular. I experimented the code (only altered a little bit) for a while in the following ways:
Both ways gave me the same result:
My findings/hypothesis of this outcome is consistant with the answers given by @GoToLoop:
What do you think? Is it what in your mind? or something is still not right?
Thanks,
here is the code I experimented upon the code provided by @GoToLoop
Here is the console messages: