Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Programming with Types this week in the Angular and TypeScript forum
or The Design of Web APIs in the Web Services forum!
  • 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
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

AWT and Threads

 
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi...


I'm trying to understand how I could implement the following in AWT(This is a project that my friend was given in his Computer Science course last semester that I am curious to implement).The idea is to have two different threads,each updating the GUI.Each Thread should display a straight line that grows according to time elapsed.I tried to do this first like this(sorry this is from my local history and may not be 100% correct,but it should serve to illustrate what I want to do):



But, I think there is some sort of blocking happening with the paint method as I only ever saw one line getting drawn.What is the correct approach to do something like this?I'd like to figure this out as there are times when I may need to write multithreaded awt components for my job(writing MHP applications.....or trying lol)
 
Sheriff
Posts: 21836
105
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Concurrency in Swing. It also applies to AWT.
 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay so I tried to instead fire the threads by using java.awt.EventQueue.invokeLater(Runnable) but that doesn't seem to help.

I don't understand how this is possible actually.As far as I understand there is only the EventQueue thread that performs paint() operations,so regardless of whether or not I fire off multiple threads to update the GUI at the same time(multiple paint() methods associated with multiple components) they are still going to execute sequentially as it is a Queue that is handling the painting....What am I missing here?
 
Rob Spoor
Sheriff
Posts: 21836
105
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can't even call paint in a different thread. The results are undocumented.

If you need to have a regular repaint you should check out javax.swing.Timer. This fires new events on the Event Dispatcher Thread.
 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay I'll check it out...thanks
 
Liar, liar, pants on fire! refreshing plug:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!