Java Magazine: Design Patterns by Andrew Benstock

Java Magazine: Design Patterns by Andrew Benstock

Author:Andrew Benstock [Benstock, Andrew]
Language: eng
Format: azw3
Publisher: UNKNOWN
Published: 2018-12-02T16:00:00+00:00


The important detail here is to perform the coordinate transformations off the rendering thread.

behavior. This can be somewhat mitigated by wrapping the while loop in the thread-safe Platform.runLater() Runnable, which is commented out in the previous code. Doing so improves the performance as follows:

Total elapsed time: Total ns: 497896, 0:s:0:ms:497:us:896:ns Drew batch size of 56194 For a similar quantity of points, total execution time has shrunk from 24 ms to less than 1 ms. Don’t be fooled though: this reduced time is deceptive because what I have done is handed the workload to the JavaFX platform to manage and then execute at the next available rendering pass. This approach takes much less time than the original approach without runLater(), but now I am sending a large number of Runnables to the JavaFX platform for processing.

This solution scales well to the 10,000-point range. However, what if you want to add an order of magnitude and make it 100,000 points? Given all the coordinate transform math and other work being done, the time spent in each Runnable instance is too large. Further, given the high frequency of data bursts, there are far too many Runnable instances being sent to the JavaFX platform for processing. This pattern could potentially throw cryptic rendering exceptions, such as java.lang.InternalError: Unrecognized PGCanvas token: 64, when the underlying JavaFX engine cannot keep up.

Second Approach An alternative approach is a slight variation to the previous method. This implementation seeks to do as little as possible in the blocking Platform.runLater() Runnable. The code for this is shown toward the end of the following implementation of the drawNext_ArrayTransforms() method:

private int drawNext_ArrayTransforms() {

int size = pointQueue.size();

GraphicsContext g = canvas.getGraphicsContext2D();

//temporary arrays to hold the transformed canvas coordinates //Be sure to use primitive double type to minimize memory double[] xArray = new double[size];

double[] yArray = new double[size];

//loop across all available points by polling the concurrent queue for (int i = 0; i < size; i++) {

PointPojo point = pointQueue.poll();

//coordinate transformations stored in temporary arrays xArray[i] = transformXToScreen(point.x);

yArray[i] = transformXToScreen(point.y);

//encourage finalization of the object I polled

point = null;

}

//Using arrays to hold transformed coordinates allows me //to use a runLater() thread while minimizing blocking time Platform.runLater(() -> {

//The key is minimizing time spent in this blocking thread for (int i = 0; i < size; i++) {

g.fillOval(xArray[i], yArray[i], radius, radius); }

});

return size;

}

You will notice that I use a for-loop to poll objects from the concurrent queue. I could have used an iterator instead of the for-loop to process the available points. However, iterators take more than an order of magnitude longer than a loop that calls poll(), because the JVM Hotspot has not yet “warmed up” to the iterator bytecode and so it is not JIT’ed but rather would be interpreting each command.

The important detail to mention here is that the code in this alternative method performs the coordinate transformations off the rendering thread. Moving these time-expensive calculations off the rendering thread minimizes the time spent blocking in the JavaFX ren- dering thread. By minimizing the



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
Whisky: Malt Whiskies of Scotland (Collins Little Books) by dominic roskrow(56073)
What's Done in Darkness by Kayla Perrin(26615)
The Fifty Shades Trilogy & Grey by E L James(19095)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19079)
Shot Through the Heart by Mercy Celeste(18952)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(17131)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(17018)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(16893)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16840)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16466)
The Subtle Art of Not Giving a F*ck by Mark Manson(14375)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14157)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(13669)
Scorched Earth by Nick Kyme(12785)
Drei Generationen auf dem Jakobsweg by Stein Pia(10980)
Suna by Ziefle Pia(10902)
Scythe by Neal Shusterman(10365)
The Ultimate Python Exercise Book: 700 Practical Exercises for Beginners with Quiz Questions by Copy(10189)
D:\Jan\FTP\HOL\Work\Alien Breed - Tower Assault CD32 Alien Breed II - The Horror Continues Manual 1.jpg by PDFCreator(10147)
De Souza H. Master the Age of Artificial Intelligences. The Basic Guide...2024 by Unknown(10143)