【多线程】

创建线程的写法

方法1.继承Thread,重写run
class  MyThread extends Thread{
    @Override
    public void run() {
        while(true){
            System.out.println("hello Thread");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
public class Demo1 {
    public static void main(String[] args) throws InterruptedException{
        Thread t =new MyThread();
        t.start();
        while (true){
            System.out.println("hello Main");
            Thread.sleep(1000);
        }
    }
}

t.start()真正系统中创建线程

run()线程的入口方法,新的线程启动了,就要执行这里的代码

run方法不需要手动调用,新的线程创建好了之后,自动的去执行

run方法相当于“回调函数”

你写一个函数你自己不去调用交给别人来调用 

方法2.实现Runnable,重写run
class MyRunnable implements Runnable{

    @Override
    public void run() {
        while (true){
            System.out.println("hello thread");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
public class Demo2 {
    public static void main(String[] args) throws InterruptedException {
        Runnable runnable = new MyRunnable();
        Thread t =new Thread(runnable);
        t.start();
        while(true){
            System.out.println("hello Main");
            Thread.sleep(1000);
        }
    }
}

Runnable runnble = new Runnable();

就是一个任务,就是一段要执行的逻辑

最终还是要通过Thread真正创建线程

线程里要干啥,通过Runnable来表示(而不是通过直接重写Thread run来表示了)

线程要执行的任务的定义是放到Thread里面,还是放到外面(Runnable中)

目的是为了解耦合,要让执行任务本身和线程这个概念能够解耦合

从而后续如果变更代码.(比如不通过线程执行这个任务,通过其他方式..)

采用Runnable这样的方案代码的修改就会更简单

方法3.继承Thread,重写run,使用匿名内部类
public class Demo3 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(){
            @Override
            public void run() {
                while(true){
                    System.out.println("hello Thread");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        };
        t.start();
        while (true){
            System.out.println("hello Main");
            Thread.sleep(1000);
        }
    }
}

这样就可以少定义一些类了

一般如果某个代码是“一次性”

就可以使用匿名内部类的写法

方法4.实现Runnable,重写run,使用匿名内部类
public class Demo4 {
    public static void main(String[] args) throws InterruptedException {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                while (true){
                    System.out.println("hello Thread");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        };
        Thread t = new Thread(runnable);
        t.start();
        while (true){
            System.out.println("hello Main");
            Thread.sleep(1000);
        }
    }
}

如果有一天需要把这里的任务,通过其他方式执行(不使用多线程了)

就需要把代码进行大规模的调整

使用Runnable,任务和线程概念是分离的

降低耦合就是为了后续改代码方便

如果压根不会改这里的代码,写成那种都无所谓

方法5.使用lambda表达式

lambda表达式本质上就是一个“匿名函数”

最主要的用途,就是作为“回调函数”

很多语言都有,只不过叫法可能有差异

Java中,方法必须要依托于类来存在

“函数式接口”

()->{}

创建了一个匿名的函数式接口的子类,并且创建出对应的实例

并且重写了里面的方法

(编译器在背后做的事情)

public class Demo5 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(()->{
           while (true){
               System.out.println("hello Thread");
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
           }
        });
        t.start();
        while (true){
            System.out.println("hello Main");
            Thread.sleep(1000);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值