当前位置: 首页 > news >正文

大冶市建设部门网站域名注册商怎么查

大冶市建设部门网站,域名注册商怎么查,房产中介在线咨询,互联网技术与应用一、CAS#xff08;Compare And Swap#xff09; 1、CAS介绍 CAS原理#xff1a;假设有三个值#xff0c;E#xff08;旧值#xff09;、U#xff08;需要更新的值#xff09;、V#xff08;内存中真实的值#xff09;#xff0c;具体参照下图#xff1a; 作用Compare And Swap 1、CAS介绍 CAS原理假设有三个值E旧值、U需要更新的值、V内存中真实的值具体参照下图 作用解决线程轻微竞争场景同一时间只有一个线程能进入CAS代码块中其它线程空转循环 compareAndSwapInt()方法对不同系统CAS指令的包装Intel的汇编指令cmpxchg不同厂家所实现的具体算法不一样 2、举例 public class UnsafeFactory {/*** 通过反射获取Unsafe属性* return*/public static Unsafe getUnsafe() {Field theUnsafe;try {theUnsafe Unsafe.class.getDeclaredField(theUnsafe);theUnsafe.setAccessible(true);// 因为theUnsafe是静态属性 所以field.get(Object)参数传什么都可以return (Unsafe) theUnsafe.get(null);} catch (Exception e) {e.printStackTrace();}return null;}/*** 找到指定类属性在内存中偏移的地址* param clazz* param fieldName* return*/public static long getFieldOffset(Class clazz, String fieldName) {try {return getUnsafe().objectFieldOffset(clazz.getDeclaredField(fieldName));} catch (Exception e) {throw new Error(e);}} }public class CASTest {public static void main(String[] args) {Entity entity new Entity();Unsafe unsafe UnsafeFactory.getUnsafe();long fieldOffset UnsafeFactory.getFieldOffset(Entity.class, x);System.out.println(fieldOffset);System.out.println(unsafe.compareAndSwapInt(entity, fieldOffset, 0, 1));System.out.println(unsafe.compareAndSwapInt(entity, fieldOffset, 1, 2));// 这个时候内存中的值已经改成2所以0改成3是不能改成功的System.out.println(unsafe.compareAndSwapInt(entity, fieldOffset, 0, 3));} }class Entity {// markword占8字节 klasspointer默认开启指针压缩占4字节 x属性的偏移量就是12int x; } 打印结果 12 true true false  3、存在问题 1激烈竞争线程空转导致性能下降 如果存在大量线程竞争一个变量必然导致其它线程资源或者长期CAS失败的线程都会给CPU调度产生性能问题 2ABA问题 可以加一个版本号区分究竟做了多少次版本的修改 3只能对一个值做原子操作 当对一个共享变量执行操作时我们可以使用循环CAS的方式来保证原子操作但对多个共享变量操作时循环CAS无法保证操作的原子性这个时候可以用锁 二、Atomic原子操作类 1、使用 在java.util.concurrent.atomic包里提供了一组原子操作类 基本类型AtomicInteger、AtomicLong、AtomicBoolean引用类型AtomicReference、AtomicStampedRerence、AtomicMarkableReference数组类型AtomicIntegerArray、AtomicLongArray、AtomicReferenceArray对象属性原子修改器AtomicIntegerFieldUpdater、AtomicLongFieldUpdater、 AtomicReferenceFieldUpdater原子类型累加器jdk1.8增加的类DoubleAccumulator、DoubleAdder、 LongAccumulator、LongAdder、Striped64 1原子基本类型 public class AtomicIntegerTest {static AtomicInteger atomicInteger new AtomicInteger();public static void main(String[] args) throws InterruptedException {for (int i 0; i 10; i) {Thread thread new Thread() {Overridepublic void run() {for (int j 0; j 10000; j) {atomicInteger.incrementAndGet();}}};thread.start();thread.join();}System.out.println(atomicInteger.get());} } 打印结果100000  incrementAndGet()方法就是通过CAS循环读取AtomicInteger类的value属性在内存中的值直到加1成功跳出while循环返回旧值 2原子更新数组类型 public class AtomicIntegerArrayTest {static int[] array {10, 21, 9, 32, 99};static AtomicIntegerArray atomicIntegerArray new AtomicIntegerArray(array);public static void main(String[] args) {// 设置下标为0的元素为100 // atomicIntegerArray.set(0, 100);int andSet atomicIntegerArray.getAndSet(0, 100);// 这里返回原值 实际值是100System.out.println(andSet);int i atomicIntegerArray.get(0);System.out.println(i);int andAdd atomicIntegerArray.getAndAdd(1, 9);// 这里也是一样 返回的是原值 实际是加9之后的值System.out.println(andAdd);int j atomicIntegerArray.get(1);System.out.println(j);} } 打印结果 10 100 21 30  3原子更新引用类型 public class AtomicReferenceTest {public static void main(String[] args) {User user1 new User(张三, 11);User user2 new User(李四, 18);User user3 new User(王五, 15);AtomicReferenceUser atomicReference new AtomicReferenceUser();atomicReference.set(user1);System.out.println(atomicReference.get());// 都是和AtomicInteger一样 先比较user1然后设置user2atomicReference.compareAndSet(user1, user2);System.out.println(atomicReference.get());atomicReference.compareAndSet(user1, user3);System.out.println(atomicReference.get());} } Data AllArgsConstructor // 上面两个注解需要lombok插件 class User {private String name;private Integer age;} 打印结果 User(name张三, age11) User(name李四, age18) User(name李四, age18)  4对象属性原子修改器 public class AtomicIntegerFieldUpdaterTest {public static class Candidate {volatile int score 0;AtomicInteger salary new AtomicInteger();}public static final AtomicIntegerFieldUpdaterCandidate aifu AtomicIntegerFieldUpdater.newUpdater(Candidate.class, score);public static AtomicInteger realScore new AtomicInteger(0);public static void main(String[] args) throws InterruptedException {final Candidate candidate new Candidate();Thread[] threads new Thread[10000];for (int i 0; i 10000; i) {threads[i] new Thread(() - {if (Math.random() 0.4) {candidate.salary.incrementAndGet();aifu.incrementAndGet(candidate);realScore.incrementAndGet();}});threads[i].start();}for (int i 0; i 10000; i) {threads[i].join();}System.out.println(AtomicIntegerFieldUpdater Scorecandidate.score);System.out.println(AtomicInteger salarycandidate.salary.get());System.out.println(realScorerealScore.get());} } 打印结果 AtomicIntegerFieldUpdater Score6057 AtomicInteger salary6057 realScore6057  对于AtomicIntegerFieldUpdater 的使用稍微有一些限制和约束约束如下 字段必须是volatile类型的在线程之间共享变量时保证立即可见.eg:volatile int value 3字段的描述类型修饰符public/protected/default/private与调用者与操作对象字段的 关系一致。也就是说调用者能够直接操作对象字段那么就可以反射进行原子操作。但是对于父 类的字段子类是不能直接操作的尽管子类可以访问父类的字段。只能是实例变量不能是类变量也就是说不能加static关键字。只能是可修改变量不能使final变量因为final的语义就是不可修改。实际上final的语义和 volatile是有冲突的这两个关键字不能同时存在。对于AtomicIntegerFieldUpdater和AtomicLongFieldUpdater只能修改int/long类型的字 段不能修改其包装类型Integer/Long。如果要修改包装类型就需要使用AtomicReferenceFieldUpdater。 2、LongAdder/DoubleAdder详解 解决高并发环境下AtomicInteger AtomicLong的自旋瓶颈问题引入了LongAdderLongAdder类是继承Striped64类的 1使用 /*** LongAdder为了解决高并发情况下自旋瓶颈* 原来的是单个值CASLongAdder是先根据base进行CASCAS失败再根据CPU线程数创建Cell数组每个线程操作的是Cell数组的Cell对象value值进行累加最后进行汇总* 这样就相当于开设了多个共享变量进行CAS操作* author gaopu* Time 2023年5月19日 下午3:39:30*/ public class LongAdderTest {public static void main(String[] args) {// 10个线程 每个线程都自增10000次testAtomicLongVSLongAdder(10, 10000);testAtomicLongVSLongAdder(10, 200000);testAtomicLongVSLongAdder(100, 200000);}static void testAtomicLongVSLongAdder(final int threadCount, final int times) {try {long start System.currentTimeMillis();testLongAdder(threadCount, times);long end System.currentTimeMillis() - start;System.out.println(线程数threadCount单线程操作自增次数timesLongAdder总自增次数(threadCount*times)总耗时:end);long start2 System.currentTimeMillis();testAtomicLong(threadCount, times);long end2 System.currentTimeMillis() - start2;System.out.println(线程数threadCount单线程操作自增次数timesAtomicLong总自增次数(threadCount*times)总耗时:end2);} catch (InterruptedException e) {e.printStackTrace();}}static void testAtomicLong(final int threadCount, final int times) throws InterruptedException {CountDownLatch countDownLatch new CountDownLatch(threadCount);AtomicLong atomicLong new AtomicLong();for (int i 0; i threadCount; i) {new Thread(() - {for (int j 0; j times; j) {atomicLong.incrementAndGet();}countDownLatch.countDown();}, threadi).start();}countDownLatch.await();}static void testLongAdder(final int threadCount, final int times) throws InterruptedException {CountDownLatch countDownLatch new CountDownLatch(threadCount);LongAdder longAdder new LongAdder();for (int i 0; i threadCount; i) {new Thread(() - {for (int j 0; j times; j) {longAdder.add(1);}countDownLatch.countDown();}).start();}countDownLatch.await();} } 打印结果 线程数10单线程操作自增次数10000LongAdder总自增次数100000总耗时:59 线程数10单线程操作自增次数10000AtomicLong总自增次数100000总耗时:4 线程数10单线程操作自增次数200000LongAdder总自增次数2000000总耗时:14 线程数10单线程操作自增次数200000AtomicLong总自增次数2000000总耗时:40 线程数100单线程操作自增次数200000LongAdder总自增次数20000000总耗时:37 线程数100单线程操作自增次数200000AtomicLong总自增次数20000000总耗时:358 由此可见随着线程数和自增次数增加 LongAdder的优势就体现出来了 2分析 具体实现思想如下图 https://www.processon.com/view/link/64c0dc6ef208ef32d3e43abd LongAdder的sum()放存在线程不安全问题调用sum()方法获取的结果不一定就是最终的结果 有可能base和Cell类的value属性正在参与运算 3自定义计算函数 public class LongAccumulatorTest {public static void main(String[] args) throws InterruptedException {// 累加 xyLongAccumulator accumulator new LongAccumulator((x, y) - x y, 0);ExecutorService executor Executors.newFixedThreadPool(8);// 2到10累加IntStream.range(2, 11).forEach(i - executor.submit(() - accumulator.accumulate(i)));Thread.sleep(2000);System.out.println(accumulator.getThenReset());} } 打印结果54 三、并发安全问题 1、线程封闭 就是把对象封装到一个线程里只有这一个线程能看到此对象。那么这个对 象就算不是线程安全的也不会出现任何安全问题。 1栈封闭 多个线程访问一个方法此方法中的局部变量都会被拷贝一份到 线程栈中。所以局部变量是不被多个线程所共享的也就不会出现并发问题。所 以能用局部变量就别用全局的变量全局变量容易引起并发问题。      2ThreadLocal ThreadLocal 是实现线程封闭的最好方法。ThreadLocal 内部维护了一个 Map Map 的 key 是每个线程的名称而 Map 的值就是我们要封闭的对象。每个线程 中的对象都对应着 Map 中一个值也就是 ThreadLocal 利用 Map 实现了对象的 线程封闭。    2、无状态的类 没有任何成员变量的类就叫无状态的类方法中含有其它对象导致的线程不安全那就是方法参数中这个类的问题 3、让类不可变 让状态不可变加 final 关键字对于一个类所有的成员变量应该是私有 的同样的只要有可能所有的成员变量应该加上 final 关键字但是加上 final 要注意如果成员变量又是一个对象时这个对象所对应的类也要是不可变才能 保证整个类是不可变的。 但是要注意一旦类的成员变量中有对象上述的 final 关键字保证不可变 并不能保证类的安全性为何因为在多线程下虽然对象的引用不可变但是 对象在堆上的实例是有可能被多个线程同时修改的没有正确处理的情况下对 象实例在堆中的数据是不可预知的。 4、加锁和CAS 我们最常使用的保证线程安全的手段使用 synchronized 关键字使用显式 锁使用各种原子变量修改数据时使用 CAS 机制等等。 5、死锁 一个锁资源肯定是不会发生死锁最少是两个线程去争抢两个资源争抢的顺序不对并且抢不到就一直抢而导致死锁在Java中可以通过jps命令jvisualvm打开可视化界面分析什么原因导致死锁 6、活锁 两个线程在尝试拿锁的机制中发生多个线程之间互相谦让不断发生同一 个线程总是拿到同一把锁在尝试拿另一把锁时因为拿不到而将本来已经持有 的锁释放的过程比如A、B两个线程同时进行A线程尝试拿锁1B线程尝试拿锁2此时都要拿对象持有的锁资源A线程拿不到锁2就释放了锁1线程B拿不到锁1就释放了锁2这样一直循环往复就永远相互等待。 解决办法每个线程休眠随机数错开拿锁的时间。 7、线程饥饿 低优先级的线程总是拿不到执行时间 8、单例模式 1基于DCL线程安全的懒汉单例模式 public class SingletonTest {private SingletonTest() {}private static volatile SingletonTest singletonTest null;public static SingletonTest get() {if (singletonTest null) {synchronized (SingletonTest.class) {// 这里为什么还要判断DCL双重检查 为了防止等待锁的线程进来没有判断又创建一个对象if (singletonTest null) {// java创建对象不是原子的// 1、申请内存空间// 2、对象初始化// 3、指向内存空间的地址// 要加上volatile关键字防止指令重排序返回没有初始化完的对象singletonTest new SingletonTest();}}}return singletonTest;}public static void main(String[] args) {System.out.println(SingletonTest.get());} } 2虚拟机保证线程安全的单例模式 懒汉单例模式 public class SingleLazy {private SingleLazy() {}private static class InstanceHolder {// 静态属性在类加载期间就初始化好了private static SingleLazy lazy new SingleLazy();}public static SingleLazy getInstance() {return InstanceHolder.lazy;} } 饿汉单例模式 public class SingleHungry {private SingleHungry() {}private static SingleHungry hungry new SingleHungry(); }
http://www.huolong8.cn/news/160693/

相关文章:

  • 如何给自己开发的网站加域名南宁做网站优化的公司
  • 如何用word做简单的网站中国建设银行太原招聘信息网站
  • 南昌建设厅网站南宁网站制作专业
  • 湛江做网站电话做一个网站需要多大的空间
  • 思乐网站建设小型企业网站开发
  • 网站定制化台州外包加工网
  • 伦教网站设计传奇类型的网游
  • 赣榆哪里有做网站的印象笔记 wordpress
  • 怎样做网站导购教程wordpress模板仿遮天小说站
  • 哪个网站做恒生指数最安全揭阳建网站
  • wordpress网站搭建教程做网站建设的怎么赢利
  • 深圳网站制作企业邮箱企业门户网站 php
  • 网站建设开发计入二级科目明细wordpress无刷新评论
  • 安装网站模版视频教程中国企业网银怎么转账
  • 做百度推广是不是得有个网站先深圳白帽优化
  • 网络推广方案的制定流程长春企业网站seo
  • 800元网站建设网站实用性
  • 网站开发和编程有什么区别网站排名 优帮云
  • 高端网站建设公司教育机构的域名
  • 诚通凯胜生态建设有限公司网站什么是网络营销的现实基础
  • 炫酷做网站背景图江苏省工程建设招标网站
  • 北京朝阳区建设工作办公网站东莞网站建设曼哈顿信科
  • 网站怎么做百度推广平面设计培训学校推荐
  • 上海网站建设公司排名如何卸载和重装wordpress
  • 重庆建设造价工程信息网站网络营销专业学校排名
  • 北京的医疗网站建设广州做网站多少钱
  • 怎样在线做网站404购物优惠券网站怎么做
  • 零代码建站平台古腾堡 主题 wordpress
  • 禁止网站采集公司官网的作用
  • 三门峡网站建设深圳高端网站建设