Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
  • 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 ...
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Threads - stopping a while loop  RSS feed

Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm only a basic Java programmer, trying to do something quite complex, so please forgive me. I've tried reading a textbook and some tutorials, but I can't get my head around this problem!
Assuming I have 2 threads:

Thread A - the 'main' thread, contains UI
Thread B - which once started goes into a while loop reading a buffer. This is a Runnable object

I'm wondering about the best way to get the while loop in B to stop (it's capturing data from a microphone) from the UI. Assuming: should I use: in thread B, or is there a better way?

If this is the best way, then should the variable be in thread A (so I'd pass a reference of object A to B in the constructor, and say or if I put it in thread B (as well as a method) and called the method from thread A, would the method run during the while loop, or after (e.g. never!) ?

What is the most efficient and elegant way to go about this? I need the thread to continue doing something else once the while loop has continued, so can't say which I think I've seen elsewhere.

[ February 25, 2006: Message edited by: Chris Flynn ]
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
while(!stop) or while(running) is pretty common. I've always made the boolean private with a synchronized setter.

The real tough ones have some blocking operation in the middle of the loop like a blocking read so they might never make it back to the top of the loop.
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!