• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

kicking off independent task threads in parallel

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
reply
    Bookmark Topic Watch Topic
  • New Topic