Android : Java中创建线程的几种方式_简单应用

主方法 MainTest.java

package com.example.mythread;

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

public class MainTest {

    public static void main(String[] data){
//        以下的方法的线程并发执行
        //执行线程一
        ExtendsThread extendsThread = new ExtendsThread();
        // 1.执行线程中的方法
        extendsThread.getData();
        // 2. 或者 启动线程
        extendsThread.start();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        //执行线程二
        Runnable runnable = new RunnableThread();
        Thread thread2 = new Thread(runnable);
        //1 启动线程
        thread2.start();



        //执行线程三 实现Callable接口和Future创建线程
        Callable<String> callable = new CallableThread();
        FutureTask<String> futureTask = new FutureTask<>(callable);

        Thread thread3 = new Thread(futureTask);
        thread3.start();
        try {
            //获取线程返回值
            System.out.println(futureTask.get());

        } catch (Exception e) {
            e.printStackTrace();
        }


        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        // Lambda表达式简化写法
        //扩展 函数式接口 创建线程
        Thread thread4 = new Thread(() ->{
            System.out.println("扩展函数式接口的输出");
        });
        thread4.start();

        //扩展 Runnable函数式接口 创建线程
        Runnable runnable2 =()->{
            System.out.println("扩展Runnable函数式接口的输出");
        };
        new Thread(runnable2).start();

        // 扩展 Callable 函数式接口
        Callable<String> c = () ->{
            System.out.println("扩展线程开始运行");
            //线程暂停3秒 表示业务处理啥的
            Thread.sleep(3000);

            System.out.println("扩展线程执行完成");

            return "Callable接口实现返回的字符串";
        };
        //启动线程
        //new Thread(new FutureTask<String>(c)).start();
        FutureTask<String> futureTask2 = new FutureTask<String>(c);
        new Thread(futureTask2).start();
        try {
            //输出返回数据
            System.out.println(futureTask2.get());
        } catch (Throwable e) {
            e.printStackTrace();
        }

        //
        new Thread(){
            @Override
            public void run() {
                super.run();
                System.out.println("线程中的输出");
            }
        }.start();
    }
}

方式一:写个类继承Thread  ExtendsThread.java

package com.example.mythread;
// 方式一 写个类 extends Thread
public class ExtendsThread extends Thread{
    @Override
    public void run() {
        super.run();
        //调用方法
        getData();
    }

    public void getData(){
        System.out.println("方式一:我是线程中的输出");
    }
}

方式二: 写个类实现 Runnable接口  RunnableThread.java

package com.example.mythread;
//创建线程方式二 实现 Runnable  接口
public class RunnableThread implements Runnable{

    @Override
    public void run() {
        //调用方法
        getData();
    }

    public void getData(){
        System.out.println("方式二:我是线程中的输出");
    }
}

 方式三 写个类实现Callable接口和Future创建线程  CallableThread.java

package com.example.mythread;

import java.util.concurrent.Callable;

// 方式三 实现Callable接口和Future创建线程
public class CallableThread implements Callable<String> {

    @Override
    public String call() throws Exception {
        getData();
        return "要返回的数据";
    }
    public void getData(){
        System.out.println("方式三:我是线程中的输出");
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

javaGHui

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

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

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

打赏作者

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

抵扣说明:

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

余额充值