博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java Atomic代码
阅读量:6948 次
发布时间:2019-06-27

本文共 3079 字,大约阅读时间需要 10 分钟。

hot3.png

package javaBasic;import java.util.concurrent.atomic.AtomicBoolean;import java.util.concurrent.atomic.AtomicInteger;public class JavaAmoticTest {    static abstract class CalculatorBase {        // Creates a new {@code AtomicBoolean} with initial value {@code false}.        AtomicBoolean calledStart = new AtomicBoolean();        AtomicBoolean calledDispose = new AtomicBoolean();        protected void StartCore() {            boolean called = calledStart.getAndSet(true);            // if Called is False, Keeping Running            // if not match, throw java.lang.AssertionError            // and output error message.            assert !called;            try {                Thread.sleep(1000);                System.out.println("Start Finished");            } catch (InterruptedException e) {            }        }        protected void DisposeCore() {            boolean called = calledDispose.getAndSet(true);            assert !called;            try {                Thread.sleep(1000);                System.out.println("Dispose Finished");            } catch (InterruptedException e) {            }        }        public abstract void Start();        public abstract void Dispose();    }    static class CasCalculator extends CalculatorBase {        // Creates a new AtomicInteger with the given initial value.        AtomicInteger state = new AtomicInteger(kNotStarted);        static final int kNotStarted = 0, kStarting = 1, kStarted = 2,                kDisposed = 3;        @Override        public void Start() {            // Atomically sets the value to the given updated value            // if the current value {@code ==} the expected value.            boolean succeed = state.compareAndSet(kNotStarted, kStarting);            if (succeed) {                synchronized (this) {                    StartCore();                }                boolean cont = state.compareAndSet(kStarting, kStarted);                if (!cont) {                    synchronized (this) {                        DisposeCore();                    }                }            }        }        @Override        public void Dispose() {            // Atomically sets to the given value and returns the old value.            int orig = state.getAndSet(kDisposed);            if (orig == kStarted) {                synchronized (this) {                    DisposeCore();                }            }        }        public static void main(String[] args) throws InterruptedException {            final CasCalculator cal = new CasCalculator();            new Thread() {                @Override                public void run() {                    cal.Start();                }            }.start();            new Thread() {                @Override                public void run() {                    cal.Start();                }            }.start();            new Thread() {                @Override                public void run() {                    cal.Start();                }            }.start();        }    }}

转载于:https://my.oschina.net/u/1412027/blog/196507

你可能感兴趣的文章
cocos2d::Map
查看>>
【Ajax技术】利用XHR接受与处理XML数据
查看>>
学Hadoop还是Spark好?
查看>>
微服务生命周期的9个任务事项
查看>>
实战Kafka ACL机制
查看>>
云监控服务使用教程
查看>>
“旧城改造”的背后——银泰新零售阿里云解决方案(上)
查看>>
java B2B2C源码电子商务平台 -SpringCloud服务相互调用RestTemplate
查看>>
java B2B2C Springcloud电子商务平台源码-zuul 过滤器机制
查看>>
分布式消息系统:Kafka
查看>>
让phpmailer支持中文名称的附件和邮件标题中文乱码(转)
查看>>
JavaScript强化教程——JavaScript Math(算数) 对象
查看>>
CentOS7部署Kubernetes集群
查看>>
struts2中使用DMI(动态调用方法)方式配置action
查看>>
使用hyperpacer实现AWR报告的同步收集
查看>>
关于os.popen你可能不知道的
查看>>
redhat5.3恢复***
查看>>
linux 下mysql的乱码问题
查看>>
mongodb删除重复数据
查看>>
项目中常用的第三方框架总结
查看>>