函数式接口
1、Supplier
无参有返回值的接口。Supplier接口是对象实例的提供者,定义了一个名叫get的抽象方法,它没有任何入参,并返回一个泛型T对象。
@FunctionalInterface
public interface Supplier<T> {
/**
* Gets a result.
*
* @return a result
*/
T get();
}
2、Consumer
有参无返回值的接口。Consumer接口是一个类似消费者的接口,定义了一个名叫accept的抽象方法,它的入参是一个泛型T对象,没有任何返回。
@FunctionalInterface
public interface Consumer<T> {
/**
* Performs this operation on the given argument.
*
* @param t the input argument
*/
void accept(T t);
3、Function
有参有返回值的接口,Function接口是根据一个类型的数据得到另一个类型的数据,前者称为前置条件,后者称为后置条件。有参数有返回值。
@FunctionalInterface
public interface Function<T, R> {
/**
* Applies this function to the given argument.
*
* @param t the function argument
* @return the function result
*/
R apply(T t);
4、Predicate
有参,返回值为Boolean的接口。Predicate接口是判断是与否的接口,定义了一个名叫test的抽象方法,它的入参是一个泛型T对象,并返回一个boolean类型。
public interface Predicate<T> {
/**
* Evaluates this predicate on the given argument.
*
* @param t the input argument
* @return {@code true} if the input argument matches the predicate,
* otherwise {@code false}
*/
boolean test(T t);
理解:
函数式接口:只有一个方法的接口
@FunctionalInterface
public interface Runnable {
public abstract void run();
}
//超级多FunctionalInterface
//简化编程模型,在新版本的框架底层大量应用
//foreach(消费者类型的函数式接口)
Function 函数式接口
package com.example.juc.function;
import java.util.function.Function;
/**
* @author swh
* @date 2022/7/28 14:19
*/
/*
* Function函数型接口
* 有一个输入参数,有一个输出
* (只要是函数式接口,都可以用lambda表达式简化)
* */
public class Demo01 {
public static void main(String[] args) {
//
/* Function function = new Function<String,String>() {
@Override
public String apply(String s) {
return s;
}
};*/
Function<String,String> function =(s)->{return s;};
}
}
Predicate 断定型接口
package com.example.juc.function;
import java.util.function.Predicate;
/**
* @author swh
* @date 2022/7/28 14:29
*/
/*
* 断定型接口
* 有一个输入参数,返回值只能是布尔值
* */
public class Demo02 {
public static void main(String[] args) {
//判断字符串是否为空
/* Predicate<String> predicate = new Predicate<String>() {
@Override
public boolean test(String str) {
return str.isEmpty();
}
};*/
Predicate<String> predicate =str->{ return str.isEmpty();};
System.out.println(predicate.test("s"));
}
}
Consumer 消费型接口
package com.example.juc.function;
import java.util.function.Consumer;
/**
* @author swh
* @date 2022/7/28 14:39
*/
/*
* 消费型接口:只有输入,没有返回值
* */
public class Demo03 {
public static void main(String[] args) {
/* Consumer<String> consumer = new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println(s);
}
};*/
Consumer<String> consumer=s->{
System.out.println(s);
};
consumer.accept("ss");
}
}
Supplier 供给型接口
package com.example.juc.function;
import java.util.function.Supplier;
/**
* @author swh
* @date 2022/7/28 14:45
*/
/*
* 供给型接口
* 没有参数,只有返回值
* */
public class Demo04 {
public static void main(String[] args) {
/* Supplier supplier = new Supplier<String>() {
@Override
public String get() {
return "123";
}
};*/
Supplier supplier =()->{return "123";};
System.out.println(supplier.get());
}
}
任何一个新的技术,绝对不是仅仅只是覆盖了原来的技术,优势和补充!
Lock 接口
公平锁: 十分公平;可以先来后到
非公平锁: 十分不公平;可以插队(默认非公平锁)
Callable:
与Runnable的不同
- 可以有返回值
- 可以抛出异常
- 方法不同,run() | call()
- 有缓存
- 结果可能需要等待,会阻塞!