This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring 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 ...
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Using threads effectively  RSS feed

Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a small task that continually gets triggered. How would I use Threads effectively to make sure the previous one always finishes before the next one gets started?
If this was a normal piece of code I'd probably use some kind of flag. But this involves calling another piece of code (which I've no control over) to speak to the user.
I pass the method a string to be spoken. The invisible processing takes care of everything else. Immediately afterwards, some other string gets passed to the method for speaking and the first one still hasn't finished. I have no way of telling when the first method finishes. I'm thinking about using Threads in some way to improve my calls to the speaking method.
I know this might sound a little obscure but has anyone got any hints or ideas about a way to do this?
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,
Since you want to perform operations sequencially you are not going to be using Threads effectively anyway. The idea with threads is that more than one thing can be happening at almost the same time (i.e., painting components, user interactions, etc.). By making the Threads work in sequence you are just adding more overhead and making execution time longer for no good reason.
If you want sequencial then just call a method. If you don't like that then you can generate events (at least that will happen in some order and can be behind where you are at now).
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul, You could make the method synchronized and then use the wait() and notify() methods to ensure your thread doesn't execute until the previous one is finished. You can set a flag to represent a currently executing thread. When another thread calls this method, if the flag is set it goes into a waiting pool. When the current thread finishes, your method calls notify() and this selects a thread from the wait pool. I think the only problem may be if you have multiple threads in the wait pool... you won't know which one will be activated.
I can't beleive you just said that. Now I need to calm down with this tiny ad:
Download Free Java APIs to Work with Office Files and PDF
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!