StackAsk is a community where developers ask questions, share answers and learn from each others real-world experience, so every solved problem helps the next person who runs into it.
public class VolatileExample { private volatile boolean active; public void prepare() throws InterruptedException { new Thread(() -> { System.out.println("application preparing ..."); sleep(3); active = true; }) .start(); } public void start() throws Exception { new Thread(() -> { while(!active); System.out.println("application started"); }) .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 { VolatileExample example = new VolatileExample(); example.prepare(); example.start(); sleep(10); } }
volatile
System.out.println("application started");
public class VolatileIntExample { private volatile int count; private final Map map = new ConcurrentHashMap(); public void start() { Thread[] threads = new Thread[10]; for (int i = 0 ; i < threads.length ; ++i) { threads[i] = new Thread(() -> while(count <= 1000000) { ++ count; map.put(Integer.valueOf(count), Integer.valueOf(count)); } }); } for (int i = 0 ; i < threads.length ; ++i) { threads[i].start(); } } public static void main(String[] args) throws Exception { VolatileIntExample example = new VolatileIntExample(); example.start(); Thread.sleep(3000); System.out.println(example.map.size()); } }
979638
public class AtomicExample { private AtomicBoolean active = new AtomicBoolean(false); public void prepare() throws InterruptedException { new Thread(() -> { System.out.println("application preparing ..."); sleep(3); active.set(true); }) .start(); } public void start() throws Exception { new Thread(() -> { while(!active.get()); System.out.println("application started"); }) .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 { AtomicExample example = new AtomicExample(); example.prepare(); example.start(); sleep(10); } }
public class AtomicIntExample { private AtomicInteger count = new AtomicInteger(0); private final Map map = new ConcurrentHashMap(); public void start() { Thread[] threads = new Thread[10]; for (int i = 0 ; i < threads.length ; ++i) { threads[i] = new Thread(() -> { int value; while((value = count.incrementAndGet()) <= 1000000) { map.put(Integer.valueOf(value), Integer.valueOf(value)); } }); } for (int i = 0 ; i < threads.length ; ++i) { threads[i].start(); } } public static void main(String[] args) throws Exception { AtomicIntExample example = new AtomicIntExample(); example.start(); Thread.sleep(3000); System.out.println(example.map.size()); } }
1000000
2.2K Point(s)
1.2K Point(s)
313 Point(s)
178 Point(s)
138 Point(s)
Your account is signed in on the device(s) below. Sign out of at least one of them to continue signing in on this device.
Your account is signed in on the device(s) below. Sign out directly on one of those devices, or wait until the sign-in again time shown below.
If you cant access those devices, please contact the administrator to sign them out for you.
Input your email to receive reset password link, or if you remember your password, you can click