マルチスレッドテスト


テストコード
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 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 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 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 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