• 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
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Threads - stopping a while loop  RSS feed

 
Greenhorn
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.

Cheers!
[ February 25, 2006: Message edited by: Chris Flynn ]
 
(instanceof Sidekick)
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!