aspose file tools
The moose likes Threads and Synchronization and the fly likes kicking off independent task threads in parallel Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "kicking off independent task threads in parallel" Watch "kicking off independent task threads in parallel" New topic
Author

kicking off independent task threads in parallel

Rick Nelly
Greenhorn

Joined: Mar 01, 2011
Posts: 3
Hello,
I am newbie to multi-threading, I have a use case where I need to do 2 search tasks, so created ThreadA which is first priority and THreadB which is second priority. The goal is Thread B needs to start after ThreadA is complete and application execution should not stop for ThreadB. Later client will polll server to get ThreadB results whenever its completed. I created the following snippet, but both the threads seem to be starting one after the other and the application execution is further delayed as 2 threads are now executing. I am still getting familiar with Executors, ThreadPools and concurrent threading and so on..please advice the best approach and examples or good reference..
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3047
    
    1

Welcome to CodeRanch, Rick!

First of all, when you post your code here, you may want to UseCodeTags. It will make it a lot easier to read your code, and people will be more likely to help you out.

Rick Nelly wrote:The goal is Thread B needs to start after ThreadA is complete and application execution should not stop for ThreadB.


Alright, why do you use two different threads for this? Just make two search tasks, stick them in a queue, and let one thread deal with both of them, while the rest of your program continues to do what you want it to do.
Rick Nelly
Greenhorn

Joined: Mar 01, 2011
Posts: 3
Thanks Stephan, added the code tags for better formatting.

The only reason, I decided 2 different threads, coz, second task is independent of first task/search and main application is only dependent on completion on first task. The application after a certain wait will poll back to see if task2 is done to get results from it. Couldnt think of how this can be done in single thread, without interrupting the main thread/execution/application.

If possible, could you post a sample code for reference to using the queue approach for this usecase. Thank You.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3047
    
    1

The problem is that you're calling the run() method on t1. This will not only prevent t2 from running until after t1 is finished, but the rest of your program as well!

Take a look at what the class java.util.concurrent.PriorityBlockingQueue can do for you. You can add your searches to this queue, and start one thread and have it take the searches off this queue and handle them. Meanwhile the rest of your program can continue execution, and even poll the queue to determine how far your searching thread has progressed.
Rick Nelly
Greenhorn

Joined: Mar 01, 2011
Posts: 3
Thanks Stephan, will certainly follow up further, Future Callable Tasks and PriorityQueue..both my tasks have some return data, so just figured, I will need callable instead of runnable as well.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3047
    
    1

 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: kicking off independent task threads in parallel
 
Similar Threads
Threads and Synchronization examples
Thread Priority issue
Thread Priority
Insights into synchronization