Cross-thread drawing from thread() in Processing 3 and 4
0 reputation · 26 Mar 2022, 06:35 UTC
Goal: Safely share state between a Processing thread() callback and the animation thread without calling drawing functions from the secondary thread.
Processing runs setup() and draw() on a single animation thread, while thread() starts an independent JVM thread. The reference warns against cross-thread drawing calls, yet Processing 3 and 4 do not document a supported marshaling API or invoke-later wrapper for returning work to the renderer. Authors are left choosing between polling a volatile flag inside draw() or using java.awt.EventQueue.invokeLater, neither of which is endorsed in the official reference.
Renderer behavior differs across JAVA2D, P2D, and P3D, and internal PApplet synchronization is not specified as stable between Processing 3 and Processing 4.
Is polling a volatile flag in draw() preferable to java.awt.EventQueue.invokeLater for cross-version sketches? Does the safer pattern depend on whether the sketch uses JAVA2D or P3D?