当前位置:网站首页>Multithreading -- callable interface, lambda
Multithreading -- callable interface, lambda
2022-07-25 10:16:00 【Look at the bugs】
Callable The benefits of
1, You can define the return value
2, You can throw an exception
Four steps to implement the interface
1, Create execution service : ExecutorService ser= Executors.newFixedThreadPool(3);
2, Submit for execution : Future result1=ser.submit(th1);
3, To get the results : boolean rs1= result1.get();
4, Close the service :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; // Address of network picture
private String name;// Saved file 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");
// Create execution service
ExecutorService ser= Executors.newFixedThreadPool(3);
// Submit for execution
Future<Boolean> result1=ser.submit(th1);
Future<Boolean> result2=ser.submit(th2);
Future<Boolean> result3=ser.submit(th3);
// To get the results
boolean rs1= result1.get();
boolean rs2= result2.get();
boolean rs3= result3.get();
System.out.println(rs1);
// Close the service
ser.shutdown();
}
// Download the execution of the image thread
@Override
public Boolean call() throws Exception {
webDownLoader web=new webDownLoader();
web.downloader(url,name);
System.out.println(" Downloaded the file named "+name);
return true;
}
// Downloader
class webDownLoader {
// Download method
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 abnormal "+"downLoader Problems arise ");
}
}
}
}
lambda expression
Why use lambda expression
Avoid too many anonymous inner class definitions
Make the code simpler
Any interface , Contains only one abstract method , So it's a functional interface
For functional interfaces, we can use lambda Expression creates the object of the interface
package LamdaStudy;
public class Demo01 {
public static void main(String[] args) {
ILike like = new Like();
like.lambda();
like=new Like2();
like.lambda();
//4, Local inner classes
class Like3 implements ILike{
@Override
public void lambda() {
System.out.println("I Like Lambda3");
}
}
like=new Like3();
like.lambda();
//5, Anonymous inner class , There is no class name , You have to use an interface or a parent class
like =new ILike() {
@Override
public void lambda() {
System.out.println("I Like Lambda4");
}
};
// Use anonymous inner class
like.lambda();
//6, use lambda simplify
like=()->{
System.out.println("I Like Lambda5");
};
like.lambda();
}
//3, Static inner class
static class Like2 implements ILike{
@Override
public void lambda() {
System.out.println("I Like Lambda2");
}
}
}
// Interface
interface ILike {
void lambda();
}
//2, Implementation class
class Like implements ILike{
@Override
public void lambda() {
System.out.println("I Like Lambda");
}
}
lambda Expression simplification
// summary
//1, When there is a parameter , You can omit parentheses , Parameter types can also be omitted
//2, When there are multiple parameters , Parentheses cannot be omitted , But both parameter types must exist or not exist
//3, When the method body has only one sentence , You can omit the braces , However, multiple statements cannot be omitted
package LamdaStudy;
public class Demo02 {
public static void main(String[] args) {
ILove love=new Love();
love.Love(12);
// Anonymous inner class
love=new ILove() {
@Override
public void Love(int a) {
System.out.println("I Love You"+a);
}
};
love.Love(2);
//1,lambda expression
ILove love1=(int a)->{
System.out.println("I Love You"+a);
};
love1.Love(3);
//2, simplify 1 Remove parameter type
love=(a)->{
System.out.println("I Love You"+a);
};
love.Love(5);
//3, Simplified brackets ()
love = a->{
System.out.println("I Love You"+a);
};
love.Love(6);
//4, simplify 3 Get rid of {}
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);
}
}
边栏推荐
猜你喜欢
随机推荐
Selenium 等待元素出现与等待操作可以执行的条件
message from server: “Host ‘xxx.xxx.xxx.xxx‘ is not allowed to connect to this MySQL server“
数论---最大公约数最小公倍数
拷贝过来老的项目变成web项目
TCP传输
Swing组件之单选与多选按钮
UE4源码的获取和编译
Output stream in io stream
史上最全面的UE4 文件操作,打开,读、写,增、删、改、查
mongoDB的使用
Swing组件
Small knowledge of common classes
语音自监督预训练模型 CNN Encoder 调研总结
Angr(七)——angr_ctf
IO流中的输入流
1、 Initial mysql, MySQL installation, environment configuration, initialization
Input stream in io stream
构建 Dompteur 容器问题小记
四舍五入取近似值
静态路由的配置(以华为eNSP为例)









