This week's giveaways are in the MongoDB and Jobs Discussion forums.
We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes Try to stop execution of multiple threads if found a result from one of the thread. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Try to stop execution of multiple threads if found a result from one of the thread." Watch "Try to stop execution of multiple threads if found a result from one of the thread." New topic
Author

Try to stop execution of multiple threads if found a result from one of the thread.

Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 223
I have a class below, where I try to run several process simultaneously from a thread.

If one of the thread found a result, I want to stop all the processes/ threads and return some results to the caller (in this case the main() method). Is it possible to do this?

Maybe it's something trivial that I miss.

Thanks in advance for all the help.


Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 223
Any thoughts?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19214

http://faq.javaranch.com/java/PatienceIsAVirtue comes to mind.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Jeff Storey
Ranch Hand

Joined: Apr 07, 2007
Posts: 230
Susan,

A couple of things come to mind here. I think you need you clarify a little bit about what it is you're trying to do. In the run method, you said you want to return some values to the main method. Remember that the run method can't actually return any values. As for stopping all threads once a result is found, you can only stop a thread if it has a flag that it is periodically checking (since Thread.stop is inherently unsafe and deprecated).

When you're starting a bunch of threads like you are doing in your example, it is typically a good idea to use a thread Executor service. This allows an available pool of threads to complete the tasks you want to run rather than starting a new thread for each one (saves on the cost of starting new threads -- all threads aren't going to be running at once, so you might as well save resources). Another major benefit to using an executor service is that you can in fact cancel any tasks that haven't started yet. And you can also submit Callable tasks which can return a value.

The basic algorithm for your problem might be something like:

create an executor service
continue to read each line from the csv file as you do now, but instead of starting a new thread, submit a Callable that is similar to your run method but returns field1 and field2 and you could have another thread that is checking which callables are complete and evaluating the results. you could also use a Runnable here and the runnable itself could check if field1 != 0 and field2 != 0 and notify listeners (or invoke another method or something else) when these conditions hold true. this second method of using a runnable might actually be preferable so you don't need to have a thread that is polling the callables to see if they are finished. depending on your requirements you could choose between a polling vs notification system.
once a result is found, cancel all tasks that are currently queued in the executor. any currently executing tasks will finish but you could ignore their results (or if it's really important to stop immediately and exit the application, they could always be daemon threads, but it seems like the processing is not very intensive, so finishing remaining tasks should be negligible).

Hope this helps,
Jeff
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Try to stop execution of multiple threads if found a result from one of the thread.
 
Similar Threads
Thread - Wait() and Notify();
parsing data and storing in the xml
Overhead IO : The cryptic case of the extra bytes
Parsing a text file
Data loss converting StringBuilder to byte[]?