• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Problems with multiple threads/fast-updating GUI  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I have a problem. I have a JavaFX application class, where I am polling(.take() ) Lists from a BlockingQueue(information is entered in another thread) for recent information(Inside a Task and a Platform.RunLater). And after getting the List, I need to forward it
to a method inside the same(JavaFX application) class.
The problem seems, that when I add the List to the BlockingQueue outside javafx thread, it has all the elements inside of it, when I retrieve it in inside platform.RunLater, it also has all the needed elements,
but now when I call out a method with said List, and check the List size in that method, it is 0 i.e null. I'm not sure even how to call this problem.

My code:


Also a problem - my application gets about 40 objects with data in a second. I need to parse the data and then display it in a X Y chart. As I understand updating GUI that fast, isn't a possibility, so I thought i'd send
packets(Lists) of data with the platform.RunLater and then foreach through the list and display the new data all almost at once. And so every 500ms or depending on how much I set the Thread.Sleep().
If you have any better ideas or suggestions how to handle a lot of data, i'd be really happy to hear them, because it seems the application still freezes up at periods.








 
Saloon Keeper
Posts: 20655
122
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My money is on the standard mis-conception people have about how GUIs work. I can't be certain just from a casual reading, but it sounds likely.

In a GUI, you do not work-and-draw. Drawing is done as a separate process. All you provide is a list of what is to be drawn, but the actual drawing - or more precisely, rendering, is done by a private thread owned by the GUI itself.

In other words, it's not:



It's more like:


The "paint" is the cue for the graphics subsystem to wake up its rendering thread and set it to work using the items you told it to draw earlier.

This is one reason why having data come in faster than you can draw it shouldn't be much of a problem. The paint operation takes whatever data has been drawn up to that point. If more comes in, well, there's always the next paint cycle.

One reason why things are done this way has to do with layering (Z-stacking) of graphics operations. That is, draw a square and then draw a circle on top of it. If someone drags another window over the top of the window with the circle and square in it, that part of the display is considered "damaged". When the obstruction is removed, the damage needs to be repaired by re-painting what was obscured, and that's easier to do if the rendering system has a list of items to re-paint.
 
Sheriff
Posts: 24374
55
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Arthur!

As for your list which is mysteriously getting emptied, my guess is that something in another thread is emptying the list. My wild guess is that the process which adds lists to the blocking queue reuses a single list which it empties, rather than creating new lists all the time. Just a wild guess, though.
 
Arthur Jackson
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the late reply, took a break from programming for a day, but got the error fixed!

Tim Holloway wrote:

This is one reason why having data come in faster than you can draw it shouldn't be much of a problem. The paint operation takes whatever data has been drawn up to that point. If more comes in, well, there's always the next paint cycle.



A lot of data at once freezes the application, that's the problem.

Paul Clapham wrote:
As for your list which is mysteriously getting emptied, my guess is that something in another thread is emptying the list. My wild guess is that the process which adds lists to the blocking queue reuses a single list which it empties, rather than creating new lists all the time. Just a wild guess, though.



And you were completely correct, I wouldn't have come up with that myself, thank you.
 
Paul Clapham
Sheriff
Posts: 24374
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Arthur Jackson wrote:

Paul Clapham wrote:
As for your list which is mysteriously getting emptied, my guess is that something in another thread is emptying the list. My wild guess is that the process which adds lists to the blocking queue reuses a single list which it empties, rather than creating new lists all the time. Just a wild guess, though.



And you were completely correct, I wouldn't have come up with that myself, thank you.



Woo hoo!
 
Sheriff
Posts: 5931
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good job fixing the problem!  Would you like to post your solution here or tell us what you did so others can benefit?
 
Arthur Jackson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



That is what I had, it cleared the list after putting it in queue.

I replaced the .clear() method with making a new ArrayList

 
Paul Clapham
Sheriff
Posts: 24374
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Arthur Jackson wrote:I replaced the .clear() method with making a new ArrayList



Is there a reason why your List variable isn't a local variable in the read() method? (It's also confusing to have its name start with a capital letter, so it looks like a class name. Call it list instead.)

And personally I wouldn't gather the CustomObjects into groups before adding the groups to the queue, I would just add each CustomObject to the queue. (That's assuming that the task which removes objects from the queue takes them out of the groups and processes them individually.) That simplifies the programming, and it also avoids the situation where the code you posted ends abruptly with a half-filled list which is not yet added to the queue.
 
Arthur Jackson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Arthur Jackson wrote:I replaced the .clear() method with making a new ArrayList



Is there a reason why your List variable isn't a local variable in the read() method? (It's also confusing to have its name start with a capital letter, so it looks like a class name. Call it list instead.)


And personally I wouldn't gather the CustomObjects into groups before adding the groups to the queue, I would just add each CustomObject to the queue. (That's assuming that the task which removes objects from the queue takes them out of the groups and processes them individually.) That simplifies the programming, and it also avoids the situation where the code you posted ends abruptly with a half-filled list which is not yet added to the queue.



Actually now that I think about it, it doesn't have to be global indeed. And im gathering the objects so I can poll less in the GUI thread and then draw them all at once, to minimize freezing/lag.
 
Paul Clapham
Sheriff
Posts: 24374
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Arthur Jackson wrote:And im gathering the objects so I can poll less in the GUI thread and then draw them all at once, to minimize freezing/lag.



Did you make that change (from one object at a time to 20) based on what you saw when you sent one object at a time? And did sending 20 make a difference?
 
Arthur Jackson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Arthur Jackson wrote:And im gathering the objects so I can poll less in the GUI thread and then draw them all at once, to minimize freezing/lag.



Did you make that change (from one object at a time to 20) based on what you saw when you sent one object at a time? And did sending 20 make a difference?



I raised the 20 to 60, but yes I tried the sending one by one as well and there is a noticable difference.
 
Paul Clapham
Sheriff
Posts: 24374
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Arthur Jackson wrote:I raised the 20 to 60, but yes I tried the sending one by one as well and there is a noticable difference.



I'm glad to hear that. Often people write excessively complicated code because they think (with no evidence) that the simple code will be less performant in some way, and then have bugs in the more complicated code. So it's good that you started with the simple version.
 
ice is for people that are not already cool. Chill with this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!