| | 96 | } |
| | 97 | |
| | 98 | }}} |
| | 99 | |
| | 100 | === Test case II - using !ExecutorService === |
| | 101 | |
| | 102 | !GridGain's distributed executor service operates on the Callable/Runnable interfaces. The main work was done in the '''Agent''' class, which implements Callable interface: |
| | 103 | |
| | 104 | {{{ |
| | 105 | #!java |
| | 106 | |
| | 107 | public class Agent implements Callable<FastBigInt128>, Serializable { |
| | 108 | private static final long serialVersionUID = 1L; |
| | 109 | private String[] imageDesc; |
| | 110 | private int z; |
| | 111 | |
| | 112 | public Agent(String[] imageDesc, int z) { |
| | 113 | this.imageDesc = imageDesc; |
| | 114 | this.z = z; |
| | 115 | } |
| | 116 | |
| | 117 | public FastBigInt128 call() { |
| | 118 | Worker cmbfWorker = new Worker(); |
| | 119 | FastBigInt128 result = cmbfWorker.countInImage(imageDesc); |
| | 120 | System.out.println("\tResult from task #" + z + ", " + "\tvalue: " + result); |
| | 121 | return result; |
| | 122 | } |
| | 123 | } |
| | 124 | |
| | 125 | }}} |
| | 126 | |
| | 127 | In order to perform computations, we had to divide problem into small tasks and submit them into executor service: |
| | 128 | |
| | 129 | {{{ |
| | 130 | #!java |
| | 131 | |
| | 132 | for (int i = 1; i <= n; i++) { |
| | 133 | for (final String[] imageDesc : cmbfWorker.generateImages(i, level)) { |
| | 134 | Future<FastBigInt128> future = executorService.submit(new Agent(imageDesc, ++count)); |
| | 135 | tasks.add(future); |
| | 136 | } |
| | 137 | } |
| | 138 | |
| | 139 | }}} |
| | 140 | |
| | 141 | After that we had to gather results: |
| | 142 | |
| | 143 | {{{ |
| | 144 | #!java |
| | 145 | |
| | 146 | for (Future<FastBigInt128> w : tasks) { |
| | 147 | results.add(w.get()); |