-
@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 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 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 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 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 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();
}
那么如何才能正确的掌握Redis呢?
为了让大家能够在Redis上能够加深,所以这次给大家准备了一些Redis的学习资料,还有一些大厂的面试题,包括以下这些面试题
-
并发编程面试题汇总
-
JVM面试题汇总
-
Netty常被问到的那些面试题汇总
-
Tomcat面试题整理汇总
-
Mysql面试题汇总
-
Spring源码深度解析
-
Mybatis常见面试题汇总
-
Nginx那些面试题汇总
-
Zookeeper面试题汇总
-
RabbitMQ常见面试题汇总
JVM常频面试:
Mysql面试题汇总(一)
Mysql面试题汇总(二)
Redis常见面试题汇总(300+题)
找小编(vip1024c)领取
-
Netty常被问到的那些面试题汇总
-
Tomcat面试题整理汇总
-
Mysql面试题汇总
-
Spring源码深度解析
-
Mybatis常见面试题汇总
-
Nginx那些面试题汇总
-
Zookeeper面试题汇总
-
RabbitMQ常见面试题汇总
JVM常频面试:
[外链图片转存中…(img-nF5uUeFc-1721631932879)]
Mysql面试题汇总(一)
[外链图片转存中…(img-MyTkVX3c-1721631932880)]
Mysql面试题汇总(二)
[外链图片转存中…(img-Sc2hKCyH-1721631932880)]
Redis常见面试题汇总(300+题)
[外链图片转存中…(img-ja980K7j-1721631932881)]
找小编(vip1024c)领取