系列三、创建线程的方式

一、继承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();
        }
    }
}

四、线程池

相关推荐
阿伟*rui9 天前
J.U.C(1)
juc
我命由我1234518 天前
27.Java 线程间通信(synchronized 实现线程间通信、Lock 实现线程间通信)
java·开发语言·后端·java-ee·intellij-idea·juc·后端开发
跳跳的向阳花23 天前
04、JUC并发编程之:简单概述(四)
java·开发语言·cas·juc·volatile·原子引用·原子整数
车到山前必有路52923 天前
JUC学习
学习·juc·condition
唐僧洗头爱飘柔95272 个月前
(Java并发编程——JUC)常见的设计模式概念分析与多把锁使用场景!!理解线程状态转换条件!带你深入JUC!!文章全程笔记干货!!
java·设计模式·并发编程·juc·reentrantlock·顺序控制·生产者与消费者
慕木沐2 个月前
【JUC并发】cmpxchg和lock指令
后端·并发·juc·汇编指令
HelloWorld. py4 个月前
JUC-ReentrantLock 锁粒度为什么更小
java·juc
lazy★boy4 个月前
JUC学习笔记(一)
juc
lazy★boy4 个月前
JUC学习笔记(三)
juc
小小工匠5 个月前
J.U.C Review - 常见的通信工具类解析
juc·countdownlatch·exchanger·phaser·semaphore·cyclicbarrier