线程与静态代理模式、创建接口对象的简写方法

线程与静态代理模式以及创建接口对象的简写方法

静态代理模式

静态代理是多线程底部的原理,在 Java 中线程的设计就使用了静态代理设计模式,其中自定义线程类及Thread类都是实现了Runable接口。

​ 在创建子线程的时候,传入了自定义线程类的引用,再通过调用start()方法,调用自定义线程对象的run()方法。实现了线程的并发执行。

概要

  • 真实对象和代理对象都要实现同一个接口
  • 代理对象要代理真实角色
  • 真实对象只专注于自己的功能,代理对象可以帮忙做其他的事

步骤

  1. 定义抽象接口
  2. 定义真实对象的类
  3. 定义代理对象的类
  4. 通过代理对象调用实现方法
package com.gcbeen.thread;

public class TestStaticProxy {

    public static void main(String[] args) {
        WeddingCompany company = new WeddingCompany(new You());
        company.happyMary();
    }
}

// 结婚
interface Marry{
    void happyMary();
}

// 真实角色:你去结婚
class You implements Marry{
    @Override
    public void happyMary() {
        System.out.println(" gcbeen 结婚了……");
    }
}

// 代理角色:婚庆公司帮你结婚
class WeddingCompany implements Marry{
    private Marry target;   // 代理--->真实目标角色,帮谁结婚

    public WeddingCompany(Marry target){
        this.target = target;
    }

    @Override
    public void happyMary() {
        before();
        this.target.happyMary();
        after();
    }

    private void before(){
        System.out.println("结婚前,布置现场!");
    }

    private void after(){
        System.out.println("结婚后,收尾款!");
    }

}

Thread 源码

    public synchronized void start() {
        /**
         * This method is not invoked for the main method thread or "system"
         * group threads created/set up by the VM. Any new functionality added
         * to this method in the future may have to also be added to the VM.
         *
         * A zero status value corresponds to state "NEW".
         */
        if (threadStatus != 0)
            throw new IllegalThreadStateException();

        /* Notify the group that this thread is about to be started
         * so that it can be added to the group's list of threads
         * and the group's unstarted count can be decremented. */
        group.add(this);

        boolean started = false;
        try {
            // 调用了 本地方法 start0
            // start0 执行了 run 方法
            start0();
            started = true;
        } finally {
            try {
                if (!started) {
                    group.threadStartFailed(this);
                }
            } catch (Throwable ignore) {
                /* do nothing. If start0 threw a Throwable then
                  it will be passed up the call stack */
            }
        }
    }

    private native void start0();

真实对象和代理对象都要实现一个接口;代理对象要代理真实角色。

  • 好处
    • 代理对象可以做很多真实对象做不了的事情;
    • 真实对象专注做自己的事。

使用 Lambda 表达式 简化接口对象的创建

在这里插入图片描述

λ 希腊字母表中排序第十一位的字母,英语名称为 Lambda;

避免匿名内部类定义过多;其实质属于函数式编程的概念;去掉了一堆没有意义的代码,只留下核心逻辑。

表达式书写格式

  • (params)-> expression[表达式]

  • (params) -> statement[语句]

  • (params)->

a -> System.out.println("i like lamda-->"+a)
package com.gcbeen.thread;

public class TestStaticProxy02 {

    public static void main(String[] args) {
        new Thread(() -> System.out.println("金石良缘、木石前盟")).start();
        // new Thread(() -> System.out.println("多线程学习。。。。")).start();
    }

}

函数式接口的定义

理解 Functional Interface (函数式接口) 是学习 Java 8 lambda 表达式的关键。

任何接口,如果只包含唯一一个抽象方法,那么它就是一个函数式接口。

Runnable 接口举例

public interface Runnable{
    public abstract void run();
}

通过 Lambda表达式 创建接口的对象

对于函数式接口,我们可以通过 Lambda表达式 来创建该接口的对象。

package com.gcbeen.thread;

public class TestLambda {

    public static void main(String[] args) {
        // 创建接口对象
        Like like = new Like();
        like.like();

    }
}

// 1.定义一个函数式接口
interface ILike {
    void like();
}

// 2.实现类
class Like implements ILike {
    @Override
    public void like() {
        System.out.println("I like Lambda");
    }
}

静态内部类

package com.gcbeen.thread;

public class TestLambda {

    // 静态内部类 实现 接口
    static class Like implements ILike{
        @Override
        public void like(){
            System.out.println("I like Lambda2");
        }
    }

    public static void main(String[] args) {
        // 创建接口对象
        Like like = new Like();
        like.like();

    }
}

// 1.定义一个函数式接口
interface ILike {
    void like();
}

局部内部类

package com.gcbeen.thread;

public class TestLambda {

    public static void main(String[] args) {

        // 局部内部类 实现 接口
        class Like implements ILike{
            @Override
            public void like(){
                System.out.println("I like Lambda3");
            }
        }

        // 创建接口对象
        Like like = new Like();
        like.like();

    }
}

// 1.定义一个函数式接口
interface ILike {
    void like();
}

匿名内部类

package com.gcbeen.thread;

public class TestLambda {

    public static void main(String[] args) {

        // 创建接口对象
        ILike like = new ILike() {
            // 5.匿名内部类,没有类的名称,必须借助接口或父类
            @Override
            public void like() {
                System.out.println("I like Lambda4");
            }
        };
        like.like();

    }
}

// 1.定义一个函数式接口
interface ILike {
    void like();
}

lambda表达式

package com.gcbeen.thread;

public class TestLambda {

    public static void main(String[] args) {
        // 简化 创建接口对象
        ILike like = () -> {
            System.out.println("I like Lambda5");
        };
        like.like();
    }
}

// 1.定义一个函数式接口
interface ILike {
    void like();
}

lambda 简写方式

前提是接口为函数式接口(只能有一个方法)

()省略的条件是 没有参数。

{} 简略的条件是只能有一行代码,多行{}就不能简略了

多个参数也可以去掉参数类型, 要去掉就都去掉,必须加上()

package com.gcbeen.thread;

public class TestLambda02 {

    public static void main(String[] args) {
        // 1.lambda
        ILove love = (int a) -> {
            System.out.println("木石前盟 -->" + a);
        };
        // 2.lambda简化1.0
        love = (a) -> {
            System.out.println("木石前盟 --> " + a);
        };
        // 3.lambda简化2.0
        love = a -> {
            System.out.println("木石前盟 --> " + a);
        };
        // 4.lambda简化4.0
        love = a -> System.out.println("木石前盟 --> " + a);

        love.love(100);
    }
}

// 1.定义一个函数式接口
interface ILove {
    void love(int a);
}
posted @ 2022-09-30 05:50  gcbeen  阅读(43)  评论(0)    收藏  举报