创建线程的方式

oracle的官方文档中,表示只有两种方法可以创建一个新的执行线程。一种方法是继承Thread类。另一种是实现Runnable 接口。

1.继承Thread类(官方)

package thread;

class MyThread extends Thread {
    public void run() {
        while (true) {
            System.out.println("继承Thread创建的线程 ");
            //减缓打印的速度
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

public class ThreadDemo1 {
    public static void main(String[] args) {
       Thread t = new MyThread();
       t.start();
       while (true){
           System.out.println("main");
           try {
               t.sleep(1000);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
       }
    }
}

运行结果(仅展示前十行):

代码一会儿打印 “main” 一会儿打印 “继承Thread创建的线程” 是因为此时有两个线程并发执行,一个线程就是一个单独的执行流。

当代码执行到 t.start() 时,第二个线程( t 线程)就被创建了。

哪个线程先执行是不确定的,因为操作系统内核中,有一个 “调度器” 模块,这个模块的实现方式类似于"随机调度" 效果,也就是一个线程什么时候被调度到 CPU 上执行,什么时候从 CPU 下来给别的线程让位置的时机都是未知的,这种行为也叫 “抢占式执行”

2.实现Runnable接口(官方)

package thread;

//通过 Runnable 接口,可以抽象表示出一段可以被其他实体执行的代码
class MyThread2 implements Runnable{
    @Override
    public void run() {
        while (true){
            System.out.println("实现Runnable接口");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

public class ThreadDemo2 {
    public static void main(String[] args) {
        //此处的 runnable 只是一段可执行代码
        Runnable runnable = new MyThread2();
        //真正创建线程还需要Thread实例化
        Thread t = new Thread(runnable);
        t.start();
        while(true){
            System.out.println("main");
            try {
                t.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

继承Thread类 和 实现 Runnable 接口 两种方法的比较

        1. 实现 Runnable 接口的这种写法其实就是把 线程 和 要执行的代码 进行了解耦合,多个线程有共享数据的情况。

 Runnable runnable = new MyThread2();
 Thread t = new Thread(runnable);

        2. Java中子类只能继承一个父类,但是可以实现多个接口。继承Thread类比较有局限性。

        3. 继承Thread类 这种方式,每当出现一个新任务,都要创建一个线程,如此线程的创建和销毁都会消耗比较大的资源。而 实现 Runnable 接口 这种方式只需要实现runnable接口即可, 节省了线程的创建和销毁所消耗的资源。

运行结果:

除去上述两种官方认定的创建线程的方式外,在日常开发中,我们好可以通过 线程池 和 实现Callable接口 两种方法创建线程。

3.实现Callable接口

实现Callable接口 与 实现 Runnable 接口 相比

        1. Runnable 关注的是这个过程,不关注执行结果。Runnable 提供的 run() 方法, 返回值类型是 void;Callable 关注的是执行结果,Callable 提供的 call() 方法,返回值就是线程执行任务得到的结果。

        2. Callable 提供的 call() 方法可以抛出异常,但是 run() 方法没有指定任何异常,所以不能抛出异常。

package thread;

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

class NewThread implements Callable {
    @Override
    public Object call() throws Exception {
        int sum = 0;
        for (int i = 1; i <= 10; i++) {
            sum += i;
        }
        return sum;
    }
}

public class ThreadDemo25 {
    public static void main(String[] args) {
        NewThread Thread = new NewThread();
        //创建FutureTask的对象
        FutureTask future = new FutureTask(Thread);
        Thread t = new Thread(future);
        
        t.start();
        
        try {
            Object sum = future.get();
            System.out.println("10以内数字总和为:" + sum);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

运行结果:

10以内数字总和为:55

4.使用线程池

创建方法(详细代码暂不予展示):

1. 通过实现 Runnable 接口或实现 Callable 接口创建线程池。
2. 调用线程池的 execute() 方法执行目标线程。

使用线程池的优点

1. 省去了创建新线程的时间。
2. 重复利用线程池中线程,减少资源浪费。

3. 可以更好的管理线程。

各种变形写法

Thread匿名内部类

匿名:没有名字,这样意味着这个不可以重复使用。

内部类:在类里再定义一个类。

匿名内部类最主要的目的,是描述这个方法(设置回调函数)。但是方法不能脱离类,单独存在,这就导致为了设置回调函数,不得不套上一层类了

package thread;

public class ThreadDemo3 {
    public static void main(String[] args) {
        //{}的意思是定义一个类,且这个类继承自 Thread
        //t 指向的实例不是单纯的 Thread,而是 Thread 的子类    
        Thread t = new Thread(){
            public void run(){
                while (true){
                    System.out.println("继承Thread,重写run,匿名内部类!");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        t.start();

        while(true){
            System.out.println("main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

运行结果:

Runnable匿名内部类

package thread;

public class ThreadDemo4 {
    public static void main(String[] args) {
        // Thread 构造方法的参数,填写了 Runnable 的匿名内部类
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println("实现Runnable,重写run,匿名内部类!");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        thread.start();

        while (true) {
            System.out.println("main!");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

运行结果:

lambda表达式

最常用的写法(代码简单直观时适用,因为lambda表达式可读性不好)

lambda表达式是匿名函数

package thread;

public class ThreadDemo5 {
    public static void main(String[] args) {
        //"()->"中括号前是函数名,但因为是匿名的,所以是空的
        //这个写法相当于实现 Runnable 重写 run,lambda 代替了 Runnable 的位置
        Thread t = new Thread(() ->{
            while(true){
                System.out.println("lambda匿名内部类");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();

        while(true){
            System.out.println("main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值