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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.