public final int getAndIncrement() {
for (;;) {
int current = get();
int next = current + 1;
if (compareAndSet(current, next))
return current;
}
}
public class CASDemo {
private final int THREAD_NUM = 1000;
private final int MAX_VALUE = 20000000;
private AtomicInteger casI = new AtomicInteger(0);
private int syncI = 0;
private String path = "/Users/pingping/DataCenter/Books/Linux/Linux常用命令详解.txt";
public void casAdd() throws InterruptedException {
long begin = System.currentTimeMillis();
Thread[] threads = new Thread[THREAD_NUM];
for (int i = 0; i < THREAD_NUM; i++) {
threads[i] = new Thread(new Runnable() {
public void run() {
while (casI.get() < MAX_VALUE) {
casI.getAndIncrement();
}
}
});
threads[i].start();
}
for (int j = 0; j < THREAD_NUM; j++) {
threads[j].join();
}
System.out.println("CAS costs time: " + (System.currentTimeMillis() - begin));
}
public void syncAdd() throws InterruptedException {
long begin = System.currentTimeMillis();
Thread[] threads = new Thread[THREAD_NUM];
for (int i = 0; i < THREAD_NUM; i++) {
threads[i] = new Thread(new Runnable() {
public void run() {
while (syncI < MAX_VALUE) {
synchronized ("syncI") {
++syncI;
}
}
}
});
threads[i].start();
}
for (int j = 0; j < THREAD_NUM; j++)
threads[j].join();
System.out.println("sync costs time: " + (System.currentTimeMillis() - begin));
}
}
14 while (casI.get() < MAX_VALUE) {
15 casI.getAndIncrement();
16 }
public class CASDemo {
private final int THREAD_NUM = 1000;
private final int MAX_VALUE = 1000;
private AtomicInteger casI = new AtomicInteger(0);
private int syncI = 0;
private String path = "/Users/pingping/DataCenter/Books/Linux/Linux常用命令详解.txt";
public void casAdd2() throws InterruptedException {
long begin = System.currentTimeMillis();
Thread[] threads = new Thread[THREAD_NUM];
for (int i = 0; i < THREAD_NUM; i++) {
threads[i] = new Thread(new Runnable() {
public void run() {
while (casI.get() < MAX_VALUE) {
casI.getAndIncrement();
try (InputStream in = new FileInputStream(new File(path))) {
while (in.read() != -1);
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
threads[i].start();
}
for (int j = 0; j < THREAD_NUM; j++)
threads[j].join();
System.out.println("CAS Random costs time: " + (System.currentTimeMillis() - begin));
}
public void syncAdd2() throws InterruptedException {
long begin = System.currentTimeMillis();
Thread[] threads = new Thread[THREAD_NUM];
for (int i = 0; i < THREAD_NUM; i++) {
threads[i] = new Thread(new Runnable() {
public void run() {
while (syncI < MAX_VALUE) {
synchronized ("syncI") {
++syncI;
}
try (InputStream in = new FileInputStream(new File(path))) {
while (in.read() != -1);
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
threads[i].start();
}
for (int j = 0; j < THREAD_NUM; j++)
threads[j].join();
System.out.println("sync costs time: " + (System.currentTimeMillis() - begin));
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有