Java实现多线程的三种方式(带图详解)

1.继承Thread类,重写run方法

public class q1 extends Thread{
    private String threadName;
    public q1(String threadName){
        this.threadName = threadName;
    }
    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println(this.threadName+":"+i);
        }
    }

    public static void main(String[] args) {
        //start方法是多线程的启动线程的核心
        new q1("A").start();
        new q1("B").start();
        new q1("C").start();
    }
}

结果如下所示
结果

2.实现Runnable接口

我们编写一个MyRunnable类实现Runnable接口,重写run方法
通过观察可以看到Thead可传入一个Runnable类型参数
在这里插入图片描述

public class MyRunnable implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            //Thread.currentThread().getName()获得当前线程名称
            System.out.println(Thread.currentThread().getName()+":"+i);
        }
    }

    public static void main(String[] args) {
        //第二个参数即为自定义的线程名称
        new Thread(new MyRunnable(),"A").start();
        new Thread(new MyRunnable(),"B").start();
        new Thread(new MyRunnable(),"C").start();
    }
}

该接口只提供run方法,是一个函数式接口,可用lambda表达式简化书写

public class MyRunnable{

    public static void main(String[] args) {
        new Thread(() ->{
            for (int i = 0; i < 5; i++) {
                System.out.println(Thread.currentThread().getName()+""+i);
            }
        },"A").start();

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

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

3.实现Callable接口

可以注意到实现Runnable接口,当线程结束时没有任何标识,为了解决这一问题,通过实现Callable接口可以获得返回值

public class MyCalllable implements Callable<String> {
    @Override
    public String call() throws Exception {
        //注意call方法等价于run方法,是执行多线程的方法主体,只是多了一个返回值
        for (int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName()+":"+i);
        }
        return Thread.currentThread().getName()+"线程执行结束";
    }

    public static void main(String[] args) throws Exception {
        FutureTask<String> task1 = new FutureTask<>(new MyCalllable());
        FutureTask<String> task2 = new FutureTask<>(new MyCalllable());
        FutureTask<String> task3 = new FutureTask<>(new MyCalllable());
        new Thread(task1,"A").start();
        new Thread(task2,"B").start();
        new Thread(task3,"C").start();
        System.out.println(task3.get());
        System.out.println(task1.get());
        System.out.println(task2.get());
    }
}

结果为
在这里插入图片描述
该接口也是一个函数式接口,感兴趣的可以将上述写法改为lambda表达式形式

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值