O'Reilly logo

Java 9 Concurrency Cookbook - Second Edition by Javier Fernández González

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

Follow these steps to implement the example:

  1. Create a class named MyWorkerThread that extends the ForkJoinWorkerThread class:
        public class MyWorkerThread extends ForkJoinWorkerThread {
  1. Declare and create a private ThreadLocal attribute parameterized by the Integer class named taskCounter:
        private final static ThreadLocal<Integer> taskCounter=                                         new ThreadLocal<Integer>();
  1. Implement a constructor of the class:
        protected MyWorkerThread(ForkJoinPool pool) {           super(pool);         }
  1. Override the onStart() method. Call the method on its parent class, print a message to the console, and set the value of the taskCounter attribute of this thread to zero:
        @Override         protected void onStart() {           super.onStart();  System.out.printf("MyWorkerThread ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required