创建多线程的4种方式

目录
  • - 继承Thread
  • - 实现Runnable接口
  • - 实现Callable接口
  • - 使用线程池ThreadPoolExecutor

- 继承Thread

继承Thread类,重写run方法实现多线程

package com.noneplus;

class Task1 extends Thread {

    @Override
    public void run() {

        for (int i = 0; i < 10; i++) {
            System.out.println("Task1输出:" + i);
        }
    }
}

class Task2 extends Thread {

    @Override
    public void run() {

        for (int i = 0; i < 10; i++) {
            System.out.println("                 Task2输出:" + i);
        }
    }
}

/**
 * @Description: 继承Thread,重写run方法,实现多线程
 * @Author noneplus
 * @Date 2020/8/3 17:34
 */
public class ExtendThread {

    public static void main(String[] args) {

        Task1 task1 = new Task1();
        task1.start();

        Task2 task2 = new Task2();
        task2.start();

        for (int i = 0; i < 10; i++) {
            System.out.println("                                         mainTask输出:" + i);
        }

        //3个线程的执行顺序由CPU的线程调度决定

    }
}

- 实现Runnable接口

Runnable接口解决了单继承的问题

package com.noneplus;

class Task3 implements Runnable {

    @Override
    public void run() {

        for (int i = 0; i < 10; i++) {
            System.out.println("Task1输出:" + i);
        }
    }
}

class Task4 implements Runnable {

    @Override
    public void run() {

        for (int i = 0; i < 10; i++) {
            System.out.println("                      Task2输出:" + i);
        }
    }
}

/**
 * @Description: 实现Runnable接口,实现多线程,弥补单继承的问题
 * @Author noneplus
 * @Date 2020/8/3 17:34
 */
public class ImplementRunnable {

    public static void main(String[] args) {

        Task3 runnable3 = new Task3();
        Task4 runnable4 = new Task4();

        Thread task3 = new Thread(runnable3);
        Thread task4 = new Thread(runnable4);

        task3.start();
        task4.start();

        for (int i = 0; i < 10; i++) {
            System.out.println("                                                                  " +
                    " mainTask输出:" + i);
        }

        //3个线程的执行顺序由CPU的线程调度决定

    }
}

- 实现Callable接口

Callable支持返回值(但用多线程加返回值有点奇怪)

package com.noneplus;

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

class Task5 implements Callable {

    @Override
    public Integer call() throws Exception {
        Integer i = 0;
        for (; i < 10; i++) {
            System.out.println("Task5输出:" + i);
        }
        return i;
    }
}

class Task6 implements Callable {

    @Override
    public Integer call() throws Exception {
        Integer i = 0;
        for (; i < 10; i++) {
            System.out.println("             Task6输出:" + i);
        }
        return i;
    }
}


/**
 * @Description: 实现Callable接口,可以定义返回值
 * @Author noneplus
 * @Date 2020/8/3 17:53
 */
public class ImplementCallable {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        for (int i = 0; i < 10; i++) {
            System.out.println("                                         mainTask输出:" + i);
        }

        FutureTask futureTask = new FutureTask(new Task5());
        FutureTask futureTask1 = new FutureTask(new Task6());

        Thread thread = new Thread(futureTask);
        Thread thread1 = new Thread(futureTask1);

        thread.start();
        thread1.start();

        System.out.println("任务返回结果:"+futureTask.get());
        System.out.println("任务返回结果:"+futureTask1.get());

    }
}

- 使用线程池ThreadPoolExecutor

package com.noneplus;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

class Task7 implements Runnable {
    @Override
    public void run() {

        for (int i = 0; i < 10; i++) {
            System.out.println("Task7输出:" + i + "当前线程" + Thread.currentThread().getName());
        }
    }
}

class Task8 implements Runnable {
    @Override
    public void run() {

        for (int i = 0; i < 10; i++) {
            System.out.println("Task8输出:" + i + "当前线程" + Thread.currentThread().getName());

        }

    }
}

/**
 * @Description: 使用线程池创建线程池,实现线程复用和管理
 * @Author noneplus
 * @Date 2020/8/5 15:38
 */
public class ThreadPool {

    public static void main(String[] args) {
        /**
         * 阿里推荐:ThreadPoolExecutor
         * ```
         * public ThreadPoolExecutor(int corePoolSize,
         *                               int maximumPoolSize,
         *                               long keepAliveTime,
         *                               TimeUnit unit,
         *                               BlockingQueue workQueue)
         * ```
         * 1、corePoolSize 核心线程数大小,当线程数 < corePoolSize ,会创建线程执行 runnable
         *
         * 2、maximumPoolSize 最大线程数, 当线程数 >= corePoolSize的时候,会把 runnable 放入 workQueue中
         *
         * 3、keepAliveTime 保持存活时间,当线程数大于corePoolSize的空闲线程能保持的最大时间。
         *
         * 4、unit 时间单位
         *
         * 5、workQueue 保存任务的阻塞队列
         *
         * 6、threadFactory 创建线程的工厂
         *
         * 7、handler 拒绝策略
         *
         */
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 10, 3, TimeUnit.SECONDS, new ArrayBlockingQueue(3));

        for (int i=0;i<10;i++)
        {
            threadPoolExecutor.execute(new Task7());

            threadPoolExecutor.execute(new Task8());
        }


    }
}

你可能感兴趣的:(创建多线程的4种方式)