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 TaskAtomic and specify that it implements the Runnable interface:
        public class TaskAtomic implements Runnable {
  1. Declare a private AtomicInteger attribute named number:
        private final AtomicInteger number;
  1. Implement the constructor of the class to initialize its attributes:
        public TaskAtomic () {           this.number=new AtomicInteger();         }
  1. Implement the run() method. In a loop with 1,000,000 steps, assign the number of steps to the atomic attribute as a value, using the set() method:
        @Override         public void run() {           for (int i=0; i<1000000; i++) {             number.set(i);           }         }
  1. Create a class named TaskLock and specify that it implements the Runnable interface: ...

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