哈尔滨市住房和城乡建设局网站,专注网站建设怎么样,慈溪建设集团网站,ssh做电商 网站目录 使用单线程使用多线程使用多线程 synchronized使用多线程 原子类AtomicLong 使用单线程
单线程修改计数器的值#xff0c;没有发生问题#xff0c;每次运行结果都是10000#xff0c;不过程序耗时较长
package com.example;/*** 计数器*/
class Counter {private st… 目录 使用单线程使用多线程使用多线程 synchronized使用多线程 原子类AtomicLong 使用单线程
单线程修改计数器的值没有发生问题每次运行结果都是10000不过程序耗时较长
package com.example;/*** 计数器*/
class Counter {private static long count;public static long getCount() {return count;}public static void incrementCount() {count;}
}public class Demo {public static void main(String[] args) throws InterruptedException {long count Counter.getCount();System.out.println(count);// 0for (int i 0; i 10000; i) {try {Thread.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}Counter.incrementCount();}count Counter.getCount();System.out.println(count);// 10000}
}
使用多线程
单线程修改计数器的值运行速度提高了不过运行结果每次都不一致而且结果不是10000
package com.example;import java.util.ArrayList;
import java.util.List;/*** 计数器*/
class Counter {private static long count;public static long getCount() {return count;}public static void incrementCount() {count;}
}public class Demo {public static void main(String[] args) throws InterruptedException {long count Counter.getCount();System.out.println(count);// 0ListThread list new ArrayList();// 启动10000个线程同时访问计数器for (int i 0; i 10000; i) {Thread thread new Thread(new Runnable() {Overridepublic void run() {try {Thread.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}Counter.incrementCount();}});list.add(thread);}for (Thread thread : list) {thread.start();}for (Thread thread : list) {thread.join();}count Counter.getCount();System.out.println(count);}
}执行结果
第一次9910
第二次9912
第三次9910使用多线程 synchronized
多线程加锁后最后结果都是10000
package com.example;import java.util.ArrayList;
import java.util.List;/*** 计数器*/
class Counter {private static long count;public static long getCount() {return count;}public static synchronized void incrementCount() {count;}
}public class Demo {public static void main(String[] args) throws InterruptedException {long count Counter.getCount();System.out.println(count);// 0ListThread list new ArrayList();// 启动10000个线程同时访问计数器for (int i 0; i 10000; i) {Thread thread new Thread(new Runnable() {Overridepublic void run() {try {Thread.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}Counter.incrementCount();}});list.add(thread);}for (Thread thread : list) {thread.start();}for (Thread thread : list) {thread.join();}count Counter.getCount();System.out.println(count);}
}
执行结果
第一次10000
第二次10000
第三次10000使用多线程 原子类AtomicLong
多线程中使用原子类AtomicLong实现计数器最后结果都是10000
原理是CASCompare and Set
先比较原始值和预期值如果相等则修改为新值不相等则修改失败
伪代码如下
bool compareAndSet(oldValue, expectValue, updateValue){if(oldValue expectValue){oldValue updateValue// update success} else{// update fail}
}package com.example;import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;/*** 计数器*/
class Counter {private static AtomicLong count new AtomicLong(0);public static long getCount() {return count.get();}public static void incrementCount() {count.incrementAndGet();}
}public class Demo {public static void main(String[] args) throws InterruptedException {long count Counter.getCount();System.out.println(count);// 0ListThread list new ArrayList();// 启动10000个线程同时访问计数器for (int i 0; i 10000; i) {Thread thread new Thread(new Runnable() {Overridepublic void run() {try {Thread.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}Counter.incrementCount();}});list.add(thread);}for (Thread thread : list) {thread.start();}for (Thread thread : list) {thread.join();}count Counter.getCount();System.out.println(count);}
}
执行结果
第一次10000
第二次10000
第三次10000参考 使用Atomic-廖雪峰的官方网站CAS锁机制无锁、自旋锁、乐观锁、轻量级锁java中的Atomic类