Executors一篇就够

article/2025/9/9 2:22:25

Executors框架包含的内容十分的多:看图:
在这里插入图片描述


一、各个接口的作用

按照图示关系进行介绍:
在这里插入图片描述
Executor

该接口作为顶层接口只有一个execute()方法

execute(Runnable r)
该接口接受一个Runnable实例,即要执行的任务

ExecutorService

该接口继承了Executor接口,增加了非常多的功能

ExecutorService接口提供了很多的方法,比如:
shutdown();  线程池的关闭
ExecutorService的三种状态:
1.运行
2.关闭
3.终止
-------------------------------------------------
运行状态:只要线程池一直不调用shutdown的话,那么线程池是会一直运行下去的
关闭状态:线程池不再接受新的任务,对于已经提交的任务会处理完毕
终止状态:调用shutdown()方法后已提交的任务处理完之后线程池处于终止状态

ThreadPoolExecutor
线程池的实现类,最常用的类。实现了ExecutorService中所有的方法

ScheduleThreadPoolExecutor

该类继承了ThreadPoolExecutor并实现了ScheduledExecutorService接口
该类主要用来延时执行任务,或定时执行任务。

二、ScheduleThreadPoolExecutor的使用

1.schedule方法

该方法可以用来延迟任务的执行

延迟执行任务一次:public ScheduledFuture<?> schedule(Runnable command,long delay, TimeUnit unit)参数:
command:执行的任务
delay:延迟的时间
unit:枚举类,表示时间的单位可以是时分秒……

案例:

public class ScheduleThreadPoolExecutorTest {public static void main(String[] args) {System.out.println("当前系统时间"+System.currentTimeMillis());//使用该构造方法默认最大核心线程数为Interger_MaxScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(5);executor.schedule(new Runnable() {@Overridepublic void run() {System.out.println("开始执行时间"+System.currentTimeMillis());}},2, TimeUnit.SECONDS);//延迟两秒再执行executor.shutdown();}
}

在这里插入图片描述
运行该程序时,你会发现先打印了当前系统时间,过了两秒后才打印出开始执行


2.固定频率执行任务

scheduleAtFixedRate()方法

public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,long initialDelay,long period,TimeUnit unit);
参数:
command:执行的任务
initialDelay:首次执行的延迟时间
period:表示每次间隔的时间
unit:枚举类是上述两个时间的单位

案例:

import java.sql.SQLOutput;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;public class ScheduleThreadPoolExecutorTest {public static void main(String[] args) {System.out.println("2秒后开始打印当前时间"+getNowTime(System.currentTimeMillis()));ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(100);executor.scheduleAtFixedRate(new Runnable() {@Overridepublic void run() {System.out.println("当前时间"+getNowTime(System.currentTimeMillis()));try {//这里休眠是为了和后续的方法做比较Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}},2,1,TimeUnit.SECONDS);}public static String getNowTime(Long time){return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);}
}

打印结果:
在这里插入图片描述

3.固定的间隔时间执行任务

scheduleWithFixedDelay

public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,long initialDelay,long period,TimeUnit unit);
参数:
command:执行的任务
initialDelay:首次执行的延迟时间
period:表示每次间隔的时间
unit:枚举类是上述两个时间的单位

代码如下(示例):

import java.sql.SQLOutput;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;public class ScheduleThreadPoolExecutorTest {public static void main(String[] args) {System.out.println("2秒后开始打印当前时间"+getNowTime(System.currentTimeMillis()));ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(100);executor.scheduleWithFixedDelay(new Runnable() {@Overridepublic void run() {System.out.println("当前时间"+getNowTime(System.currentTimeMillis()));try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}},2,1,TimeUnit.SECONDS);}public static String getNowTime(Long time){return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);}
}

在这里插入图片描述
scheduleAtFixedRate和scheduleWithFixedDelay两者的区别

scheduleAtFixedRate的下一次执行时间是上一次执行时间+间隔时间
scheduleWithFixedDelay下一次执行时间是上一次执行时间结束时系统时间+间隔时间。
scheduleWithFixedDelay时间不固定但是周期固定

所以第一个案例下一次执行时间间隔了1秒,而第二个案例中每一次任务内部执行了1秒,所以下一次的执行时间就是2秒。

4.定时任务出现异常

如果说scheduleAtFixedRate和scheduleWithFixedDelay在执行时出现异常会怎么样?

案例:

import java.sql.SQLOutput;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;public class ScheduleThreadPoolExecutorTest {public static void main(String[] args) {System.out.println("2秒后开始打印当前时间"+getNowTime(System.currentTimeMillis()));ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(100);ScheduledFuture<?> scheduledFuture = executor.scheduleWithFixedDelay(new Runnable() {@Overridepublic void run() {System.out.println("当前时间" + getNowTime(System.currentTimeMillis()));try {Thread.sleep(1000);//制造异常int i = 2 / 0;} catch (InterruptedException e) {e.printStackTrace();}}}, 2, 1, TimeUnit.SECONDS);}public static String getNowTime(Long time){return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);}
}

我们手动只要了一个异常:看一下结果
在这里插入图片描述
程序执行到异常的时候不在执行了。

当我们在使用ScheduledExecutorService接口的实现类处理任务的时候,如果一旦发生异常,就会被ScheduledExecutorService接口内部进行捕获。因此我们在发现程序不正确的时候需要即时查找原因。不然程序停止了也没有异常信息的打印。

5.取消定时任务的执行

我们在使用ScheduleThreadPoolExecutor执行任务时都会返回ScheduledFuture的。我们可以通过方法进行发送中断信号来取消定时任务

 boolean cancel(boolean mayInterruptIfRunning) 试图取消对此任务的执行。  boolean isCancelled() 如果在任务正常完成前将其取消,则返回 trueboolean isDone() 如果任务已完成,则返回 true

案例:
5秒后取消定时任务:

import java.sql.SQLOutput;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;public class ScheduleThreadPoolExecutorTest {public static void main(String[] args) {System.out.println("2秒后开始打印当前时间"+getNowTime(System.currentTimeMillis()));ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(100);ScheduledFuture<?> scheduledFuture = executor.scheduleAtFixedRate(new Runnable() {@Overridepublic void run() {System.out.println("当前时间" + getNowTime(System.currentTimeMillis()));try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}, 2, 1, TimeUnit.SECONDS);try {Thread.sleep(5000);//取消定时任务scheduledFuture.cancel(true);if(scheduledFuture.isDone()){System.out.println("任务完成");}if(scheduledFuture.isCancelled()){System.out.println("任务取消");}} catch (InterruptedException e) {e.printStackTrace();}}public static String getNowTime(Long time){return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);}
}

结果:
在这里插入图片描述

Executors类的使用

Executors类提供一些创建线程池的方法,线程池都实现了ExecutorService接口

newSingleThreadExecutor()

看一下源码

 public static ExecutorService newSingleThreadExecutor() {return new FinalizableDelegatedExecutorService(new ThreadPoolExecutor(1, 1, 0L,TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>()));}
该方法创建单线程的线程池(从源码中可以看到核心线程数是1,最大线程数也是1)!
工作队列采用的是无界的LinkedBlockingQueue阻塞队列。支持先提交的先执行!
如果核心线程异常的话,则创建一个线程去顶替核心线程(但始终保持单线程)
如果阻塞队列中任务太多,单线程处理不完则会引发OOM

newFixedThreadPool(int nThreads)
源码:

public static ExecutorService newFixedThreadPool(int nThreads) {return new ThreadPoolExecutor(nThreads, nThreads,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());}
从源码中我们可以看出:该方法会创建一个固定大小的线程池。
核心线程数和最大线程数一致就表明:
当接受一个任务后就创建一个线程直至达到最大线程数。此时会将任务加入工作队列中
工作队列采用的是无界的阻塞队列,支持先提交的先执行。若处理不过来会引发OOM

newCachedThreadPool ()

源码:

    public static ExecutorService newCachedThreadPool() {return new ThreadPoolExecutor(0, Integer.MAX_VALUE,60L, TimeUnit.SECONDS,new SynchronousQueue<Runnable>());}
创建一个可缓存的线程池,如果线程池的大小超多处理任务的线程,
那么就会回收空闲线程。当先提交一个线程时便会创建一个线程去处理。
该线程池的最大线程数默认为Integer.MAX_VALUE
内部采用SynchronousQueue队列,该队列要求只有线程获取任务的话才能加入队列中

newScheduledThreadPool(int corePoolSize)

    public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {return new ScheduledThreadPoolExecutor(corePoolSize);}//创建源码:super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,new DelayedWorkQueue());
该线程池创建的是支持定时任务的线程池.核心线程数指定,最大线程数为Interger.MAX_VALUE
内部使用的是:DelayedWorkQueue无界优先级阻塞队列。要求元素都实现 Delayed 接口

Future、Callable接口

Future接口提供了一些异步执行的方法:比如:异步获取执行结果、取消任务执行,判断任务是否取消,判断任务是否结束

boolean cancel(boolean mayInterruptIfRunning) 试图取消对此任务的执行。 V get() 如有必要,等待计算完成,然后获取其结果。 V get(long timeout, TimeUnit unit) 如有必要,最多等待为使计算完成所给定的时间之后,获取其结果(如果结果可用)。 boolean isCancelled() 如果在任务正常完成前将其取消,则返回 trueboolean isDone() 如果任务已完成,则返回 true

Callable接口:返回结果并且可能抛出异常的任务

 V call() 计算结果,如果无法计算结果,则抛出一个异常。 

注意:返回值类型是一个泛型,若无法计算结果抛出异常。

案例:

import java.util.concurrent.*;public class ExecutorServiceTest {public static void main(String[] args) {//创建一个线程池ExecutorService executorService = Executors.newFixedThreadPool(1);Future<Integer> future = executorService.submit(new Callable<Integer>() {@Overridepublic Integer call() throws Exception {int j = 0;for (int i = 0; i < 5; i++) {Thread.sleep(1000);j++;}return j;            }});try {System.out.println("获取执行的结果"+future.get());executorService.shutdown();} catch (InterruptedException e) {e.printStackTrace();} catch (ExecutionException e) {e.printStackTrace();}}
}
上述代码中我们使用submit()提交任务,该方法接受一个Callable的实例
返回一个Future的实例。我们可以通过该实例来进行获取结果,取消任务等操作。

结果:
在这里插入图片描述

注意:
get()方法会导致当前线程阻塞直至线程计算完结果后,才继续执行。但是我们可以设置超时时间。

get方法设置超时时间:
表明我获取结果只能等待一定的时间
在这里插入图片描述
设置等待超时时间的话会抛出TimeOutException异常

FutureTask实现类

FutureTask实现了RunnableFuture接口,而RunnableFuture接口继承了Runnable接口和Callable接口。因此该实现类可以交给Excutor或者直接使用线程的方法执行即可。

线程池的submit方法返回的Future实际类型正是FutureTask对象

FutureTask(Callable<V> callable) 创建一个 FutureTask,一旦运行就执行给定的 Callable。 
FutureTask(Runnable runnable, V result) 创建一个 FutureTask,一旦运行就执行给定的 Runnable,并安排成功完成时 get 返回给定的结果 。 

案例:

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;public class FutureTaskTest {public static void main(String[] args) {FutureTask<Integer> future = new FutureTask(new Callable<Integer>() {@Overridepublic Integer call() throws Exception {return 10;}});try {new Thread(future).start();System.out.println("获取返回的结果"+future.get());} catch (InterruptedException e) {e.printStackTrace();} catch (ExecutionException e) {e.printStackTrace();}}
}

问题分析:

获取线程池中最先处理结束的任务

上面我们采用Fucture可能会出现的问题:

import java.util.concurrent.*;public class FutureTaskTest {public static void main(String[] args) {ExecutorService executorService = Executors.newFixedThreadPool(5);Future<String> future1 = executorService.submit(new Callable<String>() {@Overridepublic String call() throws Exception {Thread.sleep(3000);return "该任务耗时3秒完成";}});Future<String> future2 = executorService.submit(new Callable<String>() {@Overridepublic String call() throws Exception {Thread.sleep(7000);return "该任务耗时7秒完成";}});try {System.out.println(future2.get());long start = System.currentTimeMillis();System.out.println(future1.get());long end =System.currentTimeMillis();System.out.println((end-start)/1000);if(future1.isDone() && future2.isDone()){executorService.shutdown();}} catch (InterruptedException e) {e.printStackTrace();} catch (ExecutionException e) {e.printStackTrace();}}
}

在这里插入图片描述
调整顺序:

            System.out.println(future1.get());long start = System.currentTimeMillis();System.out.println(future2.get());long end =System.currentTimeMillis();System.out.println((end-start)/1000);

结果:
在这里插入图片描述
上述问题我们发现顺序不同会导致不一样的结果,为什么会出现这种结果呢?就是因为调用get()方法会一直等到获取结果。因此第一此结果是由于future2任务需要的时间为7秒,而future2的任务处理时间较短3秒。此时先调用future2的get将会导致阻塞。因此start执行的时候几乎适合end一块执行的。而反之,先执行future1的话调用get结束后start就有值了,此时future2还没有执行结束,这样就会导致时间差。我们把耗时较短的放在前边这样就可以尽快的执行后续的代码,而不会因为另外的任务处理较长而等待。

上述案例我们手动设置的,但是实际中我们不知道那个任务处理的时间长,因此需要用到特殊的方法来避免任务等待的耗时。接下的CompletionService就是解决这个问题的。

Future<V> submit(Callable<V> task);用于向服务中提交有返回结果的任务,并返回Future对象Future<V> submit(Runnable task, V result);用户向服务中提交有返回值的任务去执行,并返回Future对象Future<V> take() throws InterruptedException;从服务中返回并移除一个已经完成的任务,如果获取不到,会一致阻塞到有返回值为止。此方法会响应线程中断。Future<V> poll();从服务中返回并移除一个已经完成的任务,如果内部没有已经完成的任务,则返回空,此方法会立即响应。Future<V> poll(long timeout, TimeUnit unit) throws InterruptedException;尝试在指定的时间内从服务中返回并移除一个已经完成的任务,等待的时间超时还是没有获取到已完成的任务,则返回空。此方法会响应线程中断

ExecutorCompletionService是CompletionService接口的实现类,创建的时候传递一个线程池

 public ExecutorCompletionService(Executor executor) {if (executor == null)throw new NullPointerException();this.executor = executor;this.aes = (executor instanceof AbstractExecutorService) ?(AbstractExecutorService) executor : null;this.completionQueue = new LinkedBlockingQueue<Future<V>>();}
当我们调用submit()方法时会使用传递进来的线程池去处理任务。
他的内部有一个阻塞队列,完成任务的对象会放入该阻塞队列中。
使用take或者poll方法将会取出已经完成的任务对象。因此先完成的先取出
import java.util.concurrent.*;public class FutureTaskTest {public static void main(String[] args) {ExecutorService executor = Executors.newFixedThreadPool(5);ExecutorCompletionService<String> executorService = new ExecutorCompletionService<>(executor);Future<String> future1 = executorService.submit(new Callable<String>() {@Overridepublic String call() throws Exception {Thread.sleep(3000);return "该任务耗时3秒完成";}});Future<String> future2 = executorService.submit(new Callable<String>() {@Overridepublic String call() throws Exception {Thread.sleep(7000);return "该任务耗时7秒完成";}});try {for (int i = 0; i < 2; i++) {//会先获取执行结束的线程System.out.println(executorService.take().get());}if(future1.isDone() && future2.isDone()){executor.shutdown();}} catch (InterruptedException e) {e.printStackTrace();} catch (ExecutionException e) {e.printStackTrace();}}
}

通过该实例主要可以得知那些任务处理完毕的一个顺序!


http://chatgpt.dhexx.cn/article/3AOX81tg.shtml

相关文章

Executors工具类的相关方法

前言&#xff1a;大家好&#xff0c;我是小威&#xff0c;24届毕业生。本篇将记录创建线程池的Executors工具类里面的方法&#xff0c;方便加深知识印象和复习使用。 本篇文章记录的基础知识&#xff0c;适合在学Java的小白&#xff0c;也适合复习中&#xff0c;面试中的大佬&a…

java并发编程:Executor、Executors、ExecutorService

Executors 在Java 5之后&#xff0c;并发编程引入了一堆新的启动、调度和管理线程的API。Executor框架便是Java 5中引入的&#xff0c;其内部使用了线程池机制&#xff0c;它在java.util.cocurrent 包下&#xff0c;通过该框架来控制线程的启动、执行和关闭&#xff0c;可以简化…

nlinfit非线性回归拟合

% % 使用指定函数对下述两变量进行曲线拟合 % % yak1*exp(m*t)k2*exp(-m*t); % % 离散点: t[0,4,8,40], % % y[20.09,64.52,85.83,126.75]; % % t-自变量 y-因变量 a,m,k1,k2为常数 % % 用非线性回归nlinfit&#xff0c;如果数据点多些&#xff0c;效果会更好。 脚本&…

matlab的nlinfit函数,用matlab如何进行非线性拟合 nlinfit函数?

用非线性回归nlinfit&#xff0c;如果数据点多些&#xff0c;效果会更好。 function nonlinefit clc;clear; t[0 4 8 40]; y[20.09 64.52 85.83 126.75]; betanlinfit(t,y,myfunc,[1 1 1 1]) abeta(1) k1beta(2) k2beta(3) mbeta(4) tt0:1:40 yyak1*exp(m*tt)k2*exp(-m*tt) plo…

【MATLAB统计分析与应用100例】案例013:matlab读取Excel数据,调用nlinfit函数作一元非线性回归

1. 一元线性回归分析效果预览 2. matlab完整实现代码 %读取数据,绘制散点图** HeadData = xlsread(examp08_02.xls); %从Excel文

matlab中用polyfit、regress、nlinfit等进行详细的回归分析

目录 1.说明2.回归的介绍2-1.前面两篇所发现的一些问题2-1-1.回归和拟合是什么关系?2-1-2.回归到底是做预测还是用来去脏数据?3.三个函数的核心:最小二乘法3-1.介绍3-2.matlab代码4.函数polyfit(线性)5.函数regress(线性)5-1.输出b,bint,r,rint,stats5-2.应用5.2-1.一元…

曲线拟和函数lsqcurvefit nlinfit

转载自&#xff1a;http://panda0411.com/2011/08/29/curve-fit-and-function-lsqcurvefitnlinfit/ 琢磨了好久matlab自带的曲线拟和工具箱, 发现这货只能解决从离散数据得到各种类型的拟和效果, 但是反之貌似没法实现, google一下有这两个函数可以用:lsqcurvefit和nlinfit ls…

Matlab学习手记——非线性数据拟合:nlinfit和lsqcurvefit

目的&#xff1a;通过一个实例了解Matlab的数据拟合函数nlinfit和lsqcurvefit的使用。 结果图 具体数值 p 0.3000 50.0000 0.4000 200.0000 0.3000 800.0000 p1 0.3267 48.3589 0.4030 226.6525 0.2838 809.6680 p2 0.3267 48.3646 0.4031 226.735…

MATLAB多元非线性回归nlinfit拟合圆拟合球拟合函数

先上实验效果&#xff0c;你觉得有帮助可以继续阅读。代码解析在B站有上传视频&#xff08;用户昵称同名&#xff09;&#xff0c;代码也有详细备注。 拟合圆和球面&#xff1a; 拟合多元非线性函数&#xff1a;y p1*x1p2*x1^2p3*x2p4*x2^2exp(-p5*x3)的拟合结果&#xff1a;…

MATLAB 非线性隐函数拟合采坑记录(使用 fsolve solve nlinfit lsqcurvefit函数)

MATLAB 非线性隐函数拟合采坑记录&#xff08;使用 fsolve solve nlinfit lsqcurvefit函数&#xff09; 问题描述解决思路错误示范1代码思路原因解释模型更正更正模型1更正模型2 错误示范2代码思路原因解释模型更正更正模型1更正模型2 总结 问题描述 MATLAB的 nlinfit 和 lsqc…

Matlab多元非线性函数拟合

看了多篇文章&#xff0c;觉得没有一篇比较全&#xff0c;且可以参照的多元非线性函数拟合&#xff0c;看了多篇文章后总结以下内容&#xff0c;主要以示例给出&#xff0c;希望能帮助到大家快速上手。 1.需要用到的函数语法 beta nlinfit(X, Y, modelfun, beta0) X为你的自…

MATLAB中用nlinfit做多元非线性拟合(回归)

MATLAB中有一个多元非线性拟合的功能是nlinfit 基本语法是&#xff1a; beta nlinfit(X,Y,modelfun,beta0) 式子左边的beta可以是一个向量&#xff0c;向量的元素就是要回归的模型中的参数。 式子右边&#xff0c;modelfun是要回归的函数形式。X是函数的自变量数据&#xff1b…

使用nlinfit函数进行拟合时出现Error using nlinfit>checkFunVals (line 611)

在使用nlinfit函数进行拟合时出错&#xff0c;内容如下&#xff1a; The function you provided as the MODELFUN input has returned Inf or NaN values.从第一行可以看出&#xff0c;由于赋予的初始值导致了函数生成了NaN&#xff08;无解&#xff09;&#xff0c;所以整个回…

【数学建模】多元非线性回归nlinfit(Matlab代码实现)

目录 1 基本语法 2 算例及Matlab代码实现 2.1 算例 2.2 数据 2.3 Matlab代码实现 1 基本语法 2 算例及Matlab代码实现 2.1 算例 熔喷非织造材料是口罩生产的重要原材料&#xff0c;具有很好的过滤性能&#xff0c;其生产工艺简单、成本低、质量轻等特点&#xff0c;受到国…

Matlab 使用nlinfit 函数进行多元非线性回归,并且绘制曲线拟合的误差区间

Matlab 使用nlinfit 函数进行多元非线性回归&#xff0c;并且绘制曲线拟合的误差区间 一、前言二、nlinfit函数使用1、函数语法2、拟合示例&#xff1a; 三、误差阴影绘制四、整体源码五、思考参考博客 一、前言 这个也是最近我接到的一个小项目里的内容&#xff1a; 有一组数…

利用nlinfit函数实现数据非线性拟合

所谓“拟合”&#xff0c;指的是在已有一组实验数据的前提下&#xff0c;研究这组数据有怎样的函数关系——最终结果是从这一组看似漫无规律的数据点中“找出”能用数学表达式表示的规律。 用数学语言描述的拟合定义如下&#xff1a; 一个典型的数据拟合过程包括以下几个步骤&…

Matlab非线性拟合函数——nlinfit

我们平时最常用的非线性拟合函数还是多项式拟合,有一天学弟突然问了我nlinfit 这个函数,然后直接查询matlab官方文档,原来非线性函数还可以用这个函数,下面来看看matlab官方文档的说明: 英文?没关系,下面看一下中文用法: beta = nlinfit(X, Y, modelfun, beta0) beta:…

[MATLAB]非线性回归--自配函数(nlinfit)

当谈到非线性回归模型的时候&#xff0c;同学们应该紧密的将线性回归紧密结合在一起&#xff0c;因为非线性回归很容易过拟合。那我们从一个案例谈一下非线性 拿到题目看到一个变量x一个y&#xff0c;非线性问题步骤应该是这样子的&#xff1a; 画出散点图根据散点图确定须配…

dozer使用: list对象mapping 配置

记录dozer的使用&#xff0c;复杂类型配置。 文档&#xff1a;https://dozermapper.github.io/user-guide.pdf 参考地址&#xff1a;https://github.com/klvnnsrikanth/DozerMappingExample.git Demo 的目录结构&#xff1a; 不是集合的普通映射方式 Source 类: Destinatio…

java dozer map转对象_对象转换利器之Dozer

在Java的世界中&#xff0c;经常会涉及到需要在2个对象中进行转换&#xff0c;比如说&#xff1a; 调用SOAP Web服务&#xff0c;需要把自己的Domain对象转换为Soap服务的Jaxb对象请求&#xff0c; 在分层级SOA架构中&#xff0c;2个层级之间Domain对象的转换&#xff0c; 在分…