Skip to content

Instantly share code, notes, and snippets.

@tvd12
Created August 12, 2021 14:50
Show Gist options
  • Select an option

  • Save tvd12/97f1bb633a3305e08668ad67576f80f0 to your computer and use it in GitHub Desktop.

Select an option

Save tvd12/97f1bb633a3305e08668ad67576f80f0 to your computer and use it in GitHub Desktop.
package com.tvd12.example.concurrent.sync;
public class VolatileBooleanExample {
private volatile boolean active;
private static int number;
public void prepare() throws InterruptedException {
new Thread(() -> {
System.out.println("application preparing ...");
number ++;
sleep(3);
active = true;
})
.start();
}
public void start() throws Exception {
new Thread(() -> {
while(!active);
System.out.println("application started, number = " + number);
})
.start();
}
private static void sleep(int second) {
try {
Thread.sleep(second * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
VolatileBooleanExample example = new VolatileBooleanExample();
example.prepare();
example.start();
sleep(10);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment