如何设计个人网站响应式网站和自适应
news/
2025/9/30 0:03:46/
文章来源:
如何设计个人网站,响应式网站和自适应,网站开发雷小天,电商如何做前情回顾#xff1a;i操作并不是原子操作#xff0c;因此多线程下会达不到预期的效果#xff0c;需要通过加锁或AtomicInteger或LongAdder等方法来实现。
i可以分为三步
我们通过实验来观察实现i操作的方式。
下面实验中通过继承Thread实现了多线程 错误方法#xff1a;…前情回顾i操作并不是原子操作因此多线程下会达不到预期的效果需要通过加锁或AtomicInteger或LongAdder等方法来实现。
i可以分为三步
我们通过实验来观察实现i操作的方式。
下面实验中通过继承Thread实现了多线程 错误方法
1.多线程下直接进行自增操作
public class byte1 extends Thread{static int a 0;Overridepublic void run() {for(int i1;i10000;i) {a;}}public static void main(String[] args) {long nowTimeMillisSystem.currentTimeMillis(); byte1 tByte1 new byte1();ListThread threads new ArrayListThread();for(int i1;i10;i) {threads.add(new Thread(tByte1));}for(Thread thread :threads) {thread.start();}for(Thread thread :threads) {try {thread.join();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}System.out.println(a);long nowTimeMillis1System.currentTimeMillis();System.out.println(nowTimeMillis1-nowTimeMillisms);}
}
运行结果 可以看出与预期的100000差别很大。
2.通过 volatile来实现
public class byte1 extends Thread{volatile static int a 0;Overridepublic void run() {for(int i1;i10000;i) {a;}}public static void main(String[] args) {long nowTimeMillisSystem.currentTimeMillis(); byte1 tByte1 new byte1();ListThread threads new ArrayListThread();for(int i1;i10;i) {threads.add(new Thread(tByte1));}for(Thread thread :threads) {thread.start();}for(Thread thread :threads) {try {thread.join();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}System.out.println(a);long nowTimeMillis1System.currentTimeMillis();System.out.println(nowTimeMillis1-nowTimeMillisms);}
} 运行结果 volatile也无法达到预期效果因为volatile只可以实现可见性以及禁止指令重排。
当a为1时线程1与线程2都取出a线程1实现了a的操作但并未将值写入内存(为写入内存时其他线程看不到)此时线程2也开始执行a的操作线程1开始把a2写入内存线程2开始把a2写入内存
两个线程执行完之后a的值为2。
正确的实现方式
1.加锁synchronized
加锁的几种方式
对当前对象加锁对这个类加锁对这个方法加锁对一个对象加锁(但不能是int等基础类型)
synchronized(this){do();
}synchronized(T.class){do();
}synchronized m(){do();
}Object o new Object();
synchronized(o){do();
}public class byte1 extends Thread{static int a 0;Overridepublic void run() {synchronized (this) {for(int i1;i10000;i) {a;}}}public static void main(String[] args) {long nowTimeMillisSystem.currentTimeMillis(); byte1 tByte1 new byte1();ListThread threads new ArrayListThread();for(int i1;i10;i) {threads.add(new Thread(tByte1));}for(Thread thread :threads) {thread.start();}for(Thread thread :threads) {try {thread.join();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}System.out.println(a);long nowTimeMillis1System.currentTimeMillis();System.out.println(nowTimeMillis1-nowTimeMillisms);}
}
运行结果 2. 通过AtomicInteger实现
public class byte1 extends Thread{//static int a 0;static AtomicInteger a new AtomicInteger(0);Overridepublic void run() {synchronized (this) {for(int i1;i10000;i) {a.incrementAndGet();}}}public static void main(String[] args) {long nowTimeMillisSystem.currentTimeMillis(); byte1 tByte1 new byte1();ListThread threads new ArrayListThread();for(int i1;i10;i) {threads.add(new Thread(tByte1));}for(Thread thread :threads) {thread.start();}for(Thread thread :threads) {try {thread.join();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}System.out.println(a.get());long nowTimeMillis1System.currentTimeMillis();System.out.println(nowTimeMillis1-nowTimeMillisms);}
}运行结果 3.LongAdder实现
public class byte1 extends Thread{//static int a 0;static LongAdder a new LongAdder();Overridepublic void run() {for(int i1;i10000;i) {a.increment();}}public static void main(String[] args) {long nowTimeMillisSystem.currentTimeMillis(); byte1 tByte1 new byte1();ListThread threads new ArrayListThread();for(int i1;i10;i) {threads.add(new Thread(tByte1));}for(Thread thread :threads) {thread.start();}for(Thread thread :threads) {try {thread.join();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}System.out.println(a);long nowTimeMillis1System.currentTimeMillis();System.out.println(nowTimeMillis1-nowTimeMillisms);}
}
运行结果
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/922349.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!