1:volatile
- 保证线程可见性
当多个线程访问同一个共享资源时,线程会拷贝资源的副本到自己的工作内存。这样如果某个线程对这个资源进行写操作,其他线程不会马上知道。当对这个资源加volatile关键字,其他线程就会随时监听,更新新的值。
如下例子,不加volatile关键字,线程不会停止,加volatile关键字后会及时重新更新副本stop的值,线程停止。
package com.nobody.thread;
/**
* 不加volatile,输出:
* main start...
* thread start...
* change stop=true
*
* 加volatile,输出:
* main start...
* thread start...
* thread stop...
* change stop=true
* @author Μr.ηobοdy
*
* @date 2020-04-19
*
*/
public class VolatileDemo {
private /* volatile */ static boolean stop = false;
public static void main(String[] args) {
Thread t = new Thread(() -> {
System.out.println("thread start...");
while (!stop) {
}
System.out.println("thread stop...");
});
System.out.println("main start...");
t.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
stop = true;
System.out.println("change stop=" + stop);
}
}
- 禁止指令重排序
JIT(即时编译器just-in-time compiler) 是一种提高程序运行效率的方法,会将指令重排序。例如实例化一个对象,一般可分为3步骤,第一分配内存空间,第二初始化变量等,第三将引用地址赋值给引用对象。指令重排序可将顺序改为132。这样引用对象可能就拿到一个未初始化的对象,导致出错。
package com.nobody.thread;
/**
* 单例模式(懒汉式)
* 懒汉式必须加volatile
*
* @author Μr.ηobοdy
*
* @date 2020-04-19
*
*/
public class Singleton {
private /* vovalite */ static Singleton INSTANCE;
private String name;
private Singleton(String name) {
this.name = name;
}
public static Singleton getInstance() {
if (null == INSTANCE) {
synchronized (Singleton.class) {
if (null == INSTANCE) {
// 可能会出现指令重排序,即未进行成员变量name的初始化就退出了,
// 这样别人就会拿到未初始化(name=null)的Singleton对象
INSTANCE = new Singleton("hh");
}
}
}
return INSTANCE;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
- 不保证原子性
package com.nobody.thread;
import java.util.ArrayList;
import java.util.List;
/**
* volatile不保证原子性,最终结果一般小于10000
*
* 若要保证原子性,直接将doCount方法加synchronized关键字即可,而volatile可有可无
*
* @author Μr.ηobοdy
*
* @date 2020-04-19
*
*/
public class VolatileDemo1 {
private volatile static int count = 0;
private /*synchronized*/ void doCount() {
for (int i = 0; i < 1000; i++) {
count++;
}
}
public static void main(String[] args) {
VolatileDemo1 v = new VolatileDemo1();
// 启动10个线程
List<Thread> threads = new ArrayList<>();
for (int i = 1; i <= 10; i++) {
threads.add(new Thread(v::doCount, "thread-" + i));
}
threads.forEach(t -> t.start());
// 等待10个线程执行完
threads.forEach(t -> {
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
System.out.println("count=" + count);
}
}
2:CAS(Compare And Set 无锁优化 自旋锁)
设置新值之前会先将旧的值与期望值比较,如果相等才set,不然就重试或者失败。这是有CPU原语支持的。
package com.nobody.thread;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
/**
* CAS AtomicInteger保证原子性,最终结果一定等于10000
*
*
* @author Μr.ηobοdy
*
* @date 2020-04-19
*
*/
public class AtomicIntegerDemo {
private static AtomicInteger count = new AtomicInteger(0);
private void doCount() {
for (int i = 0; i < 1000; i++) {
count.incrementAndGet();
}
}
public static void main(String[] args) {
AtomicIntegerDemo v = new AtomicIntegerDemo();
// 启动10个线程
List<Thread> threads = new ArrayList<>();
for (int i = 1; i <= 10; i++) {
threads.add(new Thread(v::doCount, "thread-" + i));
}
threads.forEach(t -> t.start());
// 等待10个线程执行完
threads.forEach(t -> {
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
System.out.println("count=" + count);
}
}
不过这种会出现ABA问题,即由值A先变成值B,然后又变回A值,最后旧值与期望值比较还是相等。可用版本号解决这个问题。
3:LongAdder
采用分段锁思想,假如有1000个线程对同一个共享变量进行操作(例如自增),此处假设分为4小组,250个线程为1组,组内进行自增操作,这样分组能减少锁的概率,最后将每个小组进行求总和处理。其实分段锁组内还是CAS原理。一般在线程数高时,效率比synchronized和AtomicLong高。
package com.nobody.thread;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;
/**
* LongAdder,AtomicLong,synchronized多线程时效率比较
* 模拟1000个线程对一个等于0的值进行自增操作,每个线程自增10000
*
* 输出结果:
* longAdderCount:10000000, time:227
* atomicLongCount:10000000, time:395
* synchronizedCount:10000000, time:909
*
* @author Μr.ηobοdy
*
* @date 2020-04-20
*
*/
public class LongAdderDemo {
private static LongAdder longAdderCount = new LongAdder();
private static AtomicLong atomicLongCount = new AtomicLong(0L);
private static long synchronizedCount = 0L;
public static void main(String[] args) {
// LongAdder测试
List<Thread> longAdderThreads = new ArrayList<>(1000);
for (int i = 1; i <= 1000; i++) {
longAdderThreads.add(new Thread(() -> {
for (int j = 0; j < 10000; j++) {
longAdderCount.increment();
}
}));
}
long start = System.currentTimeMillis();
longAdderThreads.forEach(t -> t.start());
// 等待1000个线程执行完
longAdderThreads.forEach(t -> {
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
long end = System.currentTimeMillis();
// AtomicLong测试
List<Thread> atomicLongThreads = new ArrayList<>(1000);
for (int i = 1; i <= 1000; i++) {
atomicLongThreads.add(new Thread(() -> {
for (int j = 0; j < 10000; j++) {
atomicLongCount.incrementAndGet();
}
}));
}
long start1 = System.currentTimeMillis();
atomicLongThreads.forEach(t -> t.start());
// 等待1000个线程执行完
atomicLongThreads.forEach(t -> {
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
long end1 = System.currentTimeMillis();
// AtomicLong测试
List<Thread> synchronizedThreads = new ArrayList<>(1000);
Object o = new Object();
for (int i = 1; i <= 1000; i++) {
synchronizedThreads.add(new Thread(() -> {
for (int j = 0; j < 10000; j++) {
synchronized (o) {
synchronizedCount++;
}
}
}));
}
long start2 = System.currentTimeMillis();
synchronizedThreads.forEach(t -> t.start());
// 等待1000个线程执行完
synchronizedThreads.forEach(t -> {
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
long end2 = System.currentTimeMillis();
System.out.println("longAdderCount:" + longAdderCount + ", time:" + (end - start));
System.out.println("atomicLongCount:" + atomicLongCount + ", time:" + (end1 - start1));
System.out.println("synchronizedCount:" + synchronizedCount + ", time:" + (end2 - start2));
}
}