How to do it...

Follow these steps to implement the example:

  1. First, implement a class that will simulate a text file. Create a class named FileMock with two attributes: a String array named content and int named index. They will store the content of the file and the line of the simulated file that will be retrieved:
        public class FileMock {            private String[] content;           private int index;
  1. Implement the constructor of the class that initializes the content of the file with random characters:
        public FileMock(int size, int length){           content = new String[size];           for (int i = 0; i< size; i++){             StringBuilder buffer = new StringBuilder(length);             for (int j = 0; j < length; j++){               int randomCharacter= (int)Math.random()*255;  buffer.append((char)randomCharacter); ...

Get Java 9 Concurrency Cookbook - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.