Related expansion of Java multithreading (static proxy and Lamda expression evolution)

1. Static proxy

1. Concept

Both the real object and the proxy object must implement the same interface,
and the proxy object must represent the real role

2. Advantages

Proxy objects can do many things that real objects can't do.
Real objects focus on doing their own things

3. Sample

package com.example.multithreading.demo3;

public class StaticProxy {
    
    

    public static void main(String[] args) {
    
    

        You you = new You();
//        1、没加多线程的操作
//        WeddingCompany weddingCompany = new WeddingCompany(you);
//        weddingCompany.HappyMarray();

        // 2、使用多线程的操作
        new Thread(() ->System.out.println("开始")).start();
        new WeddingCompany(new You()).HappyMarray();
    }
}

interface Marray{
    
    
    void HappyMarray();
}

// 真实角色
class You implements Marray{
    
    
    @Override
    public void HappyMarray(){
    
    
        System.out.println("主体操作");
    }
}

// 代理角色
class WeddingCompany implements Marray{
    
    

    //代理真实目标角色
    private Marray target;

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

    @Override
    public void HappyMarray(){
    
    
        before();
        // 这就是真实对象
        this.target.HappyMarray();
        after();
    }

    private void before(){
    
    
       System.out.println("代理之前的操作");
    }

    private void after(){
    
    
       System.out.println("代理之后的操作");
    }
}

result
insert image description here

2. Lamda expression (display of evolution process)

1. Define a functional interface and implement the class (initial state)

The beginning is to define an interface. Then the main program is called successfully.

package com.example.multithreading.demo4;

/**
 * 推导lambda表达式
 */
public class lamda {
    
    

    public static void main(String[] args) {
    
    
        Ilike like = new Like();
        like.lambda(1);
    }
}


// 1、定义一个函数式接口
interface Ilike{
    
    
    void lambda(int a);
}

// 2、实现类
class Like implements Ilike {
    
    
    @Override
    public void lambda(int a) {
    
    
        System.out.println("lambda:" + a);
    }
}


result
insert image description here

2. Static inner class (state evolution 2)

Evolved into a static inner class, and then called successfully

package com.example.multithreading.demo4;

/**
 * 推导lambda表达式
 */
public class lamda {
    
    

    // 3、静态内部类
    static class Like2 implements Ilike{
    
    
        @Override
        public void lambda(int a){
    
    
            System.out.println("lambda2:" + a);
        }
    }

    public static void main(String[] args) {
    
    
        Ilike like = new Like2();
        like.lambda(2);
    }
}

// 1、定义一个函数式接口
interface Ilike{
    
    
    void lambda(int a);
}

result
insert image description here

3. Local internal class (state evolution 3)

Evolved into a local inner class, found that the call is still normal

package com.example.multithreading.demo4;

/**
 * 推导lambda表达式
 */
public class lamda {
    
    

    public static void main(String[] args) {
    
    

        // 4、局部内部类
        class Like3 implements Ilike{
    
    
            @Override
            public void lambda(int a) {
    
    
                System.out.println("lambda3:" + a);
            }
        }

        Ilike like = new Like3();
        like.lambda(3);
    }
}

// 1、定义一个函数式接口
interface Ilike{
    
    
    void lambda(int a);
}

result
insert image description here

4. Anonymous inner class (state evolution 4)

After simplifying it into an anonymous inner class, the call is still successful

package com.example.multithreading.demo4;

/**
 * 推导lambda表达式
 */
public class lamda {
    
    
    
    public static void main(String[] args) {
    
    
        // 5、匿名内部类,没有类的名称,必须借助接口或者父类
        Ilike like = new Ilike() {
    
    
            @Override
            public void lambda(int a) {
    
    
                System.out.println("lambda4:" + a);
            }
        };
        like.lambda(4);

    }
}

// 1、定义一个函数式接口
interface Ilike{
    
    
    void lambda(int a);
}

result
insert image description here

5. Simplify with lambda (state evolution 5)

package com.example.multithreading.demo4;

/**
 * 推导lambda表达式
 */
public class lamda {
    
    

    public static void main(String[] args) {
    
    
        // 6、用lambda简化
        Ilike like = (int a) -> {
    
    
            System.out.println("lambda5:" + a);
        };
        like.lambda(5);

    }
}

// 1、定义一个函数式接口
interface Ilike{
    
    
    void lambda(int a);
}

result
insert image description here

6. Use lambda to simplify one (remove parameter type)

package com.example.multithreading.demo4;

/**
 * 推导lambda表达式
 */
public class lamda {
    
    

    public static void main(String[] args) {
    
    
        // 7、用lambda简化二
        Ilike like = (a) -> {
    
    
            System.out.println("lambda5:" + a);
        };
        like.lambda(6);

    }
}

// 1、定义一个函数式接口
interface Ilike{
    
    
    void lambda(int a);
}

result
insert image description here

7. Simplify two with lambda (simplify parentheses)

package com.example.multithreading.demo4;

/**
 * 推导lambda表达式
 */
public class lamda {
    
    

    public static void main(String[] args) {
    
    
        // 8、用lambda简化三
        Ilike like = a -> {
    
    
            System.out.println("lambda5:" + a);
        };
        like.lambda(7);

    }
}

// 1、定义一个函数式接口
interface Ilike{
    
    
    void lambda(int a);
}

result
insert image description here

8. Use lambda to simplify three (remove curly braces)

package com.example.multithreading.demo4;

/**
 * 推导lambda表达式
 */
public class lamda {
    
    

    public static void main(String[] args) {
    
    
        // 9、用lambda简化四
        Ilike like = a -> System.out.println("lambda5:" + a);
        like.lambda(8);
    }
}

// 1、定义一个函数式接口
interface Ilike{
    
    
    void lambda(int a);
}

result
insert image description here

9. Pay attention

A lambda expression can only be reduced to one line if it has one line of code. If there are multiple lines, it should be wrapped with a code block.
The premise is that the interface is a functional interface.
Multiple parameters can also remove the parameter type, if you want to remove it, you must remove it, and you must add parentheses.

Guess you like

Origin blog.csdn.net/qq_46106857/article/details/128148724