线程创建的几种方式

本文详细介绍了Java中通过继承Thread类、实现Runnable接口、Callable接口以及使用匿名内部类和Lambda表达式来创建和启动线程的方法,展示了不同方式下的线程执行任务和控制流程。
摘要由CSDN通过智能技术生成

1.继承Thread类

class MyThread extends Thread {
    public void run() {
        // 线程执行的任务
        for (int i = 0; i < 5; i++) {
            System.out.println("Thread: " + i);
            try {
                Thread.sleep(1000); // 使线程休眠 1 秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

public class Main {
    public static void main(String[] args) {
        MyThread thread = new MyThread();
        thread.start(); // 启动线程
    }
}

2.实现Runnable接口

class MyRunnable implements Runnable {
    public void run() {
        // 线程执行的任务
        for (int i = 0; i < 5; i++) {
            System.out.println("Runnable: " + i);
            try {
                Thread.sleep(1000); // 使线程休眠 1 秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

public class Main {
    public static void main(String[] args) {
        MyRunnable myRunnable = new MyRunnable();
        Thread thread = new Thread(myRunnable);
        thread.start(); // 启动线程
    }
}

3.实现callable接口

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

public class Main {
    public static void main(String[] args) {
        // 创建 Callable 实例
        Callable<Integer> callable = new MyCallable();
        
        // 使用 FutureTask 包装 Callable 实例
        FutureTask<Integer> futureTask = new FutureTask<>(callable);
        
        // 创建线程并启动
        Thread thread = new Thread(futureTask);
        thread.start();
        
        try {
            // 获取线程执行结果,这里会阻塞直到线程执行完成并返回结果
            int result = futureTask.get();
            System.out.println("Result: " + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

// 实现 Callable 接口
class MyCallable implements Callable<Integer> {
    public Integer call() throws Exception {
        int sum = 0;
        for (int i = 1; i <= 10; i++) {
            sum += i;
        }
        return sum;
    }
}

4.使用匿名内部类

public class Main {
    public static void main(String[] args) {
        Runnable myRunnable = new Runnable() {
            public void run() {
                // 线程执行的任务
                for (int i = 0; i < 5; i++) {
                    System.out.println("Anonymous Runnable: " + i);
                    try {
                        Thread.sleep(1000); // 使线程休眠 1 秒
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        Thread thread = new Thread(myRunnable);
        thread.start(); // 启动线程
    }
}

5.使用Lambda表达式

public class Main {
    public static void main(String[] args) {
        Runnable myRunnable = () -> {
            // 线程执行的任务
            for (int i = 0; i < 5; i++) {
                System.out.println("Lambda Runnable: " + i);
                try {
                    Thread.sleep(1000); // 使线程休眠 1 秒
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        Thread thread = new Thread(myRunnable);
        thread.start(); // 启动线程
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值