Consider the snippet:
If in a main thread, I have this inside a method-
volatile CountDownLatch latch = new CountDownLatch(3);
new Thread(new ProcessThread("Worker1",latch, 20000)).start();//20 secs
new Thread(new ProcessThread("Worker2",latch, 60000)).start();//60 secs
new Thread(new ProcessThread("Worker3",latch, 40000)).start();//40 secs
I see that volatile
is shown as an illegal modifier. And only final
is permitted. And final guarantees initialization safety.
public static class ProcessThread implements Runnable {
final CountDownLatch latch;
final long workDuration;
final String name;
public ProcessThread(String name, CountDownLatch latch, long duration){
this.name= name;
this.latch = latch;
this.workDuration = duration;
}
}
The object below i.e new CountDownLatch(3)
is properly constructed but I also want to make sure that the reference latch
to which the above object is assigned is guaranteed to be visible to the code below it.
final CountDownLatch latch = new CountDownLatch(3);
Does the above code guarantee initialization so that latch
is perfectly visible to the code below i.e
new Thread(new ProcessThread("Worker1",latch, 20000)).start();