File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

parallel processing and multi-core utilization with java

 
Mohamed Farouk
Ranch Hand
Posts: 249
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
parallel-processing-and-multi-core-utilization-with-java/ -

Parallel Processing

I am writing a low latency request to get the greates number of primitive array and trying to parallelize the serial operation as it will be called million times.

The author has explained using a loop load the following types:
1. Serial Service( Slowest(
2. ExecutorServiceTest - Parallel Attempt 1
3. CompletionExecutorServiceTest - Parallel Attempt 2
4. CallableExecutorServiceTest - Parallel Attempt 3
5. ForkJoinExecutorServiceTest - Parallel Attempt 4 (JDK7)


He has used a simple test where he creates the service and then performs a load on the task.
TEST --> TASK

My requirement is I need to create service which should manage a task
TEST --> Interface --> Implementation --> Task

For Type 4: Callable the result does not use futures but calls back the caller's callback to return the result from the task call method (has it has a reference to the caller)





My Implementation - Test , Interface , Implementaiton , Task(FindHighValueTask)
Now Question is:

In a case when my interface/implementaiton is calling the CallableTask instead of the test. how can I get the result in my implementation method as the results go the callback method

 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, you will have to make the CallBackTask call() method return the sequence instead of null:


Then you have to have your findHighValue method keep a reference to the Future, and call get() on it to get the results:
 
Mohamed Farouk
Ranch Hand
Posts: 249
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you have to have your findHighValue method keep a reference to the Future, and call get() on it to get the results:


Thanks very much for your reply

Call back has the result, Ok we have a reference to the Future and and how does future.get know the result from call back method ?

Thanks
 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Future has a lock it uses. When Future.get() is called, it wait()s on the lock, and when the processing is done, the Future is given the return value from the Callable.call() method and the wait()ing threads are notify()ed. So by the time get() returns it has waited for the call() to complete and has a reference to the result, which it returns to the caller.
 
Mohamed Farouk
Ranch Hand
Posts: 249
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Author Implements Call back to avoid blocking when we use future to get a result.



So question is so if we use the Call back way of implementation without using future is there no way of getting the result?

Thanks
 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Farouk wrote:
Author Implements Call back to avoid blocking when we use future to get a result.



So question is so if we use the Call back way of implementation without using future is there no way of getting the result?

Thanks


Can you get the result from the callback into the calling method? Yes, by implementing what I described Future as doing. Can you do it without blocking? No. The reason the task was parallelized was because it takes time. If it takes time you have to wait for the result. After all, if you could just get the results without waiting why would you go through the trouble of using the threads?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic