Java创建线程的三种方法

Java创建线程的三种方法

  1. 继承Thread类
  2. 实现Runnable接口
  3. 实现Callable接口

方法一:继承Thread类

  • 自定义线程类继承Thread类
  • 重写run()方法,编写线程执行体
  • 创建线程对象,调用start()方法开启线程
// 自定义线程类继承Thread类
public class TestThread1 extends Thread {
    private String threadName;

    public TestThread1(String threadName) {
        this.threadName = threadName;
    }
	// 重写run()方法,编写线程执行体
    public void run() {
        //线程体
        for (int i = 0; i < 20; i++) {
            System.out.println(threadName + i);
        }
    }

    public static void main(String[] args) {
	//创建线程对象,调用start()方法开启线程
        //创建线程对象一
        TestThread1 testThread11 = new TestThread1("我是线程一");
        //调用Start方法开启线程
        testThread11.start();
        //创建线程对象二
        TestThread1 testThread12 = new TestThread1("我是线程二");
        //调用Start方法开启线程
        testThread12.start();

    }
}

方法二:实现Runnable接口

  • 定义MyRunnable类实现Runnable接口
  • 实现Run()方法,编写线程执行体
  • 创建线程对象,调用start()方法开启线程
public class TestThread2 implements Runnable {
    private String threadName;
    public TestThread2(String threadName){
        this.threadName=threadName;
    }
    public void run(){
        //线程体
        for (int i = 0; i < 20; i++) {
            System.out.println(threadName+i);
        }
    }

    public static void main(String[] args) {
        //创建Runnable接口的实现类对象
        TestThread2 testThread21=new TestThread3("我是线程一");
        TestThread2 testThread22=new TestThread3("我是线程二");
        //通过线程对象来开启线程
        new Thread(testThread21).start();
        new Thread(testThread22).start();
    }
}

方法三:实现Callable接口

  • 实现Callable接口,需要返回值类型
  • 重写call方法,需要抛出异常
  • 创建目标对象
  • 创建执行服务:ExecutorService ser = Executors.newFixedThreadPool(1);
  • 提交执行:Future< Boolean> result1 = ser.submit(t1);
  • 获取结果:boolean r1=result1.get()
  • 关闭服务:ser.shutdownNow();
import java.util.concurrent.*;

public class TestThread4 implements Callable<Boolean> {
    private String threadName;

    public TestThread4(String threadName) {
        this.threadName = threadName;
    }

    public Boolean call() {
        //线程体
        for (int i = 0; i < 30; i++) {
            System.out.println(threadName + i);
        }
        return true;
    }

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

        TestThread4 t1 = new TestThread4("线程一");
        TestThread4 t2 = new TestThread4("线程二");
        //创建执行服务
        ExecutorService ser = Executors.newFixedThreadPool(2);
        //提交执行
        Future<Boolean> r1 = ser.submit(t1);
        Future<Boolean> r2 = ser.submit(t2);
        //获取结果
        boolean rs1 = r1.get();
        boolean rs2 = r2.get();
        //关闭服务
        ser.shutdownNow();

    }
}

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LeBron永鑫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值