非常好的代码,清楚解释了如何使用多线程执行任务,然后汇总

 try {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < parallelism; i++) {
          decodeOps.acquire(1);
          int start = i * partSize;
          int count = Math.min(bufSize - start, partSize);
          parallelDecoder.execute(new DecodeOp(
            readBufs, writeBufs, start, count,
            erasedLocations, reedSolomonCode[i]));
        }
        decodeOps.acquire(parallelism);
        decodeOps.release(parallelism);
        decodeTime += (System.currentTimeMillis() - startTime);
      } catch (InterruptedException e) {
        throw new IOException("Interrupted while waiting for read result");
      }

假设parallelism=5,相当于启动5个进程处理事务,当五个进程都执行完毕后,再进行的汇总。

关键技巧再次。

decodeOps.acquire(parallelism);
decodeOps.release(parallelism);

你可能感兴趣的:(非常好的代码,清楚解释了如何使用多线程执行任务,然后汇总)