Java-多个多线程求和例子以及各种方式的执行效率对比

一、使用Synchronized 同步代码块 ;

package thread.worker;

import org.junit.Test;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by hl on 2017/8/17.
 */
public class CountWorker {
    private int unsafeSum = 0;

    private synchronized void increase(CountWorker countWorker) {
         countWorker.unsafeSum++;
    }

    @Test
    public void testWorker() {
        for (int j = 0; j < 10; j++) {
            CountWorker countWorker = new CountWorker();
            for (int i = 0; i < 10; i++) {
                new Thread() {
                    public void run() {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        increase(countWorker);
                    }
                }.start();
            }

            while (Thread.activeCount() > 1)
                // 保证前面的线程都执行完
                Thread.yield();
            System.out.println(countWorker.unsafeSum);
        }
    }
}

10次计算,每次消耗的时间 :

elapse time  is : 1058
elapse time  is : 1002
elapse time  is : 1002
elapse time  is : 1002
elapse time  is : 1001
elapse time  is : 1002
elapse time  is : 1001
elapse time  is : 1002
elapse time  is : 1002
elapse time  is : 1001

二、使用 AtomicInteger 线程安全变量进行数据累加求和

package thread.worker;

import org.junit.Test;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by hl on 2017/8/17.
 */
public class CountWorker {
    AtomicInteger sum = new AtomicInteger(0);

    @Test
    public void testWorker() {
        for (int j = 0; j < 10; j++) {
            CountWorker countWorker = new CountWorker();
            for (int i = 0; i < 10; i++) {
                new Thread() {
                    public void run() {
                    countWorker.sum.incrementAndGet();
                    }
                }.start();
            }

            while (Thread.activeCount() > 1)
                // 保证前面的线程都执行完
                Thread.yield();
            System.out.println(countWorker.sum);
        }
    }
}

10次计算,每次消耗的时间 :

elapse time  is : 7
elapse time  is : 3
elapse time  is : 2
elapse time  is : 3
elapse time  is : 9
elapse time  is : 2
elapse time  is : 3
elapse time  is : 3
elapse time  is : 4
elapse time  is : 34

三、使用countDownLatch 代替Thread.activeCount()判断

package thread.worker;

import org.junit.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by hl on 2017/8/17.
 */
public class CountWorker {
    private int unsafeSum = 0;
    private synchronized void increase(CountWorker countWorker) {
        countWorker.unsafeSum++;
    }

    @Test
    public void testWorker() throws InterruptedException {
        for (int j = 0; j < 10; j++) {
            CountWorker countWorker = new CountWorker();
            CountDownLatch countLatch = new CountDownLatch(10);
            for (int i = 0; i < 10; i++) {
                new Thread() {
                    public void run() {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        increase(countWorker);
                        countLatch.countDown();
                    }
                }.start();
            }
            countLatch.await();
            System.out.println(countWorker.unsafeSum);
        }
    }
}

10次计算,每次消耗的时间 :

elapse time  is : 1011
elapse time  is : 1002
elapse time  is : 1005
elapse time  is : 1001
elapse time  is : 1001
elapse time  is : 1002
elapse time  is : 1003
elapse time  is : 1001
elapse time  is : 1002
elapse time  is : 1001

四、使用lock.lock()代替synchronized

package thread.worker;

import org.junit.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by hl on 2017/8/17.
 */
public class CountWorker {
    private int unsafeSum = 0;


    private void increase(CountWorker countWorker) {
        countWorker.unsafeSum++;
    }

    @Test
    public void testWorker() throws InterruptedException {
        Lock lock = new ReentrantLock();
        for (int j = 0; j < 10; j++) {
            CountWorker countWorker = new CountWorker();
            CountDownLatch countCount = new CountDownLatch(30);
            for (int i = 0; i < 30; i++) {
                new Thread() {
                    public void run() {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        lock.lock();
                        increase(countWorker);
                        lock.unlock();
                        countCount.countDown();
                    }
                }.start();
            }
            countCount.await();
            System.out.println(countWorker.unsafeSum);
        }
    }
}

10次计算,每次消耗的时间 :

elapse time  is : 1012
elapse time  is : 1019
elapse time  is : 1018
elapse time  is : 1011
elapse time  is : 1013
elapse time  is : 1041
elapse time  is : 1011
elapse time  is : 1013
elapse time  is : 1031
elapse time  is : 1009

最后说明一下:

while (Thread.activeCount() > 1)
// 保证前面的线程都执行完
Thread.yield();
在Run和Debug两种启动方式下 , Run方式启动会导致线程一直等待 , 目前原因不明 ….

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值