多线程测试

测试代码

package com.example.concurrency;

import com.example.concurrency.features.threadPool.ThreadPoolBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;

public class ThreadPoolTest {

    private static ExecutorService ExecutorService = new ThreadPoolBuilder.FixedThreadPoolBuilder().setPoolSize(10).build();

    public static void main(String[] args) {
        long bt = System.currentTimeMillis();
        List<String> list = new ArrayList<>();
        list.add("0");
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        list.add("6");
        list.add("7");
        list.add("8");
        list.add("9");
        m1(list);
        long et1 = System.currentTimeMillis();
        System.out.println("[1]耗时:" + (et1 - bt) + "ms");

        long at = System.currentTimeMillis();
        m2(list);
        long et2 = System.currentTimeMillis();
        System.out.println("[2]耗时:" + (et2 - at) + "ms");

        long ct = System.currentTimeMillis();
        m3(list);
        long et3 = System.currentTimeMillis();
        System.out.println("[3]耗时:" + (et3 - ct) + "ms");
    }

    //并发执行,存在cpu切换消耗
    private static void m1(List<String> list) {
        for (int i = 0; i < list.size(); i++) {
            String str = list.get(i);
            System.out.println(list.get(i));
            Runnable run = new Runnable() {
                public void run() {
                    try {
                        new Thread().sleep(1000);
                        //模拟耗时操作
                        System.out.println("[1]" + Thread.currentThread().getName() + "----" + str);
                    } catch (Exception e) {
                    }
                }
            };
            ExecutorService.execute(run);
        }
        System.out.println("[1] done!");
    }

    //异步执行
    private static void m2(List<String> list) {
        Runnable run = new Runnable() {
            public void run() {
                try {
                    for (int i = 0; i < list.size(); i++) {
                        String str = list.get(i);
                        new Thread().sleep(1000);
                        //模拟耗时操作
                        System.out.println("[2]" + Thread.currentThread().getName() + "----" + str);
                    }
                } catch (Exception e) {
                }
            }
        };
        ExecutorService.execute(run);
        System.out.println("[2] done!");
        ExecutorService.shutdown();
    }

     //单线程
    private static void m3(List<String> list) {
        for (int i = 0; i < list.size(); i++) {
            String str = list.get(i);
            try {
                new Thread().sleep(1000);  //模拟耗时操作
                System.out.println("[3]" + Thread.currentThread().getName() + "----" + str);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println("[3] done!");
    }
}


执行结果:

0
1
2
3
4
5
6
7
8
9
[1] done!
[1]耗时:5ms
[2] done!
[2]耗时:2ms
[1]pool-1-thread-2----1
[3]main----0
[1]pool-1-thread-3----2
[1]pool-1-thread-1----0
[1]pool-1-thread-10----9
[1]pool-1-thread-4----3
[1]pool-1-thread-8----7
[1]pool-1-thread-6----5
[1]pool-1-thread-7----6
[1]pool-1-thread-5----4
[1]pool-1-thread-9----8
[2]pool-1-thread-2----0
[3]main----1
[2]pool-1-thread-2----1
[3]main----2
[2]pool-1-thread-2----2
[3]main----3
[2]pool-1-thread-2----3
[3]main----4
[2]pool-1-thread-2----4
[3]main----5
[3]main----6
[2]pool-1-thread-2----5
[2]pool-1-thread-2----6
[3]main----7
[3]main----8
[2]pool-1-thread-2----7
[2]pool-1-thread-2----8
[3]main----9
[3] done!
[3]耗时:10025ms
[2]pool-1-thread-2----9

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值