Created
August 12, 2021 14:50
-
-
Save tvd12/97f1bb633a3305e08668ad67576f80f0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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