File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes parallel processing and multi-core utilization with java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "parallel processing and multi-core utilization with java" Watch "parallel processing and multi-core utilization with java" New topic
Author

parallel processing and multi-core utilization with java

Mohamed Farouk
Ranch Hand

Joined: Jun 08, 2005
Posts: 247
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


SCJP, SCWCD, SCBCD, SCEA 5
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3934
    
  17

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:


Steve
Mohamed Farouk
Ranch Hand

Joined: Jun 08, 2005
Posts: 247
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

Joined: Jan 28, 2003
Posts: 3934
    
  17

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

Joined: Jun 08, 2005
Posts: 247
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

Joined: Jan 28, 2003
Posts: 3934
    
  17

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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: parallel processing and multi-core utilization with java
 
Similar Threads
Developing SOAP Web Service.
astar search
constructors and calling methods
Save is not performing in Spring Mvc 3Hibernate
Adding objects to an array from an implemented class