DulcetAirman icon

JMM volatile field demo

DulcetAirman | PRO | 07/23/20 12:09:22 PM UTC | 0 ⭐ | 1426 👁️ | Never ⏰ | []
Java |

1.29 KB

|

None

|

0 👍

/

0 👎

package ch.claude_martin.foo;
 
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ForkJoinPool;
 
public class MyClass {
 
    static class Car {
        private String owner = "N/A";
 
        public String getOwner() {
            return this.owner;
        }
 
        public void setOwner(String o) {
            this.owner = o;
        }
    }
 
    volatile Car car = null;
 
    public static void main(String args[]) throws InterruptedException {
        new MyClass().go();
    }
 
    void go() throws InterruptedException {
 
        ExecutorService es = ForkJoinPool.commonPool();
        Runnable taskA = () -> {
            car = null;
        };
 
        Runnable taskB = () -> {
            car = new Car();
            // we call yield() because it would be rather unlikely that the thread is
            // interrupted here. You can remove it, but it will take longer.
            Thread.yield();
            car.setOwner("Jane");
        };
 
        Runnable taskC = () -> {
            final Car copy = car; // taskA might set this to null at any time
            if (copy != null) {
                final String owner = copy.getOwner();
                if ("N/A".equals(owner)) {
                    System.err.println(owner);
                    System.exit(0);// Stop the VM because we got to read the initial value
                } 
                // else System.out.println(owner);
            }
        };
 
        while (true) {
            es.submit(taskA);
            es.submit(taskB);
            es.submit(taskC);
        }
    }
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎