加入收藏 | 设为首页 | 会员中心 | 我要投稿 汽车网 (https://www.0577qiche.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

Java 多线程四:锁二

发布时间:2023-04-15 11:21:06 所属栏目:教程 来源:
导读:需要实际测试一下。

示例代码见:

package git.snippets.juc;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;
/**
* 对比AddByAdder, AddByAtomic, Add
需要实际测试一下。

示例代码见:

package git.snippets.juc;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;
/**
 * 对比AddByAdder, AddByAtomic, AddBySync几个程序,在数据量比较大的情况下,AddByAdder的效率最高
 */
public class AddWays {
    public static void main(String[] args) {
        addBySync();
        addByAtomicLong();
        addByLongAdder();
    }
    // 使用AtomicLong
    public static void addByAtomicLong() {
        AtomicLong count = new AtomicLong(0);
        Thread[] all = new Thread[1000];
        AddWays t = new AddWays();
        for (int i = 0; i < all.length; i++) {
            all[i] = new Thread(() -> {
                for (int j = 0; j < 1000000; j++) {
                    count.incrementAndGet();
                }
            });
        }
        long start = System.currentTimeMillis();
        for (Thread thread : all) {
            thread.start();
        }
        for (Thread thread : all) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printstacktrace();
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("result is " + count.get() + " time is " + (end - start) + "ms (by AtomicLong)");
    }
    // 使用LongAdder
    public static void addByLongAdder() {
        Thread[] all = new Thread[1000];
        LongAdder count = new LongAdder();
        for (int i = 0; i < all.length; i++) {
            all[i] = new Thread(() -> {
                for (int j = 0; j < 1000000; j++) {
                    count.increment();
                }
            });
        }
        long start = System.currentTimeMillis();
        for (Thread thread : all) {
            thread.start();
        }
        for (Thread thread : all) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printstacktrace();
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("result is " + count + " time is " + (end - start) + "ms (by LongAdder)");
    }
    static long count = 0;
    public static void addBySync() {
        Thread[] all = new Thread[1000];
        Object o = new Object();
        for (int i = 0; i < all.length; i++) {
            all[i] = new Thread(() -> {
                for (int j = 0; j < 1000000; j++) {
                    synchronized (o) {
                        count++;
                    }
                }
            });
        }
        long start = System.currentTimeMillis();
        for (Thread thread : all) {
            thread.start();
        }
        for (Thread thread : all) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printstacktrace();
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("result is " + count + " time is " + (end - start) + "ms (by synchronized)");
    }
}
Java SE 11 下,运行得到的执行结果是:

result is 1000000000 time is 10035ms (by synchronized)
result is 1000000000 time is 15818ms (by AtomicLong)
result is 1000000000 time is 963ms (by LongAdder)

(编辑:汽车网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章