Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads - stopping a while loop

 
Chris Flynn
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 ]
 
Stan James
(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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic