Java · Java8

Recursive Action Task in Fork/Join Framework

Recursive Task is useful where the tasks are independent and caller is not expecting any return result from the task.

Note: make sure that you are waiting to complete the first task. i.e (action.isDone() check)

RecursiveTask

Here is the example which illustrates the Recursive Task

A Main program to start pool and first task,

/**
 * 
 */
package forkjoin;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ForkJoinPool;

/**
 * @author prabhu kvn
 *
 */
public class EvenNumberFinderMain_Action {

 public static void main(String args[]) {

 // Mock Data: crate an array of 100 numbers
 List a = new ArrayList();

 for (int i = 0; i < 100; i++) {
 a.add(i);
 }
 // Initialize the Thread Pool
 ForkJoinPool pool = new ForkJoinPool(12);
 EvenNumberFinderAction action = new EvenNumberFinderAction(a);
 pool.execute(action);

 do {
 /*System.out.println("****************Pool****************");
 System.out.println("Parallesim:" + pool.getParallelism());
 System.out.println("Pool size:" + pool.getPoolSize());
 System.out.println("Active Thread count:" + pool.getActiveThreadCount());
 System.out.println("Queed Submission count:" + pool.getQueuedSubmissionCount());
 System.out.println("Qued Task count:" + pool.getQueuedTaskCount());
 System.out.println("Running Thread count:" + pool.getRunningThreadCount());
 System.out.println("Seal count:" + pool.getStealCount());*/

 } while (!action.isDone());
 System.out.println("Main thread One");
 pool.shutdown();
 System.out.println("Main thread Two");

 }
}

And RecusriveAction Class implementation,

package forkjoin;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.RecursiveAction;
import java.util.stream.Collectors;

public class EvenNumberFinderAction extends RecursiveAction{

/**
*
*/
private static final long serialVersionUID = 1L;

List a=null;

public EvenNumberFinderAction() {
}

public EvenNumberFinderAction(List a) {
this.a=a;
}
@Override
protected void compute() {
// TODO Auto-generated method stub
List taskList = new ArrayList();
List subList=null;
List evenList = new ArrayList();
/*
* See if the list is greater than 10. if so divide the task.
*/
if(a.size()>10){
subList = a.subList(0, 10);
List remaining = a.subList(10, a.size());
EvenNumberFinderAction task = new EvenNumberFinderAction(remaining);
task.fork();
taskList.add(task);
}else{
subList=a;
}

if(subList!=null){
evenList=subList.parallelStream().filter(element -> {return element%2==0;}).collect(Collectors.toList());
}
System.out.println(“Sub Result:”+evenList);

}
}

Output:
Sub Result:[40, 42, 44, 46, 48]
Sub Result:[90, 92, 94, 96, 98]
Sub Result:[10, 12, 14, 16, 18]
Sub Result:[50, 52, 54, 56, 58]
Sub Result:[30, 32, 34, 36, 38]
Sub Result:[60, 62, 64, 66, 68]
Sub Result:[20, 22, 24, 26, 28]
Sub Result:[70, 72, 74, 76, 78]
Sub Result:[0, 2, 4, 6, 8]
Main thread One
Main thread Two
Sub Result:[80, 82, 84, 86, 88]

Sequence of above task,

forkjoinwithaction

Java · Java8

File Reading Using Java8 Streams

Here we can use Java8 streams to read a file content.

/**
 * 
 */
package java8pract.streams;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author prabhu kvn
 *
 */
public class StreamsInIO {

 /**
 * Using Java8 Streams to read the file
 */
 public StreamsInIO() {
 // TODO Auto-generated constructor stub
 }

 /**
 * @param args
 */
 public static void main(String[] args) {

 try {
 FileReader freader = new FileReader(new File("d:/text1.txt"));
 BufferedReader bReader = new BufferedReader(freader);
 Stream fileStream = bReader.lines();
 List fileContent = fileStream.collect(Collectors.toList());
 System.out.println(fileContent.size());
 System.out.println(fileContent);

 } catch (FileNotFoundException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }

 }

}