Follow these steps to implement the example:
- Create a class named TaskAtomic and specify that it implements the Runnable interface:
public class TaskAtomic implements Runnable {
- Declare a private AtomicInteger attribute named number:
private final AtomicInteger number;
- Implement the constructor of the class to initialize its attributes:
public TaskAtomic () { this.number=new AtomicInteger(); }
- 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); } }
- Create a class named TaskLock and specify that it implements the Runnable interface: ...