在上一篇文章中,“使用多螺纹式Pailaering的完整形式”,如何使用多线程并发操作,但是发行的子线程的数量是一定值,该值是在代码级别固定的。子线程没有固定,那么先前的用法无法继续。目前,您需要更改用法。
基本思想是:将所有子线程任务放在整个周期中,并根据业务场景选择不同的方法:
业务方案:根据上传的多个行政区编码(ADCODE),发布了天气信息。
由于该方法具有返回值,因此需要使用它。
此方法返回对象,然后添加到对象中。
然后使用该方法,当调用该方法时,主线程将始终被阻止,并完成子线程(或超时)。
应该注意的是,未指定上述代码,因此使用默认线程池。
它是一个共享线程池(基于服务器内核的限制,如果CPU是八核,则只能在每个线程处设置八个线程,并且无法自定义线程池)。如果不正确使用,它将对性能产生严重影响。因此,通常建议在这里使用自定义:
路口
包com.example.demo;
导入org.junit.test;
导入java.util.arrays;
导入java.util.list;
导入java.util.random;
导入java.util.concurrent。*;
/**
* 项目
*由Jorgezhong于2018/9/8 11:45创建。
*/
公共类plositablefuturedemo {
/**
*创建完整的未来
* -runasync
* -supplyAsync
* -pleteDedFutur
*
*异步计算线程池是守护线
*/
@teest
public void test1(){
// 1,异步计算:无返回值
//默认线程池是:forkjoinpool.commonpool()
完整future.runasync(() - > {{{{
// todo:2018/9/8无返回异步计算
system.out.println(thread.currentthread()。isdaemon());
});
//指定线程池,(到JDK9 CompletableFuture还展开延迟的线程池)
完整future.runasync(() - > {{{{
// todo:2018/9/8无返回异步计算
},执行者.netFixedThreadPool(2));
// 2,异步计算:有一个返回值
//使用默认线程池
完整的未来
// getNow指定引发异常的异步计算的值或结果返回被null代替的值
字符串结果1 = future1.getNow(null);
//指定线程池
完整的未来
// getNow指定引发异常的异步计算的值或结果返回被null代替的值
string result2 = funer2.getnow(null);
// 3.初始化一个完整的future
完整的未来
string now = future.getNow(null);
system.out.println(“ now =” + now);
}
/**
*计算时,您需要处理异常或过程结果
* - 完整:同步治疗包括异常
* -thenapply:同步处理正常结果(前提是没有异常)
*
* - 旋转链接:异步治疗包括异常
* - thenapplyasync:异步处理正常结果(前提是没有异常)
*
* - 外面:处理异常
*/
@teest
public void test2(){
完整的未来
//当Complete方法接受未来的结果和异常,可以灵活地处理它
// 1.同步治疗
//没有回报值:处理异常
future.whencomplete(结果,可投掷) - > system.out.println(“ result =” + result));
//有回报值:没有异常治疗(前提)
完整的未来
字符串结果1 = resultfuture1.getNow(null);
// 2,异步治疗:
//无返回值:默认线程池
future.whencOlterTeasync(结果,可投掷) - > system.out.println(“ result =” + result));
//无返回值:指定线程池
future.whencOlterTeasync(结果,可抛出) - > system.out.println(“ result =” +结果),executors.newfixedThreadPool(2));
//有一个返回值:默认线程池
完整的未来
string result2 = rastion.2.getNow(null);
//有一个返回值:指定的线程池
完整的未来
string result3 = rastion.3.getnow(null);
// 3.治疗异常,处理后返回结果
完整的未来
。
system.out.println(exceptionallyfuture.getnow(null));
}
/**
*异常处理也可以使用以下两种方法
* -处理
* - 和leassync
*
*备注:如果异常,可以从异常中抛出异常同步和异步计算。上述方法可以拦截
*/
@teest
public void test3(){
完整的未来
.thenapplyasync(s->“结果” + 1 /0);
字符串handleresult1 = exceptionohandle.handle((S,throwable) - > {
if(throwable!= null){
返回throwable.getMessage();
}
返回s;
})。getnow(null);
//指定线程池
字符串handleresult2 = extracionohandle.handleasync(s,throwable) - > {
if(throwable!= null){
返回throwable.getMessage();
}
返回s;
},执行者。netFixedThreadPool(2))。getnow(null);
}
/**
*生产消费
* -thenaccept:同步
* -thenacceptasync:异步
*
*接受先前的处理结果并实施消费者,消费结果
*/
@teest
public void test4(){
//同步
ploteablefuture.completedfuture(“ produce msg”)
。
//异步
//默认线程池
ploteablefuture.completedfuture(“ produce msg”)
.thenacceptasync(s-> system.out.println(“ async消耗sg:” + s));
//指定线程池
ploteablefuture.completedfuture(“ produce msg”)
.thenacceptasync(s-> system.out.println(“ async消耗sg:“ + s),executors.netfixedthreadpool(2)););););;);;);;);;);;);
}
/**
*取消任务
* -ancel
*/
@teest
public void test5()抛出InterruptedException {
完整的未来
尝试 {
螺纹。Sleep(800);
} catch(InterruptedException e){{{
e.printstacktrace();
}
返回s +“结果”;
});
string now = message.getNow(null);
system.out.println(“ now =” + now);
//取消
boolean cancel = message.cancel(true);
system.out.println(“ cancel =” + CANCAL);
//如果您再次在这里得到它,它将引发异常,表明它已被取消
//字符串NOW1 = message.getNow(null);
线程(1000);
}
/**
*两个异步计算
* -dlytoeither:有一个返回值,同步
* -Accepteither:无返回值,同步
* -dlytoeitherAsync:有一个返回值,异步
* -
*/
@teest
public void test6(){
完整的未来
.thenapply的计算结果(s-> task1:s1 =“ + s);
//同步,有一个返回值
// ApplyToeither的第二个参数接收值是Task1计算的返回值
完整的未来
.thenapply(s-> task2:s2 =“ + s),s-> s)的计算结果;
system.out.println(“ task2:” + result1.getnow(null));
//同步,无返回值
task1.Accepteither(ploteablefuture.completedfuture(“ task3”)
.thenapply(s-> task3:s3 =“ + s),s-> system.out.println(“ task3:” + s))的计算结果;
//有一个异步返回值,默认线程池也可以指定
完整的未来
.thenapply(s-> task4:s4 =“ + s),s-> s)的计算结果;
//因为它是异步的,所以主线程运行速度更快,因此join()只能看到运行后运行的结果。
system.out.println(“ task4:” + result2.join());
//异步无返回值,指定线程池,您也可以使用默认线程池
完整的未来
.thenapply(s-> task5:s5 =“ + s),s-> system.out.println(“ task5:” + s),executors.netfixedThreadPool(2))的计算结果;
task5.join();
}
/**
*组合计算结果
* -runafterboth:计算计算后,执行一部分代码
* -thenacceptboth:计算计算后,结果将传递并执行一部分代码
*
* -Thencombine:结合两个结果
* - 综合:组合的两个结果
*/
@teest
public void test7(){
// RunafterBoth
StringBuilder msg = new StringBuilder(“ Jorgezhong”);
ploteablefuture.completedfuture(msg)(msg)
。
。
。
// Renacceptboth
完整的Future.completedFuture(“ Jorgezhong”)
。
。
.thenapplyasync(String :: ToupperCase),(S,S2)
.println(“ s1:” + s +“,s2:” + s2));
// thencombine的方式
完整的未来
。
。
。
system.out.println(“结果1:” + result1.getnow(null));
//异步
完整的未来
。
.thencombineasync(plostablefuture.completedfuture(“ Jorgezhong”)
。
system.out.println(“ result11:” + result11.join());
// 道路
完整的未来
。
。
.thenapply(字符串:: Touppercase)
。
system.out.println(“结果2:” + result2.getnow(null));
//异步
完整的未来
。
.thencomposync(s-> ploteablefuture.completedfuture(“ Jorgezhong”)
.thenapplyasync(字符串:: touppercase)
.thenapplyasync(s1-> s:“ + s +”,s1:“ + s1));
system.out.println(“结果22:” + result22.join());
}
/**
*多个完整的未来策略
* -Anyof:接受完整的图数组,然后返回到任何任务。
* - 完成:在执行完成后计算一部分代码以执行实现,并将异常和异常传输为参数
*/
@teest
public void test8()抛出InterruptedException {
列表
完整future.anyof(messages.stram()
.map(o-> ploteablefuture.completedfuture(o).thenapplyasync(s-> {{{{{{{{{{{
尝试 {
thread.sleep(new Random()。int
} catch(InterruptedException e){{{
e.printstacktrace();
}
返回s.touppercase();
}))))
。
.whencomplete((Res,Throwable) - > {
if(throwable == null){
system.out.println(res.toString());
}
});
线程(1000);
}
/**
*多个完整的未来策略
* - allof:接受一系列完整的future。所有任务返回后,创建一个完整的图
*/
@teest
public void test9(){
列表
完整future [] cfs = messages.stream()
.map(s-> ploteablefuture.completedfuture(s).thenapplyasync(string :: touppercase))
。
allablefuture.allof(cfs)
.WHENCOLLETESYNC(避免,可投掷) - > arrays.stream(cfs)。foreach(pountablefuture-> system.out
。
}
} JDK8功能 - completableFuture
执行完整的线程
作者:阿克蒙