首页 > 编程知识 正文

java方法返回值返回给谁,java有返回值的方法

时间:2023-05-05 16:37:19 阅读:154945 作者:251

yjypalign=' center ' yjy/pyjypsupposethefollowingsimplifiedexample.letbrepresentaclassprocessingsomerasterdata 3360 yjy/leta yjy/p yjypprivate Runnable f; yjy/pyjyppublicb(Booleanc_,Runnable f_ ) { c=c_; f=f_; } yjy/pyjyppublicbufferedimageprocess () yjy/pyjyp//someoperationsyjy/pyjyp粗犷保温杯output=null; yjy/pyjypif(c ) output=newbufferedimage (100,100,BufferedImage.TYPE_INT_ARGB ); yjy/p yjypreturn output; yjy/pyjyp } yjy/pyjyppublicvoidrun () { process ); } yjy/pyjyp } yjy/pyjyptheprocess (methodmaybutmaynotcreateanoutputrater.duetothecomputationalcost, theprocedurerunsinaseparatethread.yjy/pyjypletarepresentaclassinsidewhichtheprocedurewillberun.italsocontainsomepostprocessstepswaitinguntilthethereadisfinished 30 pyjyppublicclassa { yjy/pyjyppublica (} yjy/pyjyppublicvoidcompute ) {yjy/p yjypboolean c=true; yjy/pyjypbb=newb(c,new Runnable ) ({yjy/p yjyppublic void run ) ) {yjy/p yjyp//Post process,afterthethreadhasbed ? //Get resulting raster,how? yjy/pyjypif(img!=null } { yjy/pyjyp//performsomestepsyjy/pyjyp } yjy/pyjyp } yjy/pyjyp }; yjy/pyjypthreadt=newthread(b; yjy/p yjypt.start (; //runprocedureyjy/pyjyp } yjy/pyjyp } yjy/pyjyphowever,howtogetresultingrastercreatedusingtheprocess () method ofb’ whentheoutputimagerepresentsadatamemberofbtogetherwithyjy/pyjypb.getimage; yjy/pyjypireadapostaboutcallbacksyjy/pyjypbuthowtoimplementithere? thanksforyourhelpandashortexample.yjy/pyjyp解决方案yjy/p yjypUse an ExecutorService、 specificallyitsubmit(callable ) methodwhichreturnsafuturewhichget (or is done ) methodscanbecalledtoretrievetheresult 33366666 py6 yjy/pyjyppublicb(Booleanc ) { this.c=c; } yjy/pyjyppublicbufferedimagecall () yjy/pyjyp//someoperationsyjy/pyjypif (! c ) yjy/p yjypreturn null; yjy/pyjypreturnnewbufferedimage (100,100,BufferedImage.TYPE_INT_ARGB ); yjy/pyjyp } yjy/pyjyp } yjy/pyjyp//somewhere,e.g.inyoura.com pute (methodyjy/pyjypwxdylexe=executors.com) yjy/pyjypfutureres=exe.submit (b ); //willreturnimmediately.processingwillruninathreadof ' exe ' executory jy/pyjyp//.dothingsyjy/pyjyhdws.out.pexexe //willblockuntilimageisavailable (I.e.b.call ) ) returns ) yjy/pyjypyoucanusedifferentflavorsofexecutorserviceinwhichyoucanqueueprocessingswhichmay eturn aresult.thetypeofexecutoryouwanttousedependsonthetypeofprocessingandorderyouneed.yjy/p

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。