This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Thread (maintain current thread)  RSS feed

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a pretty basic problem.

I'm working on a piece of middleware between two large applications that cannot be altered. My little function is the squishy bit in the middle

Application #1: A huge computational powerhouse that has an exposed jar with one function that I use (simulated here by the function runAlgorithm)

Application #2: A desktop UI that calls my middleware and expects a result in the same thread.

Here's some sample code


The problem:
So looking at the example code, main() is basically the UI calling my function. I cannot edit main(), I cannot do anything to main. It calls my function and expects a result

Also runAlgorithm() is the massive server behind the scenes. I cannot edit runAlgorithm, I cannot do anything other than expect a result.

Instead of chaining all my runAlgorithm()'s in a row, it is perfectly fine for me to thread them out in my performFunction() call. My question is what would be (given the sparse details above) the best way to proceed? Should I thread out all of my runAlgorithms() and sleep inside performFunction() until they all respond? Is there a better way?

Any help would be appreciated.

Thanks!
 
Sheriff
Posts: 23640
48
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to start ten separate threads to do the ten separate calls to runAlgorithm(), sure, go ahead. But you don't need to do any sleeping while you're waiting for the threads to complete. Just start the threads, then call each of their join() methods.
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this code - it uses ThreadPool and Future/Callable interfaces,
with this code you dont need to deal with synchronization of threads - it is done automatically for you,
and you can easy control the number of threads in ThreadPool (sometimes too many simultaneous threads is not a good idea).
 
Paul Clapham
Sheriff
Posts: 23640
48
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even better than my quick-and-dirty suggestion. One day I will have to learn about the Java 5 concurrency classes properly.

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!