目录
线程池使用场景:
当并发的数量很多,并且每一个线程都是执行一个很短的任务就结束了.这样子频繁创建线程就会大大降低系统的效率,因为频繁创建和销毁都需要时间.这时我们就需要线程池了,线程池可以使得一个线程复用
线程池代码实现
线程池的使用步骤
创建Runnable接口的实现类RunnableImpl
public class RunnableImpl implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"创建了一个新的线程");
}
}
主程序
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class t1 {
public static void main(String[] args){
ExecutorService es = Executors.newFixedThreadPool(2);
es.submit(new RunnableImpl());//pool-1-thread-1创建了一个新的线程
es.submit(new RunnableImpl());//pool-1-thread-2创建了一个新的线程
es.submit(new RunnableImpl());//pool-1-thread-1创建了一个新的线程
}
Lambda表达式
函数式编程与面向对象编程区别
面向对象思想:做一件事,通过创建多个相关联的类,每个类设置自身的方法调用对象方法,完成事情
函数式编程思想:y=x+a-b+c 传入x参数返回y参数,通过传入参数返回结果完成事情
public class t1 {
public static void main(String[] args) {
//使用匿名内部类
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"创建了一个新的线程");
}
}).start();
//使用lambda表达式
new Thread(()->{
System.out.println(Thread.currentThread().getName()+"创建了一个新的线程");
}
).start();
}
}
Lambda表达式格式说明
使用场景:
Lambda表达式作用就是简化匿名内部类把内部类简化为一个()->
使用前提
案例:使用Lambda标准格式(无参数无返回)
创建一个Cook接口
public interface Cook {
void makeFood();
主程序
public class t1 {
public static void main(String[] args) {
//调用invokeCook,参数是Cokk接口,传递Cokk接口匿名内部类对象
invokeCook(new Cook() {
@Override
public void makeFood() {
System.out.println("吃饭了");
}
});
//lambda形式
invokeCook(()->{
System.out.println("在吃一回");
});
}
private static void invokeCook(Cook cook){
cook.makeFood();
}
}
案例:使用Lambda标准格式(有参数,有返回)
创建Person类
public class Person {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
主程序
import java.util.Arrays;
public class t1 {
public static void main(String[] args) {
Person[] arr={
new Person("柳岩",18),
new Person("范冰冰",20),
new Person("季童童",35),
};
Arrays.sort(arr,(Person o1,Person o2)->{
return o1.getAge()-o2.getAge();
});
for (Person person : arr) {
System.out.println(person);
}
}
}
创建Calculator接口类
public interface Calculator {
abstract int calc(int a, int b);
}
主程序
public class t1 {
public static void main(String[] args) {
invokeCalc(120,130,(int a,int b)->{
return a+b;
});
}
private static void invokeCalc(int a,int b,Calculator calulator){
int result = calulator.calc(a,b);
System.out.println("结果是:"+result);
}
}
Lambda省略模式
public class t1 {
public static void main(String[] args) {
//省略版lambda形式1:省略掉{}
new Thread(()-> System.out.println(Thread.currentThread().getName()));
//省略版lambda形式2:省略掉参数类型定义,{},return语句
Arrays.sort(arr,(o1,o2)->o1.getAge()-o2.getAge);
File类的概述
File类的静态成员变量
import java.io.File;
public class t1 {
public static void main(String[] args) {
String pathSeparator = File.pathSeparator;
System.out.println(pathSeparator);//路径分隔符;
String separator = File.separator;//文件名称分隔符\
System.out.println(separator);//windows:反斜杠\ linux:正斜杠/
}
}
绝对路径和相对路径
File类的构造方法
public class t1 {
public static void main(String[] args) {
show02();
}
private static void show02() {
File file = new File("D:\\BaiduNetdiskDownload");
System.out.println(file);//D:\BaiduNetdiskDownload
}
}
public class t1 {
public static void main(String[] args) {
show02("c:\\","a.txt");
}
private static void show02(String parent,String child) {
File file = new File(parent,child);
System.out.println(file);//c:\a.txt
}
}
父路径可不是字符串了而是File类型
public class t1 {
public static void main(String[] args) {
show02();
}
private static void show02() {
File parent = new File("c:\\");
File file = new File(parent, "hello.java");
System.out.println(file);//c:\hello.java
}
}
File类获取功能的方法
getNmae 方法可以使File类型转换成String类型
import java.io.File;
public class t1 {
public static void main(String[] args) {
show01();
}
private static void show01() {
File file = new File("D:\\BaiduNetdiskDownload\\a.txt");
String absolutePath = file.getAbsolutePath();
System.out.println(absolutePath);//D:\BaiduNetdiskDownload\a.txt
File file1 = new File("a.txt");
String absolutePath1 = file1.getAbsolutePath();
System.out.println(absolutePath1);//D:\BaiduNetdiskDownload\a.txt
}
}
import java.io.File;
public class t1 {
public static void main(String[] args) {
show02();
}
private static void show02() {
File file = new File("C:\\Users\\陈卓宇\\IdeaProjects\\untitled\\a.txt");
File file1 = new File("a.txt");
String path = file.getPath();
String path1 = file1.getPath();
System.out.println(path);
System.out.println(path1);//a.txt
}
}
import java.io.File;
public class t1 {
public static void main(String[] args) {
show03();
}
private static void show03() {
File file = new File("C:\\Users\\陈卓宇\\IdeaProjects\\untitled\\a.txt");
String name = file.getName();
System.out.println(name);//a.txt
}
}
import java.io.File;
public class t1 {
public static void main(String[] args) {
show04();
}
private static void show04() {
File file = new File("D:\\BaiduNetdiskDownload\\【非常重要】课程资料");
long length = file.length();
System.out.println(length);//4096
}
}
File类判断公共的方法
import java.io.File;
public class t1 {
public static void main(String[] args) {
show01();
}
private static void show01() {
File file = new File("C:\\Users\\陈卓宇\\IdeaProjects\\untitled\\a.txt");
System.out.println(file.exists());//false
}
import java.io.File;
public class t1 {
public static void main(String[] args) {
show01();
}
private static void show01() {
File file = new File("C:\\Users\\陈卓宇\\IdeaProjects\\untitled");
if (file.exists()){//存在获取,不存在没有必要获取
System.out.println(file.isDirectory());//true
System.out.println(file.isFile());//false
}
}
}
File类创建删除功能的方法
public class t1 {
public static void main(String[] args) throws IOException {
show01();
}
private static void show01() throws IOException {
File file = new File("C:\\Users\\陈卓宇\\IdeaProjects\\untitled\\1.txt");
boolean newFile = file.createNewFile();
System.out.println(newFile);//true
}
}
import java.io.File;
public class t1 {
public static void main(String[] args){
show01();
}
private static void show01(){
File file = new File("C:\\Users\\陈卓宇\\IdeaProjects\\untitled\\aaa");
boolean newFile = file.mkdir();
System.out.println(newFile);//true
File file1 = new File("C:\\Users\\陈卓宇\\IdeaProjects\\untitled\\aaa\\11\\cc");
boolean newFile1 = file1.mkdirs();
System.out.println(newFile);//true
}
}
import java.io.File;
public class t1 {
public static void main(String[] args) {
show01();
}
private static void show01(){
File file = new File("C:\\Users\\陈卓宇\\IdeaProjects\\untitled\\aaa");
boolean delete = file.delete();
System.out.println(delete);
}
}
File类遍历文件夹目录功能
import java.io.File;
public class t1 {
public static void main(String[] args) {
show01();
}
private static void show01(){
File file = new File("C:\\Users\\陈卓宇\\IdeaProjects\\untitled");
String[] list = file.list();
for (String s : list) {
System.out.println(s);
}
}
}
import java.io.File;
public class t1 {
public static void main(String[] args) {
show01();
}
private static void show01(){
File file = new File("C:\\Users\\陈卓宇\\IdeaProjects\\untitled");
File[] list = file.listFiles();
for (File s : list) {
System.out.println(s);
}
}
}
返回结果