【并发编程】Java创建多线程的五种方式

1. 继承Thread类

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

/**
 * 继承Thread类创建多线程单元测试
 *
 * @author CL
 */
@Slf4j
public class ThreadTest {

    @Test
    public void testThread() {
        for (int i = 1; i <= 5; i++) {
            new MyThread("thread-" + i).start();
        }
    }

    /**
     * 创建线程
     */
    class MyThread extends Thread {

        public MyThread(String name) {
            super(name);
        }

        @Override
        public void run() {
            log.info(Thread.currentThread().getName() + " 正在执行任务...");

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

}

  控制台打印:

21:41:48.690 [thread-1] INFO com.c3stones.test.MyThread - thread-1 正在执行任务...
21:41:48.690 [thread-3] INFO com.c3stones.test.MyThread - thread-3 正在执行任务...
21:41:48.690 [thread-5] INFO com.c3stones.test.MyThread - thread-5 正在执行任务...
21:41:48.690 [thread-4] INFO com.c3stones.test.MyThread - thread-4 正在执行任务...
21:41:48.690 [thread-2] INFO com.c3stones.test.MyThread - thread-2 正在执行任务...

2. 实现Runnable接口(包含Java8 Lambda方式)

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

/**
 * 实现Runnable接口创建多线程单元测试
 *
 * @author CL
 */
@Slf4j
public class RunnableTest {

    @Test
    public void testRunnable() {
        MyRunnable myRunnable = new MyRunnable();
        for (int i = 1; i <= 5; i++) {
            new Thread(myRunnable, "thread-" + i).start();
        }

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // java8 lambda方式
        for (int i = 1; i <= 5; i++) {
            new Thread(() -> {
                log.info(Thread.currentThread().getName() + " 正在执行任务...");

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }, "lambda-thread-" + i).start();
        }
    }

    /**
     * 创建线程
     */
    class MyRunnable implements Runnable {

        @Override
        public void run() {
            log.info(Thread.currentThread().getName() + " 正在执行任务...");

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}

  控制台打印:

21:44:56.808 [thread-1] INFO com.c3stones.test.MyRunnable - thread-1 正在执行任务...
21:44:56.807 [thread-2] INFO com.c3stones.test.MyRunnable - thread-2 正在执行任务...
21:44:56.807 [thread-4] INFO com.c3stones.test.MyRunnable - thread-4 正在执行任务...
21:44:56.808 [thread-5] INFO com.c3stones.test.MyRunnable - thread-5 正在执行任务...
21:44:56.808 [thread-3] INFO com.c3stones.test.MyRunnable - thread-3 正在执行任务...
21:44:58.899 [lambda-thread-3] INFO com.c3stones.test.RunnableTest - lambda-thread-3 正在执行任务...
21:44:58.899 [lambda-thread-1] INFO com.c3stones.test.RunnableTest - lambda-thread-1 正在执行任务...
21:44:58.900 [lambda-thread-4] INFO com.c3stones.test.RunnableTest - lambda-thread-4 正在执行任务...
21:44:58.900 [lambda-thread-2] INFO com.c3stones.test.RunnableTest - lambda-thread-2 正在执行任务...
21:44:58.900 [lambda-thread-5] INFO com.c3stones.test.RunnableTest - lambda-thread-5 正在执行任务...

3. 实现Callable接口(包含Java8 Lambda方式)

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 实现Callable接口创建多线程单元测试
 *
 * @author CL
 */
@Slf4j
public class CallableTest {

    @Test
    public void testCallable() {
        MyCallable myCallable = new MyCallable();
        for (int i = 1; i <= 5; i++) {
            FutureTask<String> futureTask = new FutureTask<>(myCallable);
            new Thread(futureTask, "thread-" + i).start();

            try {
                String result = futureTask.get();
                log.info("thread-" + i + " 返回结果:" + result);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }

        // java8 lambda方式
        for (int i = 1; i <= 5; i++) {
            FutureTask<String> futureTask = new FutureTask<>(() -> {
                log.info(Thread.currentThread().getName() + " 正在执行任务...");

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                return Thread.currentThread().getName();
            });


            new Thread(futureTask, "lambda-thread-" + i).start();

            try {
                String result = futureTask.get();
                log.info("lambda-thread-" + i + " 返回结果:" + result);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 创建线程
     */
    class MyCallable implements Callable<String> {

        @Override
        public String call() {
            log.info(Thread.currentThread().getName() + " 正在执行任务...");

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return Thread.currentThread().getName();
        }
    }

}

  控制台打印:

21:46:31.805 [thread-1] INFO com.c3stones.test.MyCallable - thread-1 正在执行任务...
21:46:32.818 [main] INFO com.c3stones.test.CallableTest - thread-1 返回结果:thread-1
21:46:32.819 [thread-2] INFO com.c3stones.test.MyCallable - thread-2 正在执行任务...
21:46:33.820 [main] INFO com.c3stones.test.CallableTest - thread-2 返回结果:thread-2
21:46:33.821 [thread-3] INFO com.c3stones.test.MyCallable - thread-3 正在执行任务...
21:46:34.822 [main] INFO com.c3stones.test.CallableTest - thread-3 返回结果:thread-3
21:46:34.822 [thread-4] INFO com.c3stones.test.MyCallable - thread-4 正在执行任务...
21:46:35.822 [main] INFO com.c3stones.test.CallableTest - thread-4 返回结果:thread-4
21:46:35.822 [thread-5] INFO com.c3stones.test.MyCallable - thread-5 正在执行任务...
21:46:36.822 [main] INFO com.c3stones.test.CallableTest - thread-5 返回结果:thread-5
21:46:36.877 [lambda-thread-1] INFO com.c3stones.test.CallableTest - lambda-thread-1 正在执行任务...
21:46:37.878 [main] INFO com.c3stones.test.CallableTest - lambda-thread-1 返回结果:lambda-thread-1
21:46:37.878 [lambda-thread-2] INFO com.c3stones.test.CallableTest - lambda-thread-2 正在执行任务...
21:46:38.878 [main] INFO com.c3stones.test.CallableTest - lambda-thread-2 返回结果:lambda-thread-2
21:46:38.878 [lambda-thread-3] INFO com.c3stones.test.CallableTest - lambda-thread-3 正在执行任务...
21:46:39.878 [main] INFO com.c3stones.test.CallableTest - lambda-thread-3 返回结果:lambda-thread-3
21:46:39.878 [lambda-thread-4] INFO com.c3stones.test.CallableTest - lambda-thread-4 正在执行任务...
21:46:40.878 [main] INFO com.c3stones.test.CallableTest - lambda-thread-4 返回结果:lambda-thread-4
21:46:40.878 [lambda-thread-5] INFO com.c3stones.test.CallableTest - lambda-thread-5 正在执行任务...
21:46:41.879 [main] INFO com.c3stones.test.CallableTest - lambda-thread-5 返回结果:lambda-thread-5

4. 项目地址

  thread-demo

posted @ 2023-03-28 16:53  C3Stones  阅读(86)  评论(0编辑  收藏  举报