当前位置:网站首页>多线程——Callable接口,lambda
多线程——Callable接口,lambda
2022-07-25 09:27:00 【看小虫子】
Callable的好处
1,可以定义返回值
2,可以抛出异常
实现接口的四大步骤
1,创建执行服务: ExecutorService ser= Executors.newFixedThreadPool(3);
2,提交执行: Future result1=ser.submit(th1);
3,获取结果: boolean rs1= result1.get();
4,关闭服务:ser.shutdownNow();
ackage CallableDemo;
import ThreadDemo.ThreadDemo01;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.*;
public class Demo01 implements Callable<Boolean> {
private String url; //网络图片的地址
private String name;//保存的文件名
public Demo01(String url, String name) {
this.url = url;
this.name = name;
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
Demo01 th1=new Demo01("https://img2.baidu.com/it/u=1814268193,3619863984&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1658682000&t=d20c88a4eebf882f4f8950f8613ac1b4","n.jpg");
Demo01 th2=new Demo01("https://img2.baidu.com/it/u=1814268193,3619863984&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1658682000&t=d20c88a4eebf882f4f8950f8613ac1b4","m.jpg");
Demo01 th3=new Demo01("https://img2.baidu.com/it/u=1814268193,3619863984&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1658682000&t=d20c88a4eebf882f4f8950f8613ac1b4","p.jpg");
//创建执行服务
ExecutorService ser= Executors.newFixedThreadPool(3);
//提交执行
Future<Boolean> result1=ser.submit(th1);
Future<Boolean> result2=ser.submit(th2);
Future<Boolean> result3=ser.submit(th3);
//获取结果
boolean rs1= result1.get();
boolean rs2= result2.get();
boolean rs3= result3.get();
System.out.println(rs1);
//关闭服务
ser.shutdown();
}
//下载图片线程的执行体
@Override
public Boolean call() throws Exception {
webDownLoader web=new webDownLoader();
web.downloader(url,name);
System.out.println("下载了文件名为"+name);
return true;
}
//下载器
class webDownLoader {
//下载方法
public void downloader(String url,String name){
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
} catch (IOException e) {
e.printStackTrace();
System.out.println("IO异常"+"downLoader出现问题");
}
}
}
}
lambda表达式
为什么要用lambda表达式
避免匿名内部类定义过多
让代码更简洁
任何接口,只包含唯一一个抽象方法,那么它就是函数式接口
对于函数式接口我们可以通过lambda表达式创建接口的对象
package LamdaStudy;
public class Demo01 {
public static void main(String[] args) {
ILike like = new Like();
like.lambda();
like=new Like2();
like.lambda();
//4,局部内部类
class Like3 implements ILike{
@Override
public void lambda() {
System.out.println("I Like Lambda3");
}
}
like=new Like3();
like.lambda();
//5,匿名内部类,没有类的名称,必须借助接口或者父类
like =new ILike() {
@Override
public void lambda() {
System.out.println("I Like Lambda4");
}
};
//使用匿名内部类
like.lambda();
//6,用lambda简化
like=()->{
System.out.println("I Like Lambda5");
};
like.lambda();
}
//3,静态内部类
static class Like2 implements ILike{
@Override
public void lambda() {
System.out.println("I Like Lambda2");
}
}
}
//接口
interface ILike {
void lambda();
}
//2,实现类
class Like implements ILike{
@Override
public void lambda() {
System.out.println("I Like Lambda");
}
}
lambda表达式简化
//总结
//1,有一个参数的时候,可以省略小括号,也可以省去参数类型
//2,有多个参数的时候,小括号不可省略,但参数类型两个必须是都存在或都不存在
//3,方法体只有一句语句的时候,可以省略大括号,但是多条语句下不可省略
package LamdaStudy;
public class Demo02 {
public static void main(String[] args) {
ILove love=new Love();
love.Love(12);
//匿名内部类
love=new ILove() {
@Override
public void Love(int a) {
System.out.println("I Love You"+a);
}
};
love.Love(2);
//1,lambda表达式
ILove love1=(int a)->{
System.out.println("I Love You"+a);
};
love1.Love(3);
//2,简化1去掉参数类型
love=(a)->{
System.out.println("I Love You"+a);
};
love.Love(5);
//3,简化括号()
love = a->{
System.out.println("I Love You"+a);
};
love.Love(6);
//4,简化3去掉{}
love = a->System.out.println("I Love You"+a);
love.Love(7);
}
interface ILove{
void Love(int a);
}
class Love implements ILove{
@Override
public void Love(int a) {
System.out.println("I Love You"+a);
}
}
边栏推荐
- 记录一些JS工具函数
- Vant problem record
- See how a junior student of double non-2 (0 Internship) can get an offer from Alibaba and Tencent
- CentOs安装redis
- 【专栏】RPC系列(理论)-夜的第一章
- VLAN的配置及其应用(以华为eNSP为例)
- message from server: “Host ‘xxx.xxx.xxx.xxx‘ is not allowed to connect to this MySQL server“
- nodejs链接mysql报错:ER_NOT_SUPPORTED_AUTH_MODEError: ER_NOT_SUPPORTED_AUTH_MODE
- Introduction to armv8 general timer
- Probabilistic robot learning notes Chapter 2
猜你喜欢

【近万字干货】别让你的简历配不上你的才华——手把手教你制作最适合你的简历

emmet语法速查 syntax基本语法部分

拷贝过来老的项目变成web项目
![[recommended collection] with these learning methods, I joined the world's top 500 - the](/img/95/e34473a1628521d4b07e56877fcff1.png)
[recommended collection] with these learning methods, I joined the world's top 500 - the "fantastic skills and extravagance" in the Internet age

UE4源码的获取和编译

ROS分布式操作--launch文件启动多个机器上的节点

车辆属性最近一次入库时间初始化生成sql脚本文件

Reflection 反射

概率论与数理统计 4 Continuous Random Variables and Probability Distributions(连续随机变量与概率分布)(上篇)
![严重 [main] org.apache.catalina.util.LifecycleBase.handleSubClassException 初始化组件](/img/39/6f6760e80acec0b02028ea2ed1a5b1.png)
严重 [main] org.apache.catalina.util.LifecycleBase.handleSubClassException 初始化组件
随机推荐
VCs common commands
概率论与数理统计 3 Discrete Random Variables and Probability Distributions(离散随机变量与概率分布) (下篇)
message from server: “Host ‘xxx.xxx.xxx.xxx‘ is not allowed to connect to this MySQL server“
[recommended collection] with these learning methods, I joined the world's top 500 - the "fantastic skills and extravagance" in the Internet age
安装 oh my zsh
Is binary cross entropy really suitable for multi label classification?
Advanced introduction to digital IC Design SOC
小程序企业发放红包功能
【建议收藏】靠着这些学习方法,我入职了世界五百强——互联网时代的“奇技淫巧”
mysql 解决不支持中文的问题
用户喜好
VLAN的配置及其应用(以华为eNSP为例)
一文学会,三款黑客必备的抓包工具教学
Detailed explanation of JDBC operation database
nodejs链接mysql报错:ER_NOT_SUPPORTED_AUTH_MODEError: ER_NOT_SUPPORTED_AUTH_MODE
Internal structure of SOC chip
MVC three-tier architecture understanding
链表相关(设计链表及环链表问题)
看一个双非二本(0实习)大三学生如何拿到阿里、腾讯的offer
严重 [main] org.apache.catalina.util.LifecycleBase.handleSubClassException 初始化组件