系列三、创建线程的方式

一、继承Thread

java 复制代码
/**
 * @Author : 一叶浮萍归大海
 * @Date: 2023/11/20 9:39
 * @Description: 创建线程的第一种方式:继承Thread
 */
public class CreateThreadDemo1 extends Thread {

    @Override
    public void run() {
        for (int i = 1; i <= 100; i++) {
            System.out.println(Thread.currentThread().getName() + i);
        }
    }

    public static void main(String[] args) {
        System.out.println("CreateThreadDemo1 main =================>");
        CreateThreadDemo1 t1 = new CreateThreadDemo1();
        CreateThreadDemo1 t2 = new CreateThreadDemo1();
        CreateThreadDemo1 t3 = new CreateThreadDemo1();

        t1.setName("aa");
        t2.setName("bbbbbb");
        t3.setName("ccccccccccccccccc");

        t1.start();
        t2.start();
        t3.start();
    }

}

二、实现Runnable

java 复制代码
/**
 * @Author : 一叶浮萍归大海
 * @Date: 2023/11/20 9:39
 * @Description: 创建线程的第二种方式:实现Runnable
 */
public class CreateThreadDemo2 implements Runnable {

    @Override
    public void run() {
        for (int i = 1; i <= 100; i++) {
            System.out.println(Thread.currentThread().getName() + i);
        }
    }

    public static void main(String[] args) {
        System.out.println("CreateThreadDemo2 main =================>");
//        CreateThreadDemo2 thread = new CreateThreadDemo2();
//        Thread t1 = new Thread(thread, "aa");
//        Thread t2 = new Thread(thread, "bbbbbb");
//        Thread t3 = new Thread(thread, "ccccccccccccccccc");
//
//        t1.start();
//        t2.start();
//        t3.start();

        new Thread(() -> {
            for (int i = 1; i <= 100; i++) {
                System.out.println(Thread.currentThread().getName() + i);
            }
        }, "aa").start();

        new Thread(() -> {
            for (int i = 1; i <= 100; i++) {
                System.out.println(Thread.currentThread().getName() + i);
            }
        }, "bbbbbb").start();

        new Thread(() -> {
            for (int i = 1; i <= 100; i++) {
                System.out.println(Thread.currentThread().getName() + i);
            }
        }, "ccccccccccccccccc").start();
    }

}

三、实现Callable

java 复制代码
/**
 * @Author : 一叶浮萍归大海
 * @Date: 2023/11/20 9:39
 * @Description: 创建线程的第二种方式:实现Callable
 */
public class CreateThreadDemo3 implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        int sum = 0;
        for (int i = 1; i <= 100; i++) {
            sum += i;
        }
        return sum;
    }

    public static void main(String[] args) {
        CreateThreadDemo3 thread = new CreateThreadDemo3();
        FutureTask<Integer> futureTask = new FutureTask<>(thread);
        new Thread(futureTask, "A").start();
        new Thread(futureTask, "B").start();
        new Thread(futureTask, "C").start();

        try {
            Integer result = futureTask.get();
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

四、线程池

相关推荐
HelloWorld. py1 个月前
JUC-ReentrantLock 锁粒度为什么更小
java·juc
lazy★boy2 个月前
JUC学习笔记(一)
juc
lazy★boy2 个月前
JUC学习笔记(三)
juc
小小工匠2 个月前
J.U.C Review - 常见的通信工具类解析
juc·countdownlatch·exchanger·phaser·semaphore·cyclicbarrier
水w2 个月前
“线程池中线程异常后:销毁还是复用?”
java·开发语言·线程池·juc
Czi橙3 个月前
深刻理解JDK中线程池的使用
java·spring·jdk·多线程·并发编程·juc
luming-023 个月前
图文详解ThreadLocal:原理、结构与内存泄漏解析
java·开发语言·jvm·java-ee·juc
Dexu73 个月前
【Java 并发编程】(三) 从CPU缓存开始聊 volatile 底层原理
juc·java并发编程
少不入川。3 个月前
ThreadLocal源码分析
java·juc·threadlocal
p_fly4 个月前
【JavaEE】CAS原理实现 + 常见应用
java·多线程·juc