aspose file tools*
The moose likes Threads and Synchronization and the fly likes Threads - stopping a while loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Threads - stopping a while loop" Watch "Threads - stopping a while loop" New topic
Author

Threads - stopping a while loop

Chris Flynn
Greenhorn

Joined: Jan 30, 2006
Posts: 7
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

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads - stopping a while loop
 
Similar Threads
I am also in elite group of SCJP-2.0
Inter Thread communication
join() and completely stopping a thread
FileChannel and thread safety
Removing entries from a collection