We want to connect the people who have knowledge to the people who need it, to bring together people with different perspectives so they can understand each other better, and to empower everyone to share their knowledge.
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)
Input your email to receive reset password link, or if you remember your password, you can click