• 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
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Should use Thread or something else?

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am developing a Swing application that checks the stock level at a pre-defined interval. To check the stock level at a pre-defined interval, what should I use. Can I use a Thread that runs till the app shuts down?

Thanks in advance.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Certainly, thats the whole idea. It is best to use the setDaemon( true ) method before starting the Thread to simplify shutdown of the application. See the java.lang.Thread JavaDocs.

Bill
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alternatively, if you're running Java 5 or later, there are factory methods in java.util.concurrent.Executors for creating thread pools that execute tasks at regular intervals. For example:

newSingleThreadScheduledExecutor()
newScheduledThreadPool(int corePoolSize)
 
Sheriff
Posts: 21759
102
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alternatively, both java.util.Timer and javax.swing.Timer could be used.
 
Author
Posts: 3443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When working with Mult-Threading inside Swing be aware of it even dispatching mechanism. Swing components need to be accessed via the event dispatching thread. So the lengthy tasks should not occur in the event dispatching thread because this will hold up the dispatcher thread. Check invokeLater() and invokeAndWait() methods.
 
Rob Spoor
Sheriff
Posts: 21759
102
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point. If your code is lengthy, don't use javax.swing.Timer. That code will run completely on the dispatch thread and may therefore cause non-responsive user interfaces.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!