线程的三种创建方式

1.继承Thread类

package com.Thread;


/**
 * 1.让子类继承Thread线程类
 */
public class MyThread extends Thread{

    //2.必须重写Thread的run方法
    @Override
    public void run() {
        //描述线程的执行任务
        for(int i=1; i<=5; i++){
            System.out.println("子线程MyThread线程输出:" + i);
        }
    }
}

package com.Thread;

/**
 * 目标:掌握线程的创建方式一:继承Thread
 */
public class ThreadTest1 {

    //main方法是由一条默认的主线程负责执行的
    public static void main(String[] args) {
        //3.创建MyThread线程类对象代表一个线程
        Thread t = new MyThread();
        //4.启动线程
        t.start();

        for(int i=1; i<=5; i++){
            System.out.println("主线程main输出:" + i);
        }
    }
}

2.实现Runnable接口

2.1常规写法

package com.Thread;

/**
 * 1.定义一个任务类,实现Runnable接口
 */
public class MyRunnable implements Runnable{

    //2.重写Runnable的run方法
    @Override
    public void run() {
        //线程要执行的任务
        for(int i=1; i<=5; i++){
            System.out.println("子线程输出 ===》" + i);
        }
    }
}

package com.Thread;

/**
 * 目标:掌握多线程的创建方式二,实现Runnable接口
 */
public class ThreadTest2 {

    public static void main(String[] args) {
        //3.创建任务对象
        Runnable target = new MyRunnable();

        //4.把任务对象交给一个线程对象处理
        new Thread(target).start();

        for(int i=1; i<=5; i++){
            System.out.println("主线程main输出:" + i);
        }
    }
}

2.2匿名类写法

package com.Thread;

public class ThreadTest2_2 {

    public static void main(String[] args) {
        //1.直接创建Runnable接口匿名内部类形式(任务对象)
        Runnable target = new Runnable() {
            @Override
            public void run() {
                for (int i = 1; i <=5 ; i++) {
                    System.out.println("子线程1输出:" + i);
                }
            }
        };

        new Thread(target).start();

        //简化形式1:
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 1; i <=5 ; i++) {
                    System.out.println("子线程2输出:" + i);
                }
            }
        }).start();

        //简化形式2(lambda表达式):
        new Thread(() -> {
            for (int i = 1; i <=5 ; i++) {
                System.out.println("子线程3输出:" + i);
            }
        }).start();


        for(int i=1; i<=5; i++){
            System.out.println("主线程main输出:" + i);
        }
    }
}

3.实现Callable接口

package com.Thread;

import java.util.concurrent.Callable;

/**
 * 1.让这个类实现Callable接口
 */
public class MyCallable implements Callable<String> {
    private int n;

    public MyCallable(int n) {
        this.n = n;
    }

    //2.重写call方法
    @Override
    public String call() throws Exception {
        //描述线程任务,返回线程执行返回后的结果
        //需求:求1-n的和返回
        int sum = 0;
        for (int i = 1; i <=n ; i++) {
            sum += i;
        }
        return "线程求出1-" + n + "的和是:" + sum;
    }
}

package com.Thread;

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

/**
 * 目标:掌握线程创建方式三,实现Callable接口
 */
public class Thread3 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //3.创建一个Callable的对象
        Callable<String> call = new MyCallable(100);
        //4.把Callable对象封装成一个FutureTask对象(任务对象)
        /**
         * 未来任务对象(FutureTask)的作用:
         * (1)它是一个任务对象,实现Runnable接口、
         * (2)可以在线程执行完毕之后,用未来任务对象调用get方法获取线程执行完毕后的结果
         */
        FutureTask<String> f1 = new FutureTask<>(call);
        //5.把任务对象交给Thread
        new Thread(f1).start();

        Callable<String> call2 = new MyCallable(200);
        FutureTask<String> f2 = new FutureTask<>(call2);
        new Thread(f2).start();

        //6.获取线程执行完毕后的结果
        //注意:如果执行到这,假如上面的线程还没执行完毕
        //这里的代码会暂停,等待上面线程执行完毕后才会获取结果
        String rs = f1.get();
        System.out.println(rs);

        String rs2 = f2.get();
        System.out.println(rs2);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值