Java Thread completionservice Batch processing task

Source: Internet
Author: User

What if you submitted a batch task to executor and want to get the results after they are done?

To do this, you can save the future associated with each task, and then constantly invoke a get with a timeout of zero to verify that the future is complete. This can be done, but it is rather tedious. Fortunately, there is a better way: to complete the service (completion services).

Completionservice integrates the functions of executor and blockingqueue.

You can submit the callable task to it for execution, and then use the take and poll methods that are similar to the queue to get this result when the results are fully available, like a packaged future. Executorcompletionservice is a class that implements the Completionservice interface and delegates the compute task to a executor.

The implementation of Executorcompletionservice is quite straightforward. It creates a blockingqueue in the constructor and uses it to keep the finished result. The Done method in Futuretask is called when the calculation is complete.

When a task is submitted, the task is first packaged as a queueingfuture, which is a subclass of Futuretask, then overwrite the done method, placing the result in Blockingqueue, and the take and poll methods delegated to the Blockingqueue, it blocks when the results are not available.

See Demo directly:

Package Javademo;

Import Java.util.Random;

Import Java.util.concurrent.BlockingQueue;

Import java.util.concurrent.Callable;

Import Java.util.concurrent.CompletionService;

Import java.util.concurrent.ExecutionException;

Import Java.util.concurrent.ExecutorCompletionService;

Import Java.util.concurrent.ExecutorService;

Import java.util.concurrent.Executors;

Import Java.util.concurrent.Future;

Import Java.util.concurrent.LinkedBlockingQueue;

/***

* Two clock way out thread run result

* @author Think

*

*/

public class Completionservicetest {

public static void Main (string[] args) throws Exception {

Completionservicetest CST = new Completionservicetest ();

Cst.count1 ();

Cst.count2 ();

}

/***

* Use the blocking container to save the results of each executor processing, and then perform the uniform processing later

* @throws Exception

*/

public void Count1 () throws exception{

Executorservice exec = Executors.newcachedthreadpool ();

blockingqueue<future<integer>> queue = new linkedblockingqueue<future<integer>> ();

for (int i=0; i<10; i++) {

Future<integer> Future =exec.submit (Gettask ());

Queue.add (future);

}

int sum = 0;

int queuesize = Queue.size ();

for (int i=0; i<queuesize; i++) {

Sum + = Queue.take (). get ();

}

System.out.println ("total:" +sum);

Exec.shutdown ();

}

/***

* Use Completionservice (complete service) to maintain the results of executor processing

* @throws interruptedexception

* @throws executionexception

*/

public void Count2 () throws Interruptedexception, executionexception{

Executorservice exec = Executors.newcachedthreadpool ();

completionservice<integer> Execcomp = new executorcompletionservice<integer> (exec);

for (int i=0; i<10; i++) {

Execcomp.submit (Gettask ());

}

int sum = 0;

for (int i=0; i<10; i++) {

Retrieves and removes the future that represents the next completed task, and waits if no such task exists at this time.

future<integer> future = Execcomp.take ();

Sum + = Future.get ();

}

System.out.println ("total:" +sum);

Exec.shutdown ();

}

/**

* Get a job

* @return Callable

*/

Public callable<integer> Gettask () {

Final random rand = new Random ();

callable<integer> task = new callable<integer> () {

@Override

Public Integer Call () throws Exception {

int i = Rand.nextint (10);

Int J = Rand.nextint (10);

int sum = I*J;

System.out.print (sum+ "\ t");

return sum;

}

};

return task;

}

}

  1. import java.util.Random;
  2. import java.util.concurrent.BlockingQueue;
  3. import java.util.concurrent.Callable;
  4. import Java.util.concurrent.CompletionService;
  5. import java.util.concurrent.ExecutionException;
  6. import Java.util.concurrent.ExecutorCompletionService;
  7. import Java.util.concurrent.ExecutorService;
  8. import java.util.concurrent.Executors;
  9. import java.util.concurrent.Future;
  10. import java.util.concurrent.LinkedBlockingQueue;
  11. Public class Test17 {
  12. Public Static void Main (string[] args) throws Exception {
  13. Test17 t = new Test17 ();
  14. T.count1 ();
  15. T.count2 ();
  16. }
  17. //Use a blocking container to save the results of each executor process, and then perform a unified process later
  18. Public void count1 () throws exception{
  19. Executorservice exec = Executors.newcachedthreadpool ();
  20. blockingqueue<future<integer>> queue = new linkedblockingqueue<future<integer>> () ;
  21. for (int i=0; i<; i++) {
  22. Future<integer> Future =exec.submit (Gettask ());
  23. Queue.add (future);
  24. }
  25. int sum = 0;
  26. int queuesize = queue.size ();
  27. for (int i=0; i<queuesize; i++) {
  28. Sum + = Queue.take (). get ();
  29. }
  30. System.out.println ("total:"+sum);
  31. Exec.shutdown ();
  32. }
  33. //Use Completionservice (complete service) to maintain the results of executor processing
  34. Public void Count2 () throws Interruptedexception, executionexception{
  35. Executorservice exec = Executors.newcachedthreadpool ();
  36. completionservice<integer> Execcomp = new executorcompletionservice<integer> (exec);
  37. for (int i=0; i<; i++) {
  38. Execcomp.submit (Gettask ());
  39. }
  40. int sum = 0;
  41. for (int i=0; i<; i++) {
  42. //Retrieve and remove the future that represents the next completed task, and wait if no such task exists at this time.
  43. future<integer> future = Execcomp.take ();
  44. Sum + = Future.get ();
  45. }
  46. System.out.println ("total:"+sum);
  47. Exec.shutdown ();
  48. }
  49. //Get a task
  50. Public Callable<integer> Gettask () {
  51. Final Random rand = new random ();
  52. callable<integer> task = new callable<integer> () {
  53. @Override
  54. Public Integer call () throws Exception {
  55. int i = rand.nextint (ten);
  56. int j = rand.nextint (ten);
  57. int sum = i*j;
  58. System.out.print (sum+"\ t");
  59. return sum;
  60. }
  61. };
  62. return task;
  63. }
  64. /**
  65. * Execution Result:
  66. 6 6 14 40 40 0 4 7 0 0 Total: 106
  67. 12 6 12 54 81 18 14 35 45 35 total: 312
  68.      */
  69. }

Java Thread completionservice Batch processing task

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.