When does parallel processing interrupt sequential processing?

// parallel processing int processors = Runtime.getRuntime().availableProcessors(); ExecutorService executorService = Executors.newFixedThreadPool(threads); final List<String> albumIds2 = new ArrayList<String>(); long start2 = System.nanoTime(); for (final HColumn<String, String> column : result.get().getColumns()) { Runnable worker = new Runnable() { @Override public void run() { albumIds2.add(column.getName()); } }; executorService.execute(worker); } long timeTaken2 = System.nanoTime() - start2; 

I have code similar to the above example that creates a List<String> album identifiers. the column is a fragment from the cassandra database. I record time for the entire list of albums being created.

I did the same with the extended loop as shown below.

  QueryResult<ColumnSlice<String, String>> result = CassandraDAO.getRowColumns(AlbumIds_CF, customerId); long start = System.nanoTime(); for (HColumn<String, String> column : result.get().getColumns()) { albumIds.add(column.getName()); } long timeTaken = System.nanoTime() - start; 

I note that no matter how large the number of albums, a shorter time is always required for each cycle. Am I doing it wrong? or I need a computer with multiple cores. I am really new to the whole concept of parallel computing, please forgive me if my question is stupid.

+6
source share
2 answers

In the parallel example, you submit one task for each column. The overhead of completing a task is probably greater than the advantage of running a parallel. This is compounded by the fact that the "task" is really fast (insert one element into the array and return). Indeed, Executor adds every task received to the queue (and that adding is expensive). Then you add task N to the queue, and each task adds an element to the array. Parallel operation performs only the last part

If the task was more complicated, you could send the work into β€œpieces” (for example, if you have N elements and P processors, each piece will have N / P elements or N / P + 1 elements). This strategy helps reduce overhead.

Please note that ArrayList not synchronized, then the simultaneous execution of several tasks may damage your list. You can use a parallel collection to avoid this problem, but the first observation remains.

+6
source

This is bad practice, the time and processor used to create the threads is much larger than your thread: albumIds2.add (column.getName ());

-1
source

All Articles